3

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(); 
        }
    }
}
  • I know you are trying to create attribute set and assign attribute programmaitcally but did you try this from admin? – Jai Mar 15 '17 at 4:41
  • its script which executed after running setup: upgrade command, what u mean by try from admin? – Sushivam Mar 15 '17 at 6:34
  • I apologizes for above comment. I was having issue of creating attributes from admin.magento.stackexchange.com/q/163795/29175. Thats because of server setting. Now its working. – Jai Mar 15 '17 at 7:45
2

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);
}
}
0

You can use AttributeSetManagementInterface to model your attribute set off of an existing one. You create an attribute set using the data API with just the information specific to your set, and then use AttributeSetManagementInterface::create() with the attribute set ID of an attribute set you want to "clone". Here I'm using the default attribute set:

<?php

namespace Vendor\Module\Setup;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Api\AttributeSetManagementInterface;
use Magento\Eav\Api\Data\AttributeSetInterfaceFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{

    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * @var AttributeSetInterfaceFactory
     */
    private $attributeSetInterfaceFactory;

    /**
     * @var AttributeSetManagementInterface
     */
    private $attributeSetManagement;

    /**
     * @var EavSetup
     */
    private $eavSetup;


    /**
     * Create new attribute set.
     *
     * @param AttributeSetInterfaceFactory $attributeSetInterfaceFactory
     * @param AttributeSetManagementInterface $attributeSetManagement
     * @param EavSetup $eavSetup
     * @param Config $eavConfig
     */
    public function __construct(
        AttributeSetInterfaceFactory $attributeSetInterfaceFactory,
        AttributeSetManagementInterface $attributeSetManagement,
        EavSetup $eavSetup,
        Config $eavConfig
    ) {

        $this->eavConfig = $eavConfig;
        $this->attributeSetInterfaceFactory = $attributeSetInterfaceFactory;
        $this->attributeSetManagement = $attributeSetManagement;
        $this->eavSetup = $eavSetup;
    }


    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $productEntityId = $this->eavConfig->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE)->getId();
        $defaultAttributeSetId = $this->eavSetup->getDefaultAttributeSetId($productEntityId);

        /** @var \Magento\Eav\Api\Data\AttributeSetInterface $attrSet */
        $attrSet = $this->attributeSetInterfaceFactory->create();
        $attrSet
            ->setAttributeSetName('Your Attribute Set Name')
            ->setEntityTypeId($productEntityId);
        $this->attributeSetManagement->create(
            ProductAttributeInterface::ENTITY_TYPE_CODE, $attrSet, $defaultAttributeSetId);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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