0

We want to load the product attribute value on the category page, of all attributes with a value "Yes" of label "is_featured". We create the following code in the list.phtml, but this really slows down the category list page.

Is there a better/faster way to load these product values?

<?php $attributes = $_product->getAttributes();
foreach($attributes as $a)
{
    if ($a->getData('is_featured') && $a->getFrontend()->getValue($_product) != NULL){
    echo '<li><span class="">' . $a->getFrontend()->getValue($_product). '</span></li>';
    }
}
?>

1 Answer 1

1

Following best practice we have 2 ideal options. A Before Plugin or a Preference Override.

Path of Core file: vendor/magento/module-catalog/Model/Product/Type/AbstractType.php

Function to modify: public function getSetAttributes($product)

Examples:

Before Plugin

    public function beforeGetSetAttributes(
        AbstractType $subject,
        Product $product
    ) {
        // Check if custom logic should be applied
        if (!$product->hasData('_cache_instance_product_set_attributes')) {
            // Load only featured attributes for the product's attribute set ID
            $attributeCollection = $this->attributeCollectionFactory->create()
                ->setEntityTypeFilter($product->getResource()->getTypeId())
                ->addFieldToFilter('attribute_set_id', $product->getAttributeSetId())
                ->addFieldToFilter('is_featured', 1)
                ->setOrder('attribute_id', 'ASC');

            

            // Cache the filtered attributes
            $product->setData('_cache_instance_product_set_attributes', $attributeCollection->load());
        }
    }

Preference Override:

use Magento\Catalog\Model\Product\Type\AbstractType as MagentoAbstractType;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory as AttributeCollectionFactory;

class CustomType extends MagentoAbstractType
{
    protected $attributeCollectionFactory;

    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Framework\Module\Manager $moduleManager,
        AttributeCollectionFactory $attributeCollectionFactory
    ) {
        parent::__construct($productFactory, $eventManager, $eavConfig, $moduleManager);
        $this->attributeCollectionFactory = $attributeCollectionFactory;
    }

    public function getSetAttributes($product)
    {
        // Check if custom logic should be applied
        if (!$product->hasData('_cache_instance_product_set_attributes')) {
            // Load all attributes for the product's attribute set ID
            $attributeCollection = $this->attributeCollectionFactory->create()
                ->setEntityTypeFilter($product->getResource()->getTypeId())
                ->addFieldToFilter('attribute_set_id', $product->getAttributeSetId())
                ->addFieldToFilter('is_featured', 1)
                ->setOrder('attribute_id', 'ASC');

            $product->setData('_cache_instance_product_set_attributes', $setAttributes);
        }
        return $product->getData($this->_cacheProductSetAttributes);


    }

If this is a universal change (loading of any type of attribute collection) then you're better off doing a preference override here: enter image description here

Path vendor/magento/module-eav/Model/Config.php

Function protected function _initAttributes($entityType)

4
  • 1
    Thanks! Maybe my question was not clear enough, but we do not have an attribute with code "is_featured", but we have option Yes/No with "is_featured" inside a attribute itself, that we check to see if the specific attribute is set as featured. So we only want to load attributes that are "is_featured" and then we want to get the getAttributeText of that specific attribute. That's also what our current code does. How can we achieve that?
    – JGeer
    Commented Jun 12 at 18:16
  • @JGeer Answer has been updated.
    – Infinri
    Commented Jun 12 at 19:45
  • Thanks! How do we load the right code then inside the list.phtml template?
    – JGeer
    Commented Jun 13 at 7:02
  • 1
    The same way you are now. With this solution you can filter your attributes by the "is_featured" and i'm sure you can also filter your products to omit any products that dont have a label/title value. which means now you just loop through your attributes and echo the frontend value.
    – Infinri
    Commented Jun 13 at 18:28

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.