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'
);
}
-
Refer magento.stackexchange.com/questions/46358/… or magento.stackexchange.com/questions/16868/…Jackson– Jackson2016年09月26日 08:24:31 +00:00Commented 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? @AnkitShahCharbel Wakim– Charbel Wakim2016年09月26日 08:28:14 +00:00Commented 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.Mark Fox– Mark Fox2017年07月11日 21:41:43 +00:00Commented Jul 11, 2017 at 21:41
3 Answers 3
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);
-
This worked fine the only thing I changed was getting the config through the constructorCharbel Wakim– Charbel Wakim2017年10月03日 13:09:58 +00:00Commented Oct 3, 2017 at 13:09
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
);
}
}
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
groupkey. 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().
Explore related questions
See similar questions with these tags.