1

I'm new in magento2 and I need your help.

In my InstallData.php, I created lot of product attributes with the following script :

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'my_attribute',
        [
            'type' => 'varchar',
            'backend' => '',
            'frontend' => '',
            'label' => 'My Attribute',
            'input' => 'select',
            'option' => [
                'values' => [],
            ],
            'class' => '',
            'source' => 'Project\Catalog\Model\Config\Source\Options',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '0',
            'searchable' => true,
            'filterable' => true,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => '',
            'group' => 'Custom Attributes'
        ]
    );

In parallel, I create attributes set with this script :

$setup->startSetup();
    $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

    $attributeSet = $this->attributeSetFactory->create();
    $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);

    $attributeSetNameArray = array();

    foreach ($attributeSetNameArray as $attributeSetName) {
        $data = [
            'attribute_set_name' => $attributeSetName,
            'entity_type_id' => $entityTypeId
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId);
        $attributeSet->save();
    }

    $setup->endSetup();

And now, I want to link attributes to attributes set. But an attribute can be link to several attributes set.

For exemple :

List of my attributes :

  • Attr1
  • Attr2
  • Attr3
  • Attr4
  • Attr5

List of my attributes set :

  • Default (the default attribute set)
  • Attr_set1
  • Attr_set2

And I want :

  • In Default, all my attributes
  • In Attr_set1, the attributes Attr1, Attr2, Attr3
  • In Attr_set2, the attributes Attr1, Attr4

I want to make the links programmatically (not via the Back office). Someone have an idea ?

2
  • You can use this: 'attribute_set_id' => 'YOUR_ATTRIBUTE_SET_NAME', while creating attribute via script.
    – Sejal Shah
    Feb 18, 2019 at 12:16
  • My attributes must be in several attributes set, so how can I do that ? I can't put an array in 'attribute_set_id' when I create my attribute...
    – Kozame
    Feb 18, 2019 at 12:26

0

Your Answer

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

Browse other questions tagged or ask your own question.