Magento AddAttributeToSelect() on custom added attribute (observer)

10,207

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.

Share:
10,207
Hosh Sadiq
Author by

Hosh Sadiq

Updated on June 06, 2022

Comments

  • Hosh Sadiq
    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 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?

    • Nasaralla
      Nasaralla over 12 years
      admin_id is an array? ... as it is in your config?
    • Hosh Sadiq
      Hosh Sadiq over 12 years
      No, admin_id is an int, it's a Magento EAV attribute which I've added to catalog_product.
    • Hosh Sadiq
      Hosh Sadiq over 12 years
      Not 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
    Hosh Sadiq over 12 years
    My 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!