Magento – Map attribute set based on Default attribute set programatically in magento 2

attribute-setmagento2

I have below script for creating attribute set, sets are created, but none of the attributes are assigned to based on the Default.

How do i map the attributes sets to have the default attributes?

app\code\Custom\AttributeSet\Setup\UpgradeData.php

$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
    if(version_compare($context->getVersion(), '1.1', '<'))
    {
        $attributeSet = $this->attributeSetFactory->create();
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
        $datas = [
            [
                'attribute_set_name' => 'Test Set10', 
                'entity_type_id' => $entityTypeId,
                'sort_order' => 200,
            ],
            [
                'attribute_set_name' => 'Test Set20', 
                'entity_type_id' => $entityTypeId,
                'sort_order' => 200,
            ],
            [
                'attribute_set_name' => 'Test Set30', 
                'entity_type_id' => $entityTypeId,
                'sort_order' => 200,
            ]
        ];

        foreach ($datas as $data) {
            $attributeSet = $this->attributeSetFactory->create();
            $attributeSet->setData($data);
            $attributeSet->validate();              
            $attributeSet->initFromSkeleton($attributeSetId);
            $attributeSet->save(); 
        }
    }
}

Best Answer

You can try this code to apply default attribute set to custom attribute. It's working fine for me.

class UpgradeData implements UpgradeDataInterface
{

/**
 * @var \Magento\Catalog\Model\Product\Attribute\Management
 */
private $attributeManagement;

/**
 * @var AttributeSetFactory
 */
protected $attributeSetFactory;

/**
 * @var CategorySetupFactory
 */
protected $categorySetupFactory;

/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory,
    \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory,
    \Magento\Catalog\Model\Product\Attribute\Management $attributeManagement
) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->categorySetupFactory = $categorySetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->attributeManagement = $attributeManagement;
}



/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    if (version_compare($context->getVersion(), '1.0.1') < 0) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);


        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        /**
         *checking the current version of the module 
         *this function is implemented from  ModuleContextInterface
        */

        $attributeSet = $this->attributeSetFactory->create();
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
        $data = [
        'attribute_set_name' => 'Attribute set name',
        'entity_type_id' => $entityTypeId,
        'sort_order' => 200,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId);
        $attributeSet->save();        
        $AttributeSetId = $attributeSet->getId();

        $groupName = 'Attribute Group name';
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
        $categorySetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 100);
        $attributeGroupId = $categorySetup->getAttributeGroupId($entityTypeId, $AttributeSetId, $groupName);
}
}