I have set up an observer on catalog_product_collection_load_after
and the following code is called:
<?php
class Drench_Admindetails_Model_Observer {
public function loadAfter($observer){
$collection = $observer->getEvent()->getCollection();
$collection->addAttributeToFilter('admin_id', Mage::getSingleton('admin/session')->getUser()->getUserId());
foreach($collection as $item) {
fb($item->getAdminId()); //fb() is a firebug call
}
return $this;
}
}
As you can see, I am filtering the collection on admin_id
, which I created through the following setup script (namespace/module/Resource/Eav/Mysql4/Setup.php).
<?php
class Drench_Admindetails_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'catalog_product' => array (
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array (
'admin_id' => array (
'group' => '',
'label' => '',
'type' => 'int',
'input' => '',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
)
)
)
);
}
}
This attribute stores the admin that added the product.
However, the collection is not filtered on admin_id
, and in the foreach()
loop in the observer method, it returns NULL
rather than the actual admin_id
it should return.
Any ideas on why its not working?
You cannot filter a collection after it has been loaded. Use the event catalog_product_collection_load_before
instead. If you attempt to iterate the collection at this point it may call the same event and start an infinite recursion, which would be bad.
The admin_id
attribute probably won't be added to the selected columns for product lists unless the attribute has used_in_product_listing
set to true
. You might also be successful with using $collection->
addAttributeToSelect('admin_id')
in the same before load event.