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 allJackson– Jackson2016年10月06日 06:21:17 +00:00Commented Oct 6, 2016 at 6:21
-
I have updated the question and my code @AnkitSushivam– Sushivam2016年10月06日 06:45:50 +00:00Commented Oct 6, 2016 at 6:45
-
That looks like a question @Sachin. ThanksJackson– Jackson2016年10月06日 06:46:45 +00:00Commented Oct 6, 2016 at 6:46
-
What am i supposed to post? i have updated the script what i triedSushivam– Sushivam2016年10月06日 06:50:29 +00:00Commented Oct 6, 2016 at 6:50
-
What u have posted is correct nowJackson– Jackson2016年10月06日 06:51:51 +00:00Commented Oct 6, 2016 at 6:51
2 Answers 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
);
}
}
-
Can we add multiple attribute sets here: $data = [ 'attribute_set_name' => 'Attribute set1', 'entity_type_id' => $entityType->getId(), 'sort_order' => 200, ];Sushivam– Sushivam2017年03月08日 07:24:25 +00:00Commented Mar 8, 2017 at 7:24
-
You cannot with native Magento. But you can create your custom attributeSetManagement based on this method :
\Magento\Eav\Model\AttributeSetManagement::createOr create a custom function which handle this by call create multiple times.Franck Garnier– Franck Garnier2017年03月08日 07:29:07 +00:00Commented Mar 8, 2017 at 7:29 -
Just add a foreach ... answer edited.Franck Garnier– Franck Garnier2017年03月08日 07:38:10 +00:00Commented Mar 8, 2017 at 7:38
-
where is $attributeManagement, it says undefinedSushivam– Sushivam2017年03月08日 09:44:32 +00:00Commented Mar 8, 2017 at 9:44
-
1You need to inject the class
Magento\Eav\Model\AttributeManagementfrom 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– Franck Garnier2017年03月08日 10:16:53 +00:00Commented Mar 8, 2017 at 10:16
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();
}
}