Question

I asked a question here about this just over a week ago, but it took a little while to edit the post to include some code.

The original post and code can be seen here

I am able to create an Attribute Set based upon Default, create the Product Attribute and add this to the Product Details Group when either installing or upgrading a module.

My problem however is that Product Attribute is added to ALL attribute sets and not just the new attribute set that was created. Looking in the MySQL tables I can see that my new Product Attribute is indeed linked to every Attribute Set.

Does anyone know how to stop the Product Attribute being added to all of the Attribute Sets EXCEPT the one that has just been created? From what I have read, this should happen from using the 'attribute_set_id' while creating the Product Attribute, but this has not worked?

Was it helpful?

Solution

If you don't want a attribute to be added to all sets you will have to call explicitly the method addAttributeToSet which adds the attribute to an attribute set and a attribute group. It is imho a litlle ilogic what happens in the EavSetup method addAttribute() but Magento's developer might have a reason for doing so :-) If you add a parameter group the method addAttribute() fetches all attribute sets for the given entity_type_id and adds the attribute to them without taking care of your givenattribute_set_id.

So the solution is the following (it worked for me in Magento 1 and as far as I know there were no major changes at this point):

  1. create the attribute in the way you have done, but don't use key 'group' in your attribute's parameter in method addAttribute()!

  2. add the following code in your setup script after addAttribute():

    $attributeCode = 'your_attribute_code';
    $attributeSetName = 'Your Attribute Set Name or ID';
    $groupName = 'Your Group Name or ID';
    //$entityTypeId is already defined in your setup script otherwise you nedd to set that variable too
    $attributeId = $eavSetup->getAttributeId($entityTypeId, $attributeCode);
    $attributeSetId = $eavSetup->getAttributeSetId($entityTypeId, $attributeSetName);
    $attributeGroupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);
    $eavSetup->addAttributeToSet($entityTypeId,$attributeSetId, $attributeGroupId, $attributeId);
    

I hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top