Enhanced Catalog Product Grid – Custom Attribute Filter

Enhanced Catalog Product Grid – Custom Attribute Filter

Sometimes one needs to have additional filtering criteria inside of a catalog’s product grid. Here is an extension that adds custom product attribute to search filter. You can choose to use any product attribute and it will be shown in the product grid.

The extension settings shows up under System->Configuration menu in its separate tab.

I decided to ignore some attributes, so not all are shown. Ignored are attributes:

  • which are already selected by the product grid (‘sku’, ‘name’, ‘qty’, ‘price’, ‘status’, ‘visibility’)
  • that have no products assigned to them

The “Attribute to filter by” combo box is declared in extensions system.xml file as:

<filter_attribute>
<label>Attribute to filter by</label>
<frontend_type>select</frontend_type>
<source_model>inchoo_product_filter/attributes</source_model>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</filter_attribute>

The model with the reference “inchoo_product_filter/attributes” is shown here:

<?php
 
class Inchoo_Productfilter_Model_Attributes
{
public function toOptionArray()
{
$ignoreAttributes = array('sku', 'name', 'attribute_set_id', 'type_id', 'qty', 'price', 'status', 'visibility');
 
$collection = Mage::getResourceModel('catalog/product_attribute_collection')
->addVisibleFilter();
 
$result = array();
foreach ($collection as $model) {
if(in_array($model->getAttributeCode(), $ignoreAttributes)) {
continue;
}
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect(array($model->getAttributeCode()));
$productCollection->addAttributeToFilter($model->getAttributeCode(), array('gt' => 0));
 
if(count($productCollection->getData()) > 0) {
$result[] = array('value' => $model->getAttributeCode(), 'label'=>$model->getFrontendLabel());
}
 
}
 
return $result;
 
}
}

The chosen product attribute is shown in the product grid:

To add another column to the grid we need to override the Magento’s class: Mage_Adminhtml_Block_Catalog_Product_Grid.

First the _prepareCollection() function:

protected function _prepareCollection()
{
if(Mage::getStoreConfig(self::CONFIG_ENABLED)) {
//removed parent's code for clarity
//this extension specific
$attributeCode = Mage::getStoreConfig(self::CONFIG_ATTRIBUTE_CODE);
$collection->joinAttribute($attributeCode, 'catalog_product/'.$attributeCode, 'entity_id', null, 'left');
$collection->addAttributeToSelect($attributeCode);
 
$this->setCollection($collection);
 
Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
//this extension specific end
} else {
parent::_prepareCollection();
}
return $this;
 
}

Then we add custom column by averriding _prepareColumns() function:

protected function _prepareColumns()
{
if(Mage::getStoreConfig(self::CONFIG_ENABLED)) {
// removed parent's code for other columns for clarity
// this extension specific
 
$attributeCodeConfig = Mage::getStoreConfig(self::CONFIG_ATTRIBUTE_CODE);
 
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $attributeCodeConfig);
 
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
 
$attributeOptions = $attribute->getSource()->getAllOptions();
$b = new Mage_Catalog_Model_Resource_Eav_Attribute();
$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
if(!empty($value['value'])) {
$attributeOptions2[$value['value']] = $value['label'];
}
 
}
 
if(count($attributeOptions2) > 0) {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
'type' => 'options',
'options' => $attributeOptions2,
 
));
} else {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
 
));
}
 
// this extension specific end
// removed parent's code for other columns for clarity
 
$this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
} else {
parent::_prepareColumns();
}
 
return $this;
}

This is definitely not all the code used in this extension. You can download the newest version of the extension here.

You made it all the way down here so you must have enjoyed this post! You may also like:

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

Magento Grid Serializer for Ajax Grids Damir Korpar
Damir Korpar, | 10

Magento Grid Serializer for Ajax Grids

