3
votes

I've added a custom attribute to the products grid in the admin area using the following code in /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php under function _prepareColumns()

It works fine, but now when searching with any search filter - the new attribute column is showing no values.

$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','custom_column');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
$attributeOptions = $attribute->getSource()->getAllOptions();

$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
    if(!empty($value['value'])) {
        $attributeOptions2[$value['value']] = $value['label'];
    }
}

$this->addColumn('custom_column',
    array(
        'header'=> Mage::helper('catalog')->__('Custom Column'),
        'width' => '150px',
        'index' => 'custom_column',
        'type'  => 'options',
        'options' => $attributeOptions2,
));

And under _prepareCollection() I've added the following code:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('custom_column');

I think it's simple but I'm not catching it at the moment, any help is highly appreciated!

EDIT:

When searching with filters - the column is populating with values EXCEPT if the filter was the Name column.

2
Didn't you try with manage attribute in catalog? there you can simply set "Use in Quick Search" field as yes! - DRAJI
Give this extension a try: github.com/tzyganu/GridEnhancer. It's free and it allows you to manage a lot of admin grids without writing any code. - Marius
@Marius, your extension looks very good, I've inspected the source code and tested it but unfortunately it didn't resolve the issue. In fact, my code above is showing values when filtering with any column except the Name column, but your extension is not showing any values for the new custom column at all. - Ethan Kawkji
Did you solved this issue? Please post the answer. I am in the same trouble - Pavan Kumar

2 Answers

2
votes

I had to add the following to _prepareCollection:

        $collection->joinAttribute(
            'custom_column',
            'catalog_product/custom_column',
            'entity_id',
            null,
            'inner',
            $store->getId()
        );
0
votes

Try to add custom filter callback

$this->addColumn('custom_column',
  array(
    'header'=> Mage::helper('catalog')->__('Custom Column'),
    'width' => '150px',
    'index' => 'custom_column',
    'type'  => 'options',
    'options' => $attributeOptions2,
    'filter_condition_callback' => array($this, 'filter_custom_column_callback'),
));

And define your filter query there, like in this example:

protected function filter_custom_column_callback($collection, $column)
{
    $filterValue = $column->getFilter()->getValue();
    $collection->getSelect()->where(" ... ");
    return $this;
}