6

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.

WISAM HAKIM
  • 2,135
  • 1
  • 12
  • 20
Sushivam
  • 2,629
  • 3
  • 36
  • 88
  • Just share some logic what u thought then we move on. Question Title not good at all – Jackson Oct 06 '16 at 06:21
  • I have updated the question and my code @Ankit – Sushivam Oct 06 '16 at 06:45
  • That looks like a question @Sachin. Thanks – Jackson Oct 06 '16 at 06:46
  • What am i supposed to post? i have updated the script what i tried – Sushivam Oct 06 '16 at 06:50
  • What u have posted is correct now – Jackson Oct 06 '16 at 06:51
  • Any update on the script for which i get the error.. – Sushivam Oct 06 '16 at 07:23
  • Refer to https://www.atwix.com/magento/adding-attribute-programatically-magento2/ https://www.mageplaza.com/magento-2-module-development/magento-2-add-product-attribute-programmatically.html http://magento.stackexchange.com/questions/103934/magento2-programmatically-add-product-attribute-options http://blog.mdnsolutions.com/magento-2-create-product-attributes-and-options-programmatically/ https://www.c3media.co.uk/blog/c3-news/creating-module-adding-product-attribute-magento-2/ – Jackson Oct 06 '16 at 07:27

2 Answers2

2
  • 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
        );
    }
}
Franck Garnier
  • 2,946
  • 1
  • 17
  • 35
  • Can we add multiple attribute sets here: $data = [ 'attribute_set_name' => 'Attribute set1', 'entity_type_id' => $entityType->getId(), 'sort_order' => 200, ]; – Sushivam Mar 08 '17 at 07: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 08 '17 at 07:29
  • Just add a foreach ... answer edited. – Franck Garnier Mar 08 '17 at 07:38
  • where is $attributeManagement, it says undefined – Sushivam Mar 08 '17 at 09:44
  • You need to inject the class Magento\Eav\Model\AttributeManagement from the constructor. I let you check Magento documentation about Dependancy Injection (DI http://devdocs.magento.com/guides/v2.1/extension-dev-guide/depend-inj.html) or open a new question because it is not the main topic. – Franck Garnier Mar 08 '17 at 10:16
  • Now i just get the attribute sets created, without any attributes assigned as per default attribute set. Have created a new question for this: http://magento.stackexchange.com/questions/164248/map-attribute-set-based-on-default-attribute-set-programatically-in-magento-2 – Sushivam Mar 14 '17 at 11:33
0

Following is the script to create an attribute set programmatically in Magento 2.

?php

namespace Company\Mymodule\Setup;

use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface { private $attributeSetFactory; private $attributeSet; private $categorySetupFactory;

public function __construct(AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; }

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

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

$setup->endSetup(); } }