Magento AddAttributeToSelect() on custom added attribute (observer)
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.
Hosh Sadiq
Updated on June 06, 2022Comments
-
Hosh Sadiq almost 2 years
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 theforeach()
loop in the observer method, it returnsNULL
rather than the actualadmin_id
it should return.Any ideas on why its not working?
-
Nasaralla over 12 yearsadmin_id is an array? ... as it is in your config?
-
Hosh Sadiq over 12 yearsNo, admin_id is an
int
, it's a Magento EAV attribute which I've added to catalog_product. -
Hosh Sadiq over 12 yearsNot that I know of, it's been added as an
int
I think (hence why type = int in the array), I'm quite new to magento development, so correct me if I'm wrong, I was following this tutorial: magentocommerce.com/wiki/5_-_modules_and_development/catalog/…
-
-
Hosh Sadiq over 12 yearsMy bad, I forgot to mention that this is only for the admin backend (manage products), but because I had used after instead of before, it wouldn't show it. Works absolutely fine now! Thanks very much!