5

I am trying to create a custom attribute and add it to a custom created attribute set. The problem is that it is being added to the Default attribute set. How can I make it only appear in my attribute set? Below is the code I am using in the upgrade script.

/* @var Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.2', '<')) {
 $data = array (
 'type' => 'string',
 'label' => 'Designer',
 'input' => 'text',
 'required' => false,
 'sort_order' => 3,
 'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
 'group' => 'Product Details',
 'used_in_product_listing' => true,
 'visible_on_front' => true,
 );
 $attributeSet = 'Test-Attribute-Set';
 $entityTypeId = $this->coreRegistry->registry('entityType');
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'designer',
 $data
 );
 $eavSetup->addAttributeToSet(
 $entityTypeId,
 $attributeSet,
 'Product Details',
 'designer'
 );
}
asked Sep 26, 2016 at 8:21
3
  • Refer magento.stackexchange.com/questions/46358/… or magento.stackexchange.com/questions/16868/… Commented Sep 26, 2016 at 8:24
  • So if I add an attribute to a group it is automatically added to all attribute sets that have that group? @AnkitShah Commented Sep 26, 2016 at 8:28
  • @AnkitShah both of those links refer to Magento 1 solutions — I don't know if they're applicable to Magento 2. Commented Jul 11, 2017 at 21:41

3 Answers 3

2

Please use the following code. Detailed explanation click here. http://www.pearlbells.co.uk/delete-attribute-options-magento-2/

$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$id = $attribute->getAttributeId();
$options = $attribute->getSource()->getAllOptions();
foreach ($options as $option) {
echo 'Delete label : '.$option['label'].PHP_EOL; 
$options['delete'][$option['value']] = true; 
$options['value'][$option['value']] = true;
}
$eavSetup = $object_Manager->get('\Magento\Eav\Setup\EavSetup');
$eavSetup->addAttributeOption($options);
answered Aug 19, 2017 at 22:30
1
  • This worked fine the only thing I changed was getting the config through the constructor Commented Oct 3, 2017 at 13:09
1

How to remove an attribute from attribute sets

So, you've accidentally added an attribute to all attribute sets?

You can remove it from the unwanted attribute sets, without deleting the attribute, and keep it in the attribute set you want.
You can also remove the unwanted group at the same time.

In your UpgradeData script:

// Remove the attribute from all attribute sets except 1:
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$attributeSetIdToKeep = $eavSetup->getAttributeSetId(
 \Magento\Catalog\Model\Product::ENTITY,
 'Test-Attribute-Set' // Attribute set name
);
$attributeId = $eavSetup->getAttributeId(
 \Magento\Catalog\Model\Product::ENTITY,
 'designer' // Attribute code
);
/** @var \Magento\Framework\Setup\ModuleDataSetupInterface $setup */
$setup->getConnection()->delete(
 $setup->getTable('eav_entity_attribute'),
 [
 'attribute_id = ?' => $attributeId,
 'attribute_set_id != ?' => $attributeSetIdToKeep,
 ]
);
// Remove the group from all attribute sets except 1:
$attributeSetIds = $eavSetup->getAllAttributeSetIds();
foreach ($attributeSetIds as $attributeSetId) {
 if ($attributeSetId != $attributeSetIdToKeep) {
 $eavSetup->removeAttributeGroup(
 \Magento\Catalog\Model\Product::ENTITY,
 $attributeSetId,
 'Product Details' // Group name
 );
 }
}
answered Jun 6, 2019 at 15:33
0

How to create a product attribute and add it to only 1 attribute set

When you create a product attribute in Magento 2, you have to ensure 2 things in the array you give to addAttribute():

  • Do not specify a group key. We'll see later how to add the attribute to the group.
  • You have to specify 'user_defined' => true,

Otherwise, Magento will add the attribute, and the group, to all of the attribute sets, as you can see in the code.

So, the call to addAttribute() might look like:

$eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'designer', // Attribute code
 [
 'type' => 'string',
 'label' => 'Designer',
 'input' => 'text',
 'required' => false,
 'sort_order' => 3,
 'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
 'used_in_product_listing' => true,
 'visible_on_front' => true,
 'user_defined' => true,
 ]
);

Which will create the attribute, without adding it to any attribute set (or any group).

Now, in order to add it to the attribute set you want, and in the group you want:

$eavSetup->addAttributeToGroup(
 \Magento\Catalog\Model\Product::ENTITY,
 'Test-Attribute-Set', // Name of the attribute set
 'Product Details', // Name of the group
 'designer' // Attribute code
);

Right now, I'm not sure about the difference between addAttributeToGroup() and addAttributeToSet().

answered Jun 6, 2019 at 15:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.