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.