I have two attribute sets in my store: 'default' and 'clothing'. The only difference is that clothing set has an extra attribute called "Fit".

I added a custom section in product pages to display the "Fit" attribute:

$fitLabel = $_product->getResource()->getAttribute('fit')->getFrontendLabel();
$fitValue = $_product->getResource()->getAttribute('fit')->getFrontend()->getValue($_product);

However, "Fit" attribute appears in ALL product pages, not only clothing.

How can I add a condition to display the Fit attribute only in clothing products (products with 'clothing' attribute set)?

  • I think you can achieve it through template file or through observers or plugin to remove the fit attribute for those attributes set in which you does not want to show it. – Abbas Jan 31 '17 at 1:12

Modify your Setup/InstallData.php

namespace ProjectName\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;

class InstallData implements InstallDataInterface
    protected $EavSetupFactory;
    protected $productResource;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        ProductResource $productResource
    ) {

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        // First, set these keys to your attribute to avoid adding attribute to all attribute sets.
                'user_defined' => true,
                'group'        => '',

        // Then add attribute to your attribute set.
        $entityTypeId = $this
        $attributeId = $eavSetup->getAttributeId($entityTypeId, 'fit');
        $attributeSetId = $eavSetup->getAttributeSetId($entityTypeId, 'YOUR_ATTRIBUTE_SET_CODE');
        $eavSetup->addAttributeToSet($entityTypeId, $attributeSetId, 'GROUP_CODE_IN_PRODUCT_PAGE', $attributeId);
| improve this answer | |

This is true for Magento 2 as well as for Magento 1: By default, a new attribute gets added to all attribute sets.

To prevent this, set user_defined to true, and leave empty the group.

 'user_defined' => true,
 'group'        => '',

Then, you add the attribute to the desired attribute set.

$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $attributeSetId = $eavSetup->getAttributeSetId($entityTypeId, 'ABC');
    $groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'General');
    $attribute = $eavSetup->getAttribute($entityTypeId, 'XYZ');
    $eavSetup->getAttributeSetId($entityTypeId, 'ABC');
| improve this answer | |

if you load the attribute like this, I think you might be levering out the product attribute loading mechanism, that should only load the attributes defined in the attribute set and as load on catalog pages.

If you just output the attribute with $_product->getData('fit') (or if it's a select field, $_product->getAttributeText('fit')), it should only be loaded, if added to the attribute set and marked as load on product pages in the attribute settings.

If this does not work, maybe all product attributes marked as load on product pages (disregarding the attribute set) might get loaded.

In this case, you could add a condition on $_product->getAttributeSetId(). (In this case you might want to add a configuration, on which attribute sets this attribute should be displayed in your etc/adminhtml/system.xml and read the configuration in your block)

| improve this answer | |
  • In order to load the attributes in a different tab in product page, I had to load them like that. (magento.stackexchange.com/questions/120725/…) Do you know how to move attributes between tabs without changing the default loading behaviour? If that is not possible, I would like to know how exactly how to set the condition to load them according to the attribute set – Luis Garcia Jan 25 '17 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.