Reputation: 227
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.
Upvotes: 3
Views: 8447
Reputation: 7193
I had to add the following to _prepareCollection
:
$collection->joinAttribute(
'custom_column',
'catalog_product/custom_column',
'entity_id',
null,
'inner',
$store->getId()
);
Upvotes: 2
Reputation: 2949
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;
}
Upvotes: 0