Question

I would like to create 3 attribute sets and one attribute group assigned to the attributes sets created via the InstallData. This is my InstallData content:

<?php
namespace Vendor\ModuleName\Setup;

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

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

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

    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $this->createAttributeSets($setup, 'Attr Set 1');
        $this->createAttributeSets($setup, 'Attr Set 2');
        $this->createAttributeSets($setup, 'Attr Set 3');

        $this->createAttributeSetGroup('Attr Set 1');
        $this->createAttributeSetGroup('Attr Set 2');
        $this->createAttributeSetGroup('Attr Set 3');
    }
    
    private function createAttributeSets($setup, $attributeSetName)
    {
        $setup->startSetup();
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $attributeSet = $this->attributeSetFactory->create();
        $entityTypeId = $categorySetup->getEntityTypeId(Product::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); // Default attribute set Id

        $data = [
            'attribute_set_name' => $attributeSetName, //attribute set name
            'entity_type_id' => $entityTypeId,
            'sort_order' => 50,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId)->save(); // based on default attribute set
    }
    
    private function createAttributeSetGroup($attributeGroupSet)
    {
        $attributeSet = 'Attributes'; // my attribute group name
        $eavSetup = $this->eavSetupFactory->create();
        $entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
        $attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);

        if (isset($attributeSet['attribute_set_id'])) {
            $eavSetup->addAttributeGroup(
                $entityTypeId,
                $attributeSet['attribute_set_id'],
                $attributeGroupSet,
                100
            );
        }
    }
}

By doing a setup upgrade. I have the attribute sets, but the group is missing. I would like to have it displayed, like in the img below: enter image description here

Please tell me , what I am doing wrong :) Thank you in advance!

Was it helpful?

Solution

I hope my answer will help someone. So this is what works for me:

<?php
namespace Vendor\ModuleName\Setup;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;

class InstallData implements InstallDataInterface
{
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    /**
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var AttributeGroupRepositoryInterface
     */
    private $attributeGroupRepository;
    /**
     * @var AttributeGroupInterfaceFactory
     */
    private $attributeGroupFactory;

    /**
     * @var CollectionFactory
     */
    private $collectionAttributeSet;

    /**
     * InstallData constructor.
     * @param AttributeSetFactory $attributeSetFactory
     * @param CategorySetupFactory $categorySetupFactory
     * @param EavSetupFactory $eavSetupFactory
     * @param AttributeGroupRepositoryInterface $attributeGroupRepository
     * @param AttributeGroupInterfaceFactory $attributeGroupFactory
     * @param CollectionFactory $collectionAttributeSet
     */
    public function __construct(
        AttributeSetFactory $attributeSetFactory,
        CategorySetupFactory $categorySetupFactory,
        EavSetupFactory $eavSetupFactory,
        AttributeGroupRepositoryInterface $attributeGroupRepository,
        AttributeGroupInterfaceFactory $attributeGroupFactory,
        CollectionFactory $collectionAttributeSet
    ) {
        $this->attributeSetFactory = $attributeSetFactory;
        $this->categorySetupFactory = $categorySetupFactory;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->attributeGroupRepository = $attributeGroupRepository;
        $this->attributeGroupFactory = $attributeGroupFactory;
        $this->collectionAttributeSet = $collectionAttributeSet;
    }

    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $this->createAttributeSets($setup, 'Attr Set 1');
        $this->createAttributeSets($setup, 'Attr Set 2');
        $this->createAttributeSets($setup, 'Attr Set 3');

        $this->createAttributeGroup(['Attr Set 1','Attr Set 2','Attr Set 3']);
    }

    private function createAttributeSets($setup, $attributeSetName)
    {
        $setup->startSetup();
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $attributeSet = $this->attributeSetFactory->create();
        $entityTypeId = $categorySetup->getEntityTypeId(Product::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); // Default attribute set Id

        $data = [
            'attribute_set_name' => $attributeSetName, //attribute set name
            'entity_type_id' => $entityTypeId,
            'sort_order' => 50,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId)->save(); // based on default attribute set
    }

    /**
     * @param array $attrSetName
     * @throws NoSuchEntityException
     * @throws StateException
     */
    private function createAttributeGroup(array $attrSetName)
    {
        for ($i=0; $i<count($attrSetName); $i++) {
            $attributeGroup = $this->attributeGroupFactory->create();

            $attributeGroup->setAttributeSetId($this->getAttrSetId($attrSetName[$i]));
            $attributeGroup->setAttributeGroupName('Attributes');
            $this->attributeGroupRepository->save($attributeGroup);
        }
    }

    /**
     * @param string $attrSetName
     * @return int
     */
    private function getAttrSetId(string $attrSetName)
    {
        $attributeSet = $this->collectionAttributeSet->create()->addFieldToSelect(
            '*'
        )->addFieldToFilter(
            'attribute_set_name',
            $attrSetName
        );
        $attributeSetId = 0;
        foreach ($attributeSet as $attr) {
            $attributeSetId = $attr->getAttributeSetId();
        }

        return $attributeSetId;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top