5

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.

  • Just share some logic what u thought then we move on. Question Title not good at all – Ankit Shah Oct 6 '16 at 6:21
  • I have updated the question and my code @Ankit – Sushivam Oct 6 '16 at 6:45
  • That looks like a question @Sachin. Thanks – Ankit Shah Oct 6 '16 at 6:46
  • What am i supposed to post? i have updated the script what i tried – Sushivam Oct 6 '16 at 6:50
  • What u have posted is correct now – Ankit Shah Oct 6 '16 at 6:51
1
  • 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
        );
    }
}
  • Can we add multiple attribute sets here: $data = [ 'attribute_set_name' => 'Attribute set1', 'entity_type_id' => $entityType->getId(), 'sort_order' => 200, ]; – Sushivam Mar 8 '17 at 7:24
  • You cannot with native Magento. But you can create your custom attributeSetManagement based on this method : \Magento\Eav\Model\AttributeSetManagement::create Or create a custom function which handle this by call create multiple times. – Franck Garnier Mar 8 '17 at 7:29
  • Just add a foreach ... answer edited. – Franck Garnier Mar 8 '17 at 7:38
  • where is $attributeManagement, it says undefined – Sushivam Mar 8 '17 at 9:44
  • You need to inject the class Magento\Eav\Model\AttributeManagement from the constructor. I let you check Magento documentation about Dependancy Injection (DI devdocs.magento.com/guides/v2.1/extension-dev-guide/…) or open a new question because it is not the main topic. – Franck Garnier Mar 8 '17 at 10:16

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.