0

I have the following code:

<?php
namespace Vendor\ModuleSetup;

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;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;


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

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        //creating attribute set 
        $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' => 'medicamento_expira_curta_FINAL', 
            'entity_type_id' => $entityTypeId,
            'sort_order' => 200,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId);
        $attributeSet->save();

        $eavSetup->addAttributeGroup(
            $entityTypeId, 
            $attributeSet->getId(), 
            'medicamento_expira_curta_YYGroupFINAL', 
            200
        );

        //Creating products attributes
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                        'Data de NNFINAL',
                        [
                            'group' => 'medicamento_expira_curta_YYGroupFINAL',
                            'type' => 'varchar',
                            'label' => 'Data de NNFINAL', //attr name
                            'backend' => '',
                            'input' => 'date',
                            'source' => '',
                            'required' => false,
                            'sort_order' => 5,
                            'default' => '0',
                            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                            'visible' => true,
                            'used_in_product_listing' => true,
                            'visible_on_front' => true,
                            'attribute_set_id' => 'medicamento_expira_curta_FINAL',
                    ]
                );  

                $eavSetup->addAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                        'Data de vencimento do YYFINAL',
                        [
                            'group' => 'medicamento_expira_curta_YYGroupFINAL',
                            'type' => 'varchar',
                            'label' => 'Data de vencimento do YYFINAL', //attr name
                            'backend' => '',
                            'input' => 'date',
                            'source' => '',
                            'required' => false,
                            'sort_order' => 5,
                            'default' => '0',
                            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                            'visible' => true,
                            'used_in_product_listing' => true,
                            'visible_on_front' => true,
                            'attribute_set_id' => 'medicamento_expira_curta_FINAL',
                    ]
                );  

        $setup->endSetup();


    }
}
?>

But, when i persist, the default attribute set receive this attribute too, but i need this not to happen. have a configuration for this ?

0

Please try below code :

<?php


namespace Product\Attribute\Setup;

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

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'data_de_nnfinal',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => '',
                'label' => 'Data de NNFINAL',
                'input' => 'date',
                'class' => '',
                'source' => ,
                'global' => 1,
                'visible' => true,
                'required' => true,
                'user_defined' => true,
                'default' => null,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => '',
                'system' => 1,
                'group' => 'medicamento_expira_curta_YYGroupFINAL',
                'option' => array('values' => array(""))
            ]
        );

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'data_de_vencimento_do_yyfinal',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => '',
                'label' => 'Data de vencimento do YYFINAL',
                'input' => 'date',
                'class' => '',
                'source' => ,
                'global' => 1,
                'visible' => true,
                'required' => true,
                'user_defined' => true,
                'default' => null,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => '',
                'system' => 1,
                'group' => 'medicamento_expira_curta_FINAL',
                'option' => array('values' => array(""))
            ]
        );
    }
}
  • just replace above code and see it working – Rakesh Donga Jan 16 at 11:48
  • but i need to use only in specifict attribute set – FlávioD Jan 16 at 11:53
  • you can add 'group' => 'medicamento_expira_curta_YYGroupFINAL', – Rakesh Donga Jan 16 at 11:55
  • dont work, is in the default attribut set too, like my original code – FlávioD Jan 16 at 12:00
  • i have updated my code you can add just your group name right – Rakesh Donga Jan 16 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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