title: Magento - Adminhtml Trying to search in the product grid by custom column
body: <p>I am using Magento 1.9.2 and i am rewriting the products grid table.</p>
  
  <p>I have done a copy from the original Grid.php and created this:</p>
  
  <p>/app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php and here is what it contains:</p>
  
  <pre><code>&lt;?php
  class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid
  {
  
      public function __construct()
      {
          parent::__construct();
          $this-&gt;setId('productGrid');
          $this-&gt;setDefaultSort('entity_id');
          $this-&gt;setDefaultDir('DESC');
          $this-&gt;setSaveParametersInSession(true);
          $this-&gt;setUseAjax(true);
          $this-&gt;setVarNameFilter('product_filter');
  
      }
  
      protected function _getStore()
      {
          $storeId = (int) $this-&gt;getRequest()-&gt;getParam('store', 0);
          return Mage::app()-&gt;getStore($storeId);
      }
  
      protected function _prepareCollection()
      {
          $store = $this-&gt;_getStore();
          $collection = Mage::getModel('catalog/product')-&gt;getCollection()
              -&gt;addAttributeToSelect('sku')
              -&gt;addAttributeToSelect('name')
              -&gt;addAttributeToSelect('attribute_set_id')
              -&gt;addAttributeToSelect('type_id');
  
          if (Mage::helper('catalog')-&gt;isModuleEnabled('Mage_CatalogInventory')) {
              $collection-&gt;joinField('qty',
                  'cataloginventory/stock_item',
                  'qty',
                  'product_id=entity_id',
                  '{{table}}.stock_id=1',
                  'left');
          }
          if ($store-&gt;getId()) {
              //$collection-&gt;setStoreId($store-&gt;getId());
              $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
              $collection-&gt;addStoreFilter($store);
              $collection-&gt;joinAttribute(
                  'name',
                  'catalog_product/name',
                  'entity_id',
                  null,
                  'inner',
                  $adminStore
              );
              $collection-&gt;joinAttribute(
                  'custom_name',
                  'catalog_product/name',
                  'entity_id',
                  null,
                  'inner',
                  $store-&gt;getId()
              );
              $collection-&gt;joinAttribute(
                  'status',
                  'catalog_product/status',
                  'entity_id',
                  null,
                  'inner',
                  $store-&gt;getId()
              );
              $collection-&gt;joinAttribute(
                  'visibility',
                  'catalog_product/visibility',
                  'entity_id',
                  null,
                  'inner',
                  $store-&gt;getId()
              );
              $collection-&gt;joinAttribute(
                  'price',
                  'catalog_product/price',
                  'entity_id',
                  null,
                  'left',
                  $store-&gt;getId()
              );
          }
          else {
              $collection-&gt;addAttributeToSelect('price');
              $collection-&gt;joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
              $collection-&gt;joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
          }
  
          $this-&gt;setCollection($collection);
  
          parent::_prepareCollection();
          $this-&gt;getCollection()-&gt;addWebsiteNamesToResult();
          return $this;
      }
  
      protected function _addColumnFilterToCollection($column)
      {
          if ($this-&gt;getCollection()) {
              if ($column-&gt;getId() == 'websites') {
                  $this-&gt;getCollection()-&gt;joinField('websites',
                      'catalog/product_website',
                      'website_id',
                      'product_id=entity_id',
                      null,
                      'left');
              }
          }
          return parent::_addColumnFilterToCollection($column);
      }
  
      protected function _prepareColumns()
      {
          $this-&gt;addColumn('entity_id',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('ID'),
                  'width' =&gt; '50px',
                  'type'  =&gt; 'number',
                  'index' =&gt; 'entity_id',
          ));
          $this-&gt;addColumn('name',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Name'),
                  'index' =&gt; 'name',
          ));
  
          $store = $this-&gt;_getStore();
          if ($store-&gt;getId()) {
              $this-&gt;addColumn('custom_name',
                  array(
                      'header'=&gt; Mage::helper('catalog')-&gt;__('Name in %s', $store-&gt;getName()),
                      'index' =&gt; 'custom_name',
              ));
          }
  
          $this-&gt;addColumn('type',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Type'),
                  'width' =&gt; '60px',
                  'index' =&gt; 'type_id',
                  'type'  =&gt; 'options',
                  'options' =&gt; Mage::getSingleton('catalog/product_type')-&gt;getOptionArray(),
          ));
  
          $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
              -&gt;setEntityTypeFilter(Mage::getModel('catalog/product')-&gt;getResource()-&gt;getTypeId())
              -&gt;load()
              -&gt;toOptionHash();
  
          $this-&gt;addColumn('set_name',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Attrib. Set Name'),
                  'width' =&gt; '100px',
                  'index' =&gt; 'attribute_set_id',
                  'type'  =&gt; 'options',
                  'options' =&gt; $sets,
          ));
  
          $this-&gt;addColumn('sku',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('SKU'),
                  'width' =&gt; '80px',
                  'index' =&gt; 'sku',
          ));
  
          $this-&gt;addColumn('number',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Поръчка №'),
                  'width' =&gt; '50px',
                  'index' =&gt; 'entity_id',
                  'renderer'  =&gt; 'Mage_Adminhtml_Block_Catalog_Product_Renderer',
          ));
  
          $store = $this-&gt;_getStore();
          $this-&gt;addColumn('price',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Price'),
                  'type'  =&gt; 'price',
                  'currency_code' =&gt; $store-&gt;getBaseCurrency()-&gt;getCode(),
                  'index' =&gt; 'price',
          ));
  
          if (Mage::helper('catalog')-&gt;isModuleEnabled('Mage_CatalogInventory')) {
              $this-&gt;addColumn('qty',
                  array(
                      'header'=&gt; Mage::helper('catalog')-&gt;__('Qty'),
                      'width' =&gt; '100px',
                      'type'  =&gt; 'number',
                      'index' =&gt; 'qty',
              ));
          }
  
          $this-&gt;addColumn('visibility',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Visibility'),
                  'width' =&gt; '70px',
                  'index' =&gt; 'visibility',
                  'type'  =&gt; 'options',
                  'options' =&gt; Mage::getModel('catalog/product_visibility')-&gt;getOptionArray(),
          ));
  
          $this-&gt;addColumn('status',
              array(
                  'header'=&gt; Mage::helper('catalog')-&gt;__('Status'),
                  'width' =&gt; '70px',
                  'index' =&gt; 'status',
                  'type'  =&gt; 'options',
                  'options' =&gt; Mage::getSingleton('catalog/product_status')-&gt;getOptionArray(),
          ));
  
          if (!Mage::app()-&gt;isSingleStoreMode()) {
              $this-&gt;addColumn('websites',
                  array(
                      'header'=&gt; Mage::helper('catalog')-&gt;__('Websites'),
                      'width' =&gt; '100px',
                      'sortable'  =&gt; false,
                      'index'     =&gt; 'websites',
                      'type'      =&gt; 'options',
                      'options'   =&gt; Mage::getModel('core/website')-&gt;getCollection()-&gt;toOptionHash(),
              ));
          }
  
          $this-&gt;addColumn('action',
              array(
                  'header'    =&gt; Mage::helper('catalog')-&gt;__('Action'),
                  'width'     =&gt; '50px',
                  'type'      =&gt; 'action',
                  'getter'     =&gt; 'getId',
                  'actions'   =&gt; array(
                      array(
                          'caption' =&gt; Mage::helper('catalog')-&gt;__('Edit'),
                          'url'     =&gt; array(
                              'base'=&gt;'*/*/edit',
                              'params'=&gt;array('store'=&gt;$this-&gt;getRequest()-&gt;getParam('store'))
                          ),
                          'field'   =&gt; 'id'
                      )
                  ),
                  'filter'    =&gt; false,
                  'sortable'  =&gt; false,
                  'index'     =&gt; 'stores',
          ));
  
          if (Mage::helper('catalog')-&gt;isModuleEnabled('Mage_Rss')) {
              $this-&gt;addRssList('rss/catalog/notifystock', Mage::helper('catalog')-&gt;__('Notify Low Stock RSS'));
          }
  
          return parent::_prepareColumns();
      }
  
      protected function _prepareMassaction()
      {
          $this-&gt;setMassactionIdField('entity_id');
          $this-&gt;getMassactionBlock()-&gt;setFormFieldName('product');
  
          $this-&gt;getMassactionBlock()-&gt;addItem('delete', array(
               'label'=&gt; Mage::helper('catalog')-&gt;__('Delete'),
               'url'  =&gt; $this-&gt;getUrl('*/*/massDelete'),
               'confirm' =&gt; Mage::helper('catalog')-&gt;__('Are you sure?')
          ));
  
          $statuses = Mage::getSingleton('catalog/product_status')-&gt;getOptionArray();
  
          array_unshift($statuses, array('label'=&gt;'', 'value'=&gt;''));
          $this-&gt;getMassactionBlock()-&gt;addItem('status', array(
               'label'=&gt; Mage::helper('catalog')-&gt;__('Change status'),
               'url'  =&gt; $this-&gt;getUrl('*/*/massStatus', array('_current'=&gt;true)),
               'additional' =&gt; array(
                      'visibility' =&gt; array(
                           'name' =&gt; 'status',
                           'type' =&gt; 'select',
                           'class' =&gt; 'required-entry',
                           'label' =&gt; Mage::helper('catalog')-&gt;__('Status'),
                           'values' =&gt; $statuses
                       )
               )
          ));
  
          if (Mage::getSingleton('admin/session')-&gt;isAllowed('catalog/update_attributes')){
              $this-&gt;getMassactionBlock()-&gt;addItem('attributes', array(
                  'label' =&gt; Mage::helper('catalog')-&gt;__('Update Attributes'),
                  'url'   =&gt; $this-&gt;getUrl('*/catalog_product_action_attribute/edit', array('_current'=&gt;true))
              ));
          }
  
          Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' =&gt; $this));
          return $this;
      }
  
      public function getGridUrl()
      {
          return $this-&gt;getUrl('*/*/grid', array('_current'=&gt;true));
      }
  
      public function getRowUrl($row)
      {
          return $this-&gt;getUrl('*/*/edit', array(
              'store'=&gt;$this-&gt;getRequest()-&gt;getParam('store'),
              'id'=&gt;$row-&gt;getId())
          );
      }
  }
  </code></pre>
  
  <p>The custom code i've added in the Grid.php is this:</p>
  
  <pre><code>    $this-&gt;addColumn('number',
          array(
              'header'=&gt; Mage::helper('catalog')-&gt;__('Поръчка №'),
              'width' =&gt; '50px',
              'index' =&gt; 'entity_id',
              'renderer'  =&gt; 'Mage_Adminhtml_Block_Catalog_Product_Renderer',
      ));
  </code></pre>
  
  <p>I have created and rendered also, i saw this in another answer:</p>
  
  <pre><code>&lt;?PHP
  class Mage_Adminhtml_Block_Catalog_Product_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
  {
        public function render(Varien_Object $row)
        {
  
          $productId = $row-&gt;getData($this-&gt;getColumn()-&gt;getIndex());
              $orders = array();
              $collection = Mage::getResourceModel('sales/order_item_collection')
                  -&gt;addAttributeToFilter('product_id', array('eq' =&gt; $productId))
                  -&gt;load();
              foreach($collection as $orderItem) {
                  $orders[$orderItem-&gt;getOrder()-&gt;getIncrementId()] = $orderItem-&gt;getOrder();
              }
  
              $first_key = key($orders);  
              return $first_key;
  
  
        }
  }
  </code></pre>
  
  <p>I have added the additional column so i can display in every row in which order ID the product is purchased. I have no problems. Everything is correct, but the problem comes when i try to search with this custom column.</p>
  
  <p>The order ID is displayed correctly in the column but can not be searched by order ID.</p>
  
  <p>Where is my mistake, why it is not working and how can i fix it?</p>
  
  <p>Thanks in advance!</p>