1

Here is my installation method. Why, after installation, in the default attribute set, I see only the attributes https://prnt.sc/s20b3a, https://prnt.sc/s20czw? They should not be here. How to fix?

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

    // version 1.4.34
    // get default attribute set id
    $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); // default attribute set

    foreach ($this->dataAttrProduct as $section) {

        // add attribute set
        $attributeSet = $this->_attributeSetFactoryProduct->create();
        $data = [
            'attribute_set_name' => $section['attr_set_name'],
            'entity_type_id' => $entityTypeId,
            'sort_order' => 100,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId);
        $attributeSet->save();



        // add attributes
        foreach ($section['attrs'] as $attr) {
            if (empty($attr['code']) && empty($attr['label'])) continue;

            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                $attr['code'],
                [
                    'type' => $attr['type'],
                    'backend' => '',
                    'frontend' => '',
                    'label' => $attr['label'],
                    'input' => $attr['input'],
                    'class' => '',
                    'source' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'visible' => $attr['visible'],
                    'required' => $attr['required'],
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => $attr['visible_on_front'],
                    'used_in_product_listing' => $attr['used_in_product_listing'],
                    'unique' => false,
                    'apply_to' => '',
                    'attribute_set' => $section['attr_set_name']
                ]
            );
        }


        // add attributes group in  attribute set
        $attributeSetIdNomenclatures = $eavSetup->getAttributeSetId($entityTypeId,$section['attr_set_name']);
        $eavSetup->addAttributeGroup(
            $entityTypeId,
            $attributeSetIdNomenclatures,
            $section['attr_group_name'],
            100
        );

        // add attribute to group
        $attributeGroupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetIdNomenclatures, $section['attr_group_name']);
        foreach ($section['attrs'] as $attr) {
            $categorySetup->addAttributeToGroup(
                $entityTypeId,
                $attributeSetIdNomenclatures,
                $attributeGroupId,
                $attr['code'],
                10
            );
        }
    }



    $setup->endSetup();
}

Values ​​$ this-> dataAttrProduct . What should happen https://prnt.sc/s28b9p

 'fastener' => [
        'attr_group_name' => 'Fastener',
        'attr_set_name' => 'Fastener',
        'attrs' => [
            0 => [
                'code' =>'fasteners_sub_type',
                'type' => 'text',
                'input' => 'text',
                'label' =>'Подраздел',
                'visible' => true,
                'required' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false
            ],
            1 => [
                'code' =>'fasteners_brand',
                'type' => 'int',
                'input' => 'select',
                'label' =>'Бренд',
                'visible' => true,
                'required' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true
            ],
        ],
    ]

1 Answer 1

0

Try this code your InstallSchema.php and this code is only simple text show.

<?php
namespace Mage\Mohit\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sample_attribute',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sample Atrribute',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

After your run this command :--

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f

Thanks...

5
  • I need my attributes to be in my group, and the group in my set of attributes. prnt.sc/s28b9p . But after completing the installation, my attributes appear in the DEFAULT attribute set in PRODUCT DETAILS groups. See screenshot prnt.sc/s20b3a
    – Goryyn
    Commented Apr 19, 2020 at 11:22
  • you magento 2 admin panel backend create attributes set and insert the your attribute set your custom attribute Commented Apr 19, 2020 at 11:31
  • check my video :- - loom.com/share/ae6768ef35064003b6df9a1245fb162b Commented Apr 19, 2020 at 12:41
  • Do not specify a group key. We'll see later how to add the attribute to the group. And 'user_defined' => true. Otherwise, Magento will add the attribute, and the group, to all of the attribute sets
    – Goryyn
    Commented Apr 20, 2020 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.