skip to Main Content

In Magento, there’s a column in Product Grid/Table which is Name.

It only accepts or filter the table if you put the exact word/s.

Sample is “What Happened Last Night” (just an example)

If you put “What” it will filter correctly, if you add “Happened” it will still filter correctly but if you input “What Last” it will return 0 records unless there’s a “What Last” record.

I have this code in the core itself.

 $this->addColumn('name',
        array(
            'header'=> Mage::helper('catalog')->__('Name'),
            'index' => 'name',
            'filter_condition_callback' => array($this, '_customNameFilter'),
    ));

    $store = $this->_getStore();
    if ($store->getId()) {
        $this->addColumn('name_search', array(
            'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
            'type'  => 'text',
            'filter_condition_callback' => array($this, '_customNameFilter'),
        ));
    }

Based on this link, Grid filter for columns with complex values

I need to remove the index and adjust something or some files in which I’m totally lost as the author did not mention the file paths used.

Can anyone guide me on what file should I adjust or create if needed.

The Code above has the file path of

appcodecoreMageAdminhtmlBlockCatalogProductGrid.php

What other files/modules should I adjust an what lines of code should I’d be looking at to attain this result.

I could not thank you enough for assisting me.

2

Answers


  1. Atwix example will not solve the problem you described. If you want to make a filter smart enough to be on-the-fly tokenizer you will have to split the search string by spaces and add each token to where part of the query with “OR LIKE %’.$token.’%”

    Solution

    you will have to rewrite Mage_Adminhtml_Block_Catalog_Product_Grid in a local Module with following xml:

    /app/code/local/Your/Module/etc/config.xml
    
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>Your_Module_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
    

    and add following methods into class

    /app/code/local/Your/Module/Block/Adminhtml/Catalog/Product/Grid.php
    
    
    class Your_Module_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
    
            protected function _prepareColumns()
            {
                $this->addColumn('name_search', array(
                    'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
                    'type'  => 'text',
                    'filter_condition_callback' => array($this, '_customNameFilter'),
                ));
                return parent::_prepareColumns();
            }
    
            protected function _customNameFilter($collection, $column)
            {
                if (!$value = $column->getFilter()->getValue()) {
                    return $this;
                }
    
                $tokens = explode(' ', $value);
    
                $where = "LIKE '%".$value."%'";
                foreach($tokens as $token) {
                    $where .= " OR LIKE '%".$token."%'";
                }
    
                $this->getCollection()->getSelect()->where(
                    "(at_name.value ?)"
                , $where);
    
    
                return $this;
            }
        }
    

    And dont forget to rename Your_Module with your own namespace and add module description xml to /app/etc/modules/

    you’re welcome

    Login or Signup to reply.
  2. here is a working solution for your case

    protected function _customNameFilter($collection, $column)
    {
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    } else if (preg_match('/s+/', $value)) {
    
            $tokens = explode(' ', $value);
    
        foreach($tokens as $token) {
            $this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$token.'%'));
        }             
    } else {   
        $this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$value.'%'));
    }
    return $this;
    }
    

    this space tokenizer filter will also work with any other grid field.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search