Magento Forum

Page 1 of 3
Adding custom attribute filter in product grid (in Admin)
 
cooper
Sr. Member
 
Total Posts:  104
Joined:  2008-03-31
 

OK,

I am trying to add custom columns to the product grid within the Adminhtml module. I want to add columns that display custom product attributes and obviously filter the product grid by those attribute values.

I have an attribute called “classification” . I have been able to add the custom column to the grid, but no values are displayed in the column (see image 1) although the filter is working good (see image 2).

And here the snippet of code for the grid: app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. I have first add to the collection : ->addAttributeToSelect(’classification’) :

protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')

            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            ->
joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left')

         
// my code
        
->addAttributeToSelect('classification');
        
// my code

and the columns in :

protected function _prepareColumns()
    
{
        $this
->addColumn('id',
            array(
                
'header'=> Mage::helper('catalog')->__('ID'),
                
'width' => '50px',
                
'type'  => 'number',
                
'index' => 'entity_id',
        ));
        
$this->addColumn('name',
            array(
                
'header'=> Mage::helper('catalog')->__('Name'),
                
'index' => 'name',
        ));

        
$store $this->_getStore();
        if (
$store->getId()) {
            $this
->addColumn('custom_name',
                array(
                    
'header'=> Mage::helper('catalog')->__('Name In %s'$store->getName()),
                    
'index' => 'custom_name',
            ));
        
}

        $this
->addColumn('type',
            array(
                
'header'=> Mage::helper('catalog')->__('Type'),
                
'width' => '60px',
                
'index' => 'type_id',
                
'type'  => 'options',
                
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
        ));

        
$sets Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->
setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->
load()
            ->
toOptionHash();

        
$this->addColumn('set_name',
            array(
                
'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
                
'width' => '100px',
                
'index' => 'attribute_set_id',
                
'type'  => 'options',
                
'options' => $sets,
        ));

        
$this->addColumn('sku',
            array(
                
'header'=> Mage::helper('catalog')->__('SKU'),
                
'width' => '80px',
                
'index' => 'sku',
        ));

         
// my code
                
$this->addColumn('classification',
            array(
                
'header'=> Mage::helper('catalog')->__('Classification'),
                
'width' => '80px',
                
'index' => 'classification',
        ));
         
// my code

Does anyone know what am I doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
cooper
Sr. Member
 
Total Posts:  104
Joined:  2008-03-31
 

Below showing:

Image1 : the column ‘Classification’ without any values
Image2 : the same column working with the filter

 
Magento Community Magento Community
Magento Community
Magento Community
 
LindyKyaw
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2008-01-02
Los Angeles
 

What version are you working on?
I cannot reproduce the issue.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cooper
Sr. Member
 
Total Posts:  104
Joined:  2008-03-31
 

1.1.6

 
Magento Community Magento Community
Magento Community
Magento Community
 
cooper
Sr. Member
 
Total Posts:  104
Joined:  2008-03-31
 

Lindy,

You are right : it works! Your observation give me a way.

In fact I had created a local module :

1 - edit my own :  /app/code/local/Mymodule/Adminhtlm/Block/catalog/Product/Grid.php
2 - replace class definition in Grid.php by : Class Mymodule_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
3 - create : /app/etc/modules/Mymodule.xml and /app/etc/local.xml

And the result is as describe in my first post.

So what I have done after your response is to put the code in app/code/local/Mage/Adminhtlm/Block/catalog/Product/Grid.php (with the original class defintion) and eveythings work now.

I am not a developper and I don’t know how to do this with a custom module. Do you have an idea?

I hope my english is comprehensible enough. tongue rolleye

 
Magento Community Magento Community
Magento Community
Magento Community
 
LindyKyaw
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2008-01-02
Los Angeles
 

Cooper,

You can look at an example from following wiki and thread how create a custom module to overwrite the core class.
http://www.magentocommerce.com/wiki/groups/174/changing_and_customizing_magento_code
http://www.magentocommerce.com/boards/viewthread/14692/

 
Magento Community Magento Community
Magento Community
Magento Community
 
cckean31
Jr. Member
 
Total Posts:  11
Joined:  2008-09-07
 

Hi,

Wanted to achieve the same purposed for Admin>Sales>Order, where i wanted to add additional attribute “Delivery Date” which i’d managed to add in along with every orders made.

I’d managed to add the column from the ...Adminhtml\Block\Sales\Order\grid.php, and i managed to find out the php file that fletch the order records to display at Admin>Sales>Order is .....Adminhtml\Block\widget\grid.php..but the code is different from cooper where i can just simpley add in ‘addAttributeToSelct(’delivery_date’) where this attribute is stored in gift_message table.

Can anyone give your valuable advice here?
tx

 
Magento Community Magento Community
Magento Community
Magento Community
 
jawshchevyvan
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2008-06-12
 

ok i don’t know if this will help you all at all.... but i needed to haxor a freaking manufacturer column for my client.  i was totally successful in getting the data to display in the admin view (ROX!!!!), but the values were just the integers of the manufacturer id, not the title.

manufacturer is attribute_id=55 in the database
then i was getting the correct option_id’s (which were a collection related to attribute_id=55
i needed to grab the values of the plain text label associated with the option id (for all stores too)
this is a little sloppy as i haven’t abstracted anything out into its own module here.

so play with risk!!!!

/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            
//codeMod
            
->addAttributeToSelect('manufacturer')
            
//endCodeMod
            
->joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left');

and in _prepareColumns()

$manufacturers Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->
setAttributeFilter(55)
            ->
setStoreFilter()
            ->
load()
            ->
toOptionHash('option_id''value');
            
        
        
$this->addColumn('manufacturer',
            array(
                
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'index' => 'manufacturer',
                
'type' => 'options',
                
'options' => $manufacturers
        
));

here is the scariest part (but my roommate doesn’t really care about magento so, he was like, well this will work, if it breaks other stuff its on you.... so again beware…

/app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Option/Collection.php
around line 96.

// Modded to allow fieldnames to pass in.
    public function toOptionHash($valueField='id'$labelField='name'{
        
return $this->_toOptionHash($valueField$labelField);
    
}

this is working for me… hopefully its helpful for you all.
remember this ain’t gwana’ work unless you got the Manufacture attribute going on.

 
Magento Community Magento Community
Magento Community
Magento Community
 
fenixf
Member
 
Total Posts:  35
Joined:  2008-04-08
 

Hi there ,

Thanks for the nice info given here!

I stumbled across this post by accident, and found it very interesting. Ofcourse one has to be able to do selecting by custom attributes in the backend,

Anyway:
Got this working fine for me with a custom selectattribute.

But: I am trying to get this to work for an other attribute where one can choose multiple options.

And guess what? It does not work.

I got the options shown in the collumn header but they won’t show up in the grid itself. Filtering with this option returns zero products.

some code:

->addAttributeToSelect('Druif')   // this one is fine. It's a select input type
     
->addAttributeToSelect('Smaak')// this one not working it's a multiselect
Code for the collumn itself
$arrSoort Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->
setAttributeFilter(555)
            ->
setStoreFilter()
            ->
load()
            ->
toOptionHash('option_id''value');
          
        
        
$this->addColumn('Smaak',
            array(
                
'header'=> Mage::helper('catalog')->__('Smaak'),
                
'index' => 'Smaak',
                
'type' => 'options',
                
'options' => $arrSoort,
        ));

I noticed that in the eav_entity table the attrubutes with multiple select have
a “eav/entity_attribute_backend_array” value in their collumn “backend_model” and the normal select has nothing in it

I looked at the visibilty collumn because there you have also multiple options like “Catalog,Search” when both are selected; But no luck.

Any help would be appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
invaderflid
Member
 
Avatar
Total Posts:  59
Joined:  2008-10-21
Northport Long Island New York
 

jawshchevyvan,

I tried using your example, the manufacturer field and column appearsin the admin section however neither the drop down or any of the items have the new column populated with any data.  I have manufacturers associated with my products, and manufacturers appear on the front-end just fine. Any idea on what I could be missing?

 
Magento Community Magento Community
Magento Community
Magento Community
 
fenixf
Member
 
Total Posts:  35
Joined:  2008-04-08
 

/app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Option/Collection.php
around line 96.

// Modded to allow fieldnames to pass in.
    public function toOptionHash($valueField='id'$labelField='name'{
        
return $this->_toOptionHash($valueField$labelField);
    
}

@invaderflid:

Did you modify above too?

 
Magento Community Magento Community
Magento Community
Magento Community
 
invaderflid
Member
 
Avatar
Total Posts:  59
Joined:  2008-10-21
Northport Long Island New York
 

@fenixf

I made the change, however is that supposed to be appended to the file or is it meant to replace a function?  I’ve found that

$manufacturers Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->
setAttributeFilter(55)
            ->
setStoreFilter()
            ->
load()
            ->
toOptionHash('option_id''value');
        
         
$this->addColumn('manufacturer',
            array(
                
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'width' => '80px',
                
'index' => 'manufacturer',
                
'type'  => 'options',
                
'options' => $manufacturers,
        ));

Would not output anything into the drop down or the fields, however when I made these changes:

$manufacturers Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->
setAttributeFilter(55)
            ->
setStoreFilter()
            ->
load()
            ->
toOptionHash('option_id''value');
        
         
$this->addColumn('manufacturer',
            array(
                
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'width' => '80px',
                
'index' => 'manufacturer',
            
/*    'type'  => 'options',
                'options' => $manufacturers,*/
        
));

The fields were populated with numbers.  Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
fenixf
Member
 
Total Posts:  35
Joined:  2008-04-08
 

The function should be replaced in /app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Option/Collection.php

Check also if your the abbtributeid of manufacturer is 55 (by me it’s something else: 66)

And also try to renew the page a few times whenever you made change to the code; That also helps:

Hope this helps. I’ll be back on tomorrow.

Btw did you do:

->addAttributeToSelect('manufacturer')
?
 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 
jawshchevyvan - 31 October 2008 12:39 PM

ok i don’t know if this will help you all at all.... but i needed to haxor a freaking manufacturer column for my client.  i was totally successful in getting the data to display in the admin view (ROX!!!!), but the values were just the integers of the manufacturer id, not the title.

manufacturer is attribute_id=55 in the database
then i was getting the correct option_id’s (which were a collection related to attribute_id=55
i needed to grab the values of the plain text label associated with the option id (for all stores too)
this is a little sloppy as i haven’t abstracted anything out into its own module here.

so play with risk!!!!

/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

protected function _prepareCollection()
    
{
        $store 
$this->_getStore();
        
$collection Mage::getModel('catalog/product')->getCollection()
            ->
addAttributeToSelect('sku')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('attribute_set_id')
            ->
addAttributeToSelect('type_id')
            
//codeMod
            
->addAttributeToSelect('manufacturer')
            
//endCodeMod
            
->joinField('qty',
                
'cataloginventory/stock_item',
                
'qty',
                
'product_id=entity_id',
                
'{{table}}.stock_id=1',
                
'left');

and in _prepareColumns()

$manufacturers Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->
setAttributeFilter(55)
            ->
setStoreFilter()
            ->
load()
            ->
toOptionHash('option_id''value');
            
        
        
$this->addColumn('manufacturer',
            array(
                
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'index' => 'manufacturer',
                
'type' => 'options',
                
'options' => $manufacturers
        
));

here is the scariest part (but my roommate doesn’t really care about magento so, he was like, well this will work, if it breaks other stuff its on you.... so again beware…

/app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Option/Collection.php
around line 96.

// Modded to allow fieldnames to pass in.
    public function toOptionHash($valueField='id'$labelField='name'{
        
return $this->_toOptionHash($valueField$labelField);
    
}

this is working for me… hopefully its helpful for you all.
remember this ain’t gwana’ work unless you got the Manufacture attribute going on.

check this out, maybe this can help you :

$items Mage::getModel('eav/entity_attribute_option')->getCollection()
    ->
setStoreFilter()
    ->
join('attribute','attribute.attribute_id=main_table.attribute_id''attribute_code');
foreach (
$items as $item) :
    if (
$item->getAttributeCode() == 'manufacturer'
        
$x[$item->getOptionId()$item->getValue();
endforeach;
 
Magento Community Magento Community
Magento Community
Magento Community
 
invaderflid
Member
 
Avatar
Total Posts:  59
Joined:  2008-10-21
Northport Long Island New York
 

@ alkarim

Thanks that solved my issue.

@fenixf

Thanks for the help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
wolfdog85
Jr. Member
 
Total Posts:  12
Joined:  2008-09-29
Biloxi, MS, USA
 

I am having the same issue… I have follow all the instructions and read other threads on this issue and can’t get it to work. I want to overload the class and am doing so by the manual and other threads.

/app/code/local/Bayview/Adminhtml/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Bayview_Adminhtml>
            <
version>0.1.0</version>
        </
Bayview_Adminhtml>
    </
modules>
    <global>
        <
blocks>
            <
adminhtml>
                <
rewrite>
                    <
catalog_product_grid>Bayview_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                </
rewrite>
            </
adminhtml>
        </
blocks>
    </global>
</
config>

/app/etc/modules/Bayview_Adminhtml.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Bayview_Adminhtml>
                <
active>true</active>
                <
codePool>local</codePool>
        </
Bayview_Adminhtml>
    </
modules>
</
config>

/app/code/local/Bayview/Adminhtml/Block/Catalog/Product/Grid.php

class Bayview_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid

IN function _prepareCollection()
ADDED

->addAttributeToSelect('manufacturer'//my code
        
->addAttributeToSelect('group'//my code

IN function _prepareColumns()
ADDED

$manufacturer_items Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id''attribute_code');
        foreach (
$manufacturer_items as $manufacturer_item) :
            if (
$manufacturer_item->getAttributeCode() == 'manufacturer'
                
$manufacturer_options[$manufacturer_item->getOptionId()$manufacturer_item->getValue();
        endforeach; 
        
    
        
$this->addColumn('manufacturer',
            array(
                
'header'=> Mage::helper('catalog')->__('Manufacturer'),
                
'width' => '180px',
                
'index' => 'manufacturer',
                
'type'  => 'options',
                
'options' => $manufacturer_options,
        )); 
        
        
$group_items Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id''attribute_code');
        foreach (
$group_items as $group_item) :
            if (
$group_item->getAttributeCode() == 'group'
                
$group_options[$group_item->getOptionId()$group_item->getValue();
        endforeach; 
        
    
        
$this->addColumn('group',
            array(
                
'header'=> Mage::helper('catalog')->__('Group'),
                
'width' => '180px',
                
'index' => 'group',
                
'type'  => 'options',
                
'options' => $group_options,
        ));

I’m getting the same problem. The columns show up and if you do the drop down and provide filter, the values will show up in the grid, but if you are providing no filter, the values don’t show up in the grid.

Any ideas?

See screenshots.

Image Attachments
ss_no_filter.jpgss_w_filter.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3