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:
Path vendor/magento/module-eav/Model/Config.php
Function protected function _initAttributes($entityType)