3

How to assign particular attribute set to newly created attribute?

My code is

    eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'test_demo221',
        [

     //   'group' => 'Bag',
        'attribute_set_id' => 16,
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Test Attribute',
            'input' => '',
            'class' => '',
            'source' => '',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => 0,
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );

It creates the attribute but appear in all attribute set. What should I have to do if I want to add to particular attribute set only?

3
  • Above code adding attribute to all attribute sets and not only to specified one. Please help me to do that. Commented Sep 4, 2018 at 6:45
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
    – 7ochem
    Commented Sep 4, 2018 at 7:04
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Commented Sep 4, 2018 at 7:13

5 Answers 5

2

Hey if you using Magento 2 then

add below function in your custom helper class

public function addAttributeToAllAttributeSets($attributeCode,$attributeGroupCode) {

        $entityType = $this->eavTypeFactory->create()->loadByCode('catalog_product');

        $attribute = $this->attributeFactory->create()->loadByCode($entityType->getId(), $attributeCode);

        if (!$attribute->getId()) {
            return false;
        }

        /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $setCollection */
        $setCollection = $this->attributeSetFactory->create()->getCollection();
        $setCollection->addFieldToFilter('entity_type_id', $entityType->getId());

        /** @var Set $attributeSet */
        foreach ($setCollection as $attributeSet) {
            /** @var Group $group */
            $group = $this->attributeGroupFactory->create()->getCollection()
                    ->addFieldToFilter('attribute_group_code', ['eq' => $attributeGroupCode])
                    ->addFieldToFilter('attribute_set_id', ['eq' => $attributeSet->getId()])
                    ->getFirstItem();

            $groupId = $group->getId() ? : $attributeSet->getDefaultGroupId();

            // Assign:
            $this->attributeManagement->assign(
                    'catalog_product', $attributeSet->getId(), $groupId, $attributeCode, $attributeSet->getCollection()->count() * 10
            );
        }

        return true;
    }

and for this construct function to create objects will be

public function __construct(
            AttributeFactory $attributeFactory,
            SetFactory $attributeSetFactory,
            CatalogtransporterFactory $catalogTransporterFactory,
            GroupFactory $attributeGroupFactory,
            TypeFactory $typeFactory,
            AttributeManagement $attributeManagement,
            Context $context,
            \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\ResourceModel\Eav\Attribute $cattributeFactory,
            \Magento\Eav\Model\Config $eavConfig,
            \Magento\Eav\Model\AttributeRepository $attributeRepository,
            \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement,
            \Magento\Eav\Api\Data\AttributeOptionLabelInterface $attributeOptionLabel,
            \Magento\Eav\Model\Entity\Attribute\Option $option
    ) {
        $this->attributeFactory = $attributeFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->catalogTransporterFactory = $catalogTransporterFactory;
        $this->eavTypeFactory = $typeFactory;
        $this->attributeGroupFactory = $attributeGroupFactory;
        $this->attributeManagement = $attributeManagement;
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_storeManager = $storeManager;
        $this->eavConfig = $eavConfig;
        $this->_cattributeFactory = $cattributeFactory;
        $this->_attributeRepository = $attributeRepository;
        $this->_attributeOptionManagement = $attributeOptionManagement;
        $this->_option = $option;
        $this->_attributeOptionLabel = $attributeOptionLabel;
        parent::__construct($context);
    }

And then call same function

 $helper = $this->_objectManager->create('\Companyname\Modulename\Helper\Data');
            $helper->addAttributeToAllAttributeSets('attribute_code','general');
1
  • The use imports would be helpfull
    – PvdL
    Commented Nov 28, 2018 at 10:17
2

Just change attribute_set_id with attribute_set. Like this:

eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'test_demo221',
    [

 //   'group' => 'Bag',
    'attribute_set' => '<yourattributesetname>',
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Test Attribute',
        'input' => '',
        'class' => '',
        'source' => '',
        'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => 0,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => ''
    ]
);
2
  • 1
    providing 'attribute_set' => 'Default', does not work for me Commented Mar 4, 2019 at 11:23
  • Thanks. How can we add this single attribute to multiple attribute set ? 'attribute_set' => ['set1','set2'], this is not working.
    – shreyas d
    Commented Feb 25, 2021 at 5:58
1

Simply change the below line

'attribute_set_id' => 16,

to

'attribute_set' => 'AttributeSetName',

Now your attribute get assigned to above mentioned attribute name.

1
  • so, can we provide 'attribute_set' => 'Default, to assign to default attribute set Commented Mar 4, 2019 at 10:18
0

create and assign to custom attribute set :

$AttributeInterface->setAttributeCode($currentAttributeCode);
$AttributeInterface->setDefaultFrontendLabel($currentAttributeLabel); 
$AttributeInterface->setIsRequired(false);
$AttributeInterface->setIsUserDefined(true);
$AttributeInterface->setBackendType('varchar');
$AttributeInterface->setFrontendInput('text');
$AttributeInterface->setEntityTypeId($attributeSet['defaultAttributeSetId']);
$AttributeRepos->save($AttributeInterface);
$createdAttributeId=$AttributeInterface->getAttributeId();
$AttributeManage->assign('catalog_product',(int)$attributeSet['AttributeSetId'], (int)$groupId,$currentAttributeCode,++$attributeSetCounter);
0

This solution worked for me.

eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'test_demo221',
        [
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Test Attribute',
            'input' => '',
            'class' => '',
            'source' => '',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => 0,
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );

    $ATTRIBUTE_GROUP = 'General';
    $attribute_set_id = '';
    $attribute_set_name = 'ENTER ATTRIBUTE SET NAME HERE';
    $attributeSetList = $this->getAttributeSetList($attribute_set_name);
    foreach ($attributeSetList as $attributeSet) {
         $attribute_set_id = $attributeSet['attribute_set_id'];
    }
    $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attribute_set_id, $ATTRIBUTE_GROUP);

    $eavSetup->addAttributeToGroup(
     $entityTypeId,
     $attribute_set_id,
     $groupId,
     'test_demo221', //attribute_code
     null
     );

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.