I have a custom column in my catalog product grid for a custom attribute. I'm using the observer method of inserting the column: on core_block_abstract_prepare_layout_before:
public function appendAuthorColumn(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if ($block->getType() == 'adminhtml/catalog_product_grid') {
$authors = Mage::helper('companyx_authors')->getAuthorOptionHash();
$block->addColumnAfter('author_id', array(
'header' => 'Author',
'type' => 'options',
'index' => 'author_id',
'options' => $authors,
'width' => '150',
), 'name');
}
}
I am updating the collection using the catalog_product_collection_load_before event:
public function addAuthorToCollection(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if (!isset($collection)) {
return $this;
}
$collection->addAttributeToSelect('author_id');
return $this;
}
Everything works as expected, except when I want to filter by product name: the author column is blank. When I filter by any other column, it is populated.
The only difference with product name is that it's a free text field. I can't think of anything else.
1 Answer 1
Instead of adding the attribute to the collection, join it:
public function addAuthorToCollection(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if (!isset($collection)) {
return $this;
}
$collection->joinAttribute('author_id', 'catalog_product/author_id', 'entity_id', null, 'left'); //use this instead
return $this;
}