Question

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
            ],
        ],
    ]
Was it helpful?

Solution

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top