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'
    );
}
3

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);
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
        );
    }
}
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().

Your Answer

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

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