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

Do not iterate on all product attributes. #7

Closed romainruaud closed 5 years ago

romainruaud commented 5 years ago

But just on attributes that might be used for label usage.

On this function : https://github.com/Smile-SA/magento2-module-product-label/blob/frontend/Block/ProductLabel/ProductLabel.php#L156

Maybe with something like this (macro code, I did not test) :

/** @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();
// You should be able to iterate on these Ids.

$productEntity = $this->getCurrentProduct()->getResourceCollection()->getEntity();
foreach ($attributeIds as $attributeId) {
   $attribute = $entity->getAttribute($attributeId);
   if ($attribute) {
       // then do the stuff you were doing previously :
       $optionIds = $this->getCurrentProduct()->getCustomAttribute($attribute->getAttributeCode());

        $attributesList[$attribute->getId()] = [
        'id' => $attribute->getId(),
        'label' => $attribute->getFrontend()->getLabel(),
        'options' => ($optionIds) ? $optionIds->getValue() : ''
        ];
   }
}
romainruaud commented 5 years ago

Ok for me on your last version !