0

I want to get multiple select type custom attribute values in product collection observer, I have tried with some events catalog_block_product_list_collection, catalog_product_collection_apply_limitations_after or catalog_product_collection_load_after

Observer look like as below:

public function execute(\Magento\Framework\Event\Observer $observer)
        {    
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/plugin_for_product_collection.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
 
            $collection = $observer->getEvent()->getData('collection');
            foreach ($collection as $item) {
                $logger->info('item: ', [$item->getName()]);
                $logger->info('item->getCustomerIds(): ', [$item->getCustomerIds()]);
                $logger->info('item->getCustomAttribute: ', [$item->getCustomAttribute('customer_ids')]);
                $logger->info('item->getCustomerIds: ', [$item->getData('customer_ids')]);
                $logger->info('item->getAllAttributeValues(customer_ids): ', [$item->getAllAttributeValues('customer_ids')]);
            }

            return $this;
        }

Result are showing as bellow:

– 2022-02-04T21:44:28+00:00 INFO (6): item:  ["Product Name showing perfect"]
– 2022-02-04T21:44:28+00:00 INFO (6): item->getCustomerGroup():  [null]
– 2022-02-04T21:44:28+00:00 INFO (6): item->getCustomAttribute:  [null]
– 2022-02-04T21:44:28+00:00 INFO (6): item->getCustomerGroup:  [null]
– 2022-02-04T21:44:28+00:00 INFO (6): item->getAllAttributeValues(customer_group):  [null]

Yet when I use $item->getData() showing product data but not showing custom attribute value. Am I missing something else? or am I using the wrong event to list the products? Any help would be really appreciated.

THANKS.

1
  • Why do you want to use event observer? Can you explain on which page you want your custom attributes values?
    – Ankit
    Commented Feb 5, 2022 at 6:13

1 Answer 1

0

Use This Observer : catalog_product_collection_load_after

and Try to select the attribute before foreach in the collection.

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/plugin_for_product_collection.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $collection = $observer->getEvent()->getData('collection');
        $collection->addAttributeToSelect(['custom_attribute_code_1','custom_attribute_code_2']);
        foreach ($collection as $item) {
            $logger->info('item: ', [$item->getName()]);
            $logger->info('item->getCustomerIds(): ', [$item->getCustomerIds()]);
            $logger->info('item->getCustomAttribute: ', [$item->getCustomAttribute('customer_ids')]);
            $logger->info('item->getCustomerIds: ', [$item->getData('customer_ids')]);
            $logger->info('item->getAllAttributeValues(customer_ids): ', [$item->getAllAttributeValues('customer_ids')]);
        }

        return $this;
    }
1
  • Great, I have tried to select the attribute before foreach working fine. Thank you! Commented Feb 5, 2022 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.