55 comments

  1. I used your extension but its not showing all custom created attributes in dropdown. Please help me to solve that issue.

  2. Hi Milovan,

    Thanks for the great post and knowledge sharing. Can you please help to let me know if there is any extension available or any way to add custom filters in front-end product listing.

    In Magento product listing page (front-end) there are options of sort by and all. Support I need to add custom options in sort by drop-down or I need to add one more drop-donw of filtering products by country.

    Is there any way to do this?

    Thanks in advance for your help.

  3. This extension does not display all filter attributes, I meant, I have added 3 custom attributes for product only one of them is shown in the dropdown option, 🙁

  4. This extension is great, however, I need to filter by unit price range just like the price column has ‘from’ and ‘to’ range, I added the unit price column just fine but how can I filter by unit price range?

  5. I’m getting:
    Fatal error: Class ‘Inchoo_Productfilter_Helper_Data’ not found in /var/www/vhosts/bato/public_html/app/Mage.php on line 545

    URL to extantion is: /app/code/local/Inchoo/Productfilter/

    Please help me 🙁

  6. after being installed, I add attribute but it not show. what do i need to config more ?

  7. thanks,works great, but how can I use it in related product section ?
    right now it only shows in product management page,I want this to work in related product section when editing a product.

  8. I wanted qty as a filter for product listing in front end.. meaning in list page i wanted to give user the feature to filter down for products that have x qty or a range of qty.. is this possible?

    1. I have added your codes in my magento 1.9.1.1 store.But after adding when I am trying to open cache management it is showing this error

      Fatal error: Call to a member function setColumn() on a non-object in /home/cloudpanel/htdocs/test.zotezo.com/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column.php on line 291

      Also in admin manage product section it is also showing this error-
      Fatal error: Call to a member function setColumn() on a non-object in /home/cloudpanel/htdocs/test.zotezo.com/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column.php on line 291
      How to rectify the error?
      Thanks.

  9. If somebody is looking for good navigation extension for your online shop, i strongly recommended amasty’s one.

    If you have some questions you can write to this guys and they will help you.

    Best,
    Matt

  10. how can i filter attribute with category. i mean, i want size attribute used in a specific category.
    For example if shoe is the category i want to list size of all products used in the shoe category.
    plz help me

  11. Nice post but i have one question about “how add CUSTOM filter” to grid.

    for example, i have grid with product, add column with selected and group category.

    Product is in category ID 3,7,11…

    How can i filter “in” (how filter only product in category 7…)?

    My code is:

    protected function _prepareCollection()
    {
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
    $collection = Mage::getModel(‘catalog/product’)->getCollection();
    $collection->addAttributeToSelect(‘*’);
    $collection->joinField(‘category_id’, ‘catalog/category_product’, ‘category_id’, ‘product_id=entity_id’, null, ‘left’);
    $collection->groupByAttribute(‘entity_id’);
    $collection->addStaticField(‘category_id’);
    $collection->addExpressionAttributeToSelect(‘category_grp’, ‘GROUP_CONCAT(category_id)’, ‘category_id’);

    $this->setCollection($collection);
    return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
    parent::_prepareColumns();

    $this->addColumn(‘category_id’, array(
    ‘header’ => Mage::helper(‘newsletter’)->__(‘Category’),
    ‘index’ => ‘category_grp’,
    ‘type’ => ‘categories’,
    ‘options’ => $options,
    ‘align’ => ‘left’,
    // ‘filter_index’ => $this->_getFlatExpressionColumn(‘category’),
    return $this;
    }

  12. hey.
    Them module works great. I would like to sort the products if the have images or not. Any ideas?

  13. Hi nilesh,
    You probably didn’t unpack the extension in the right place. Please, unpack it in the root of your Magento installation. Then delete var/cache directory to be sure.

    If this doesn’t solve your problem, please tell me which version of Magento you are using.

  14. Fatal error: Class ‘Inchoo_Productfilter_Helper_Data’ not found in C:\xampp\htdocs\AthleticPlanet\app\Mage.php on line 546

    i am getting above error while trying to open admin panel

  15. Hi,

    How can I make this compatible with Mass Product Actions from Amasty. Anyone? It’s seems so cool to select product by attribute and than assign them in bulk to a category. But this code removes the other extension..

  16. @ Manpreet,

    Did you log out and log in back to Admin? This is a standard procedure when installing any Magento extension.

  17. I have installed this extension in Magento 1.7 version it is showing in System > Configuration but when i open it 404 Error
    Page not found.
    is coming.

    Can anyone help me.
    Thanks in Advance

  18. Hi,

    Thanks for your extension. Unfortunately it doesn’t work with Magento 1.6. I have two problems:

    1) The “$attributeOptions = $attribute->getSource()->getAllOptions(true);” return an empty array.

    2) My custom column attribute is empty when I load the “catalog_product/index/” page. But the filter function works well.

    Any idea?
    Thanks!

  19. Hello,

    Thanks for your post!

    Nevertheless, i have a problem. After using the “Name” filter, the custom attribute values disappear…
    Weird thing is this is only happening with the “Name” column.
    When debugging, values are like decimals. Hence, the labels not showing up.
    Do you have any clue ?

    Thanks in advance

  20. It’s installed and works, but it only finds a few of my custom attributes, and not others? I’m confused by what it’s looking for. I want the attribute called “photographer” in this, however, the dropdown only shows weight, tax class and is product available on google checkout. What am I missing?

  21. Anyone know an extension that will let me do this for the front end? (i.e. customers filtering by custom attributes)

  22. @Adrian,

    Did you try to log out, and then log back into admin?
    This usually needs to be done after extension installation to update Magento Admin permissions.

    Cheers

  23. Hello,

    After I copied the extension to Magento and refreshed cache the Inchoo section showed in configuration, but trying to enter it results in “404 error”.

    Is there anything else that should be done except copying?

    Thx in advance,
    Adrian

  24. @Milovan

    That’s great Milovan, nice to see it’s not me breaking it for once!

    Downloading now.

    Thanks again

  25. @Andy

    Thank you very much for your post and for finding a bug!
    I have just fixed the issue with not showing the sku and updated the download link to a new version of the extension.

    Thanks again!
    Cheers

  26. Hi, I’ve got this installed and it works ok, but when active, it prevents the sku from showing/being searchable.

    It’s not a huge problem as I just enable the module when I need to filter, but any ideas what might be causing this? We’re on 1.5.1 and don’t have any other extensions installed that affect the product grid.

    Thanks

  27. Product grid with custom column

    View all products the custom column is great
    name custom
    aaa xxx
    bbb yyyy

    View filtered products (example: name = aaa) the custom column is empty
    name custom
    aaa empty

  28. @IdeI
    I am not sure I understood you well. If I didn’t, please, rephrase your question!
    For attributes that don’t have options, the custom column header is a text field, so you need to enter the text you want to search for.

  29. Great work, but…

    I have a problem… when I use the grid filter, in the custom columb is empty why?

    Perfect work all products

  30. @Milovan

    Yes, totally a rookie mistake. It works great. Thank you for sharing this, it is Very helpful.

  31. @D Hassen,

    I just tested it on EE 1.8 and it works for me.

    Do you have any other extension which affects the product grid?

  32. I couldn’t get this working on EE 1.8. I just get a blank screen when I go to the Product Filter config page, even after refreshing the cache and logging back in.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.