Smile-SA / magento2-module-product-label

Module to handle custom labels with images on products
Open Software License 3.0
54 stars 36 forks source link

Load only attributes used for rules in the plugin #10

Closed romainruaud closed 5 years ago

romainruaud commented 5 years ago

The Plugin\Catalog\Model\Config is a good start.

However, you could :

A meta code (untested) :

    /**
     * Retrieve Attributes used in product listing
     *
     * @return array
     */
    public function afterGetAttributesUsedInProductListing(\Magento\Catalog\Model\Config $subject, $result)
    {
        if ($this->_usedInProductListing === null) {
            $this->_usedInProductListing = $result;
            $entityType = \Magento\Catalog\Model\Product::ENTITY;

            /** @var Smile\ProductLabel\Model\ResourceModel\ProductLabel\CollectionFactory */
            $productLabelsCollection = $productLabelCollectionFactory->create();
            // Here you have all the attribute ids that are used to build product label rules.
            $attributeIds = $productLabelsCollection->getAllAttributeIds();

            // Filter the collection on these attributes only.
            $attributesDataExtra = $this->_attributeFactory->getCollection()->addIdFilter($attributeIds)->getData();
            $this->_eavConfig->importAttributesData($entityType, $attributesDataExtra);
            foreach ($attributesDataExtra as $attributeData) {
                $attributeCode = $attributeData['attribute_code'];
                $this->_usedInProductListing[$attributeCode] = $this->_eavConfig->getAttribute(
                    $entityType,
                    $attributeCode
                );
            }
        }

        return $this->_usedInProductListing;
    }
houdaElr commented 5 years ago

Done!