Magento – Programatically add attribute sets Magento2

attribute-setmagento2

I need idea on how to create a script for adding bulk attribute sets and attributes in magento2?

This is the script i have tried:

AttributeSetupscript.php

<?php
 require __DIR__ . '/app/bootstrap.php';
 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
 /** @var \Magento\Framework\App\Http $app */
 $app = $bootstrap->createApplication('AttributeSet');
 $bootstrap->run($app);

AttributeSet.php

<?php
use \Magento\Framework\App\Bootstrap;
//include('../app/bootstrap.php');
use Magento\Catalog\Api\CategoryRepositoryInterface;
class AttributeSet extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
protected $_moduleDataSetup;
protected $_eavSetupFactory;

public function __construct(      //not sure if i can include construct here
    ObjectManagerInterface $objectManager,
    ModuleDataSetupInterface $moduleDataSetup,
    EavSetupFactory $eavSetupFactory,
) {

    $this->_objectManager = $objectManager;
    $this->_moduleDataSetup = $moduleDataSetup;
    $this->_eavSetupFactory = $eavSetupFactory;
}

public function launch()
{
    $eavSetup = $this->_eavSetupFactory->create([
                'setup' => $this->_moduleDataSetup
    ]);

    $defaultId = $eavSetup->getDefaultAttributeSetId(self::ENTITY_TYPE);

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    //$entityTypeId  = $objectManager->create('\Magento\Catalog\Model\Product');
    $entityTypeId = $objectManager->create('Magento\Eav\Model\Config')
                                    ->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
                                    ->getEntityTypeId();    // to get entity_type_id by entity_type_code

    $model = $objectManager->create('Magento\Eav\Api\Data\AttributeSetInterface')
                                    ->setId(null)
                                    ->setEntityTypeId(4)
                                    ->setAttributeSetName($name);

            $objectManager->create('Magento\Eav\Api\AttributeSetManagementInterface')
                            ->create(self::ENTITY_TYPE, $model, $defaultId)
                            ->save();
}


public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
    return false;
}

}

I get Call to a member function sendResponse() on null in \Magento\Framework\App\Bootstrap.php on line 25

1) Is what i tried i construct() in scripts.

2) Or should i run InstallData Schmeas procedure.

Best Answer

  • Create a module
  • Create a setup script

Adapt this code to your context. It creates an attribute set and then assign an attribute to this attribute set. Use dependency injection.

use Magento\Eav\Model\Entity\TypeFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Eav\Model\AttributeSetManagement;
use Magento\Eav\Model\AttributeManagement;

public function createAttributeSets() {
    $entityTypeCode = 'catalog_product';
    $entityType     = $this->eavTypeFactory->create()->loadByCode($entityTypeCode);
    $defaultSetId   = $entityType->getDefaultAttributeSetId();

    $attributeSet = $this->attributeSetFactory->create();
    $data = [
        'attribute_set_name'    => 'attribute_set_name',
        'entity_type_id'        => $entityType->getId(),
        'sort_order'            => 200,
    ];
    $attributeSet->setData($data);

    $this->attributeSetManagement->create($entityTypeCode, $attributeSet, $defaultSetId);

    $this->attributeManagement->assign(
        'catalog_product',
        $attributeSet->getId(),
        $attributeSet->getDefaultGroupId(),
        'attribute_code',
        $attributeSet->getCollection()->count() * 10
    );
}

If you want a bulk import, adapt your code. (edited from comment request)

public function createAttributeSets() {
    $entityTypeCode = 'catalog_product';
    $entityType     = $this->eavTypeFactory->create()->loadByCode($entityTypeCode);
    $defaultSetId   = $entityType->getDefaultAttributeSetId();

    $datas = [
        [
            'attribute_set_name'    => 'attribute_set_name',
            'entity_type_id'        => $entityType->getId(),
            'sort_order'            => 200,
        ],
        [
            'attribute_set_name'    => 'attribute_set_name_2',
            'entity_type_id'        => $entityType->getId(),
            'sort_order'            => 300,
        ]
    ];

    foreach ($datas as $data) {
        $attributeSet = $this->attributeSetFactory->create();

        $attributeSet->setData($data);

        $this->attributeSetManagement->create($entityTypeCode, $attributeSet, $defaultSetId);

        $this->attributeManagement->assign(
            'catalog_product',
            $attributeSet->getId(),
            $attributeSet->getDefaultGroupId(),
            'attribute_code',
            $attributeSet->getCollection()->count() * 10
        );
    }
}
Related Topic