use the result of the precedent call in your function instead of duplicating it
only add the attributes that are used for labels. With your actual solution, you are adding all attributes on all listing pages even if they are not used to create rules.
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;
}
The
Plugin\Catalog\Model\Config
is a good start.However, you could :
A meta code (untested) :