Assign Attribute to only specific attribute set Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?create date and time attribute for product in magento 2Create default billing/shipping addressAttribute Set NamePrice not getting updated for second product…How to save only specific attribute value rather than saving the whole product in Magento2Magento 2.1.5 How to create Module which can use Eav functionalities (addAttribute in my case)Magento2 update layout according to attribute setAdding a Product Attribute in magento 2.1.7New created attributes not found, after use EavSetup to createShow only Specific Order Status by user in magento 2

How could we fake a moon landing now?

What does this say in Elvish?

If the probability of a dog barking one or more times in a given hour is 84%, then what is the probability of a dog barking in 30 minutes?

How does the math work when buying airline miles?

Strange behavior of Object.defineProperty() in JavaScript

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Why can't I install Tomboy in Ubuntu Mate 19.04?

Google .dev domain strangely redirects to https

What do you call the main part of a joke?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Maximum summed subsequences with non-adjacent items

The Nth Gryphon Number

Dynamic filling of a region of a polar plot

AppleTVs create a chatty alternate WiFi network

How does light 'choose' between wave and particle behaviour?

macOS: Name for app shortcut screen found by pinching with thumb and three fingers

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

How many time has Arya actually used Needle?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

What makes a man succeed?

Can the Flaming Sphere spell be rammed into multiple Tiny creatures that are in the same 5-foot square?

What's the point of the test set?

What initially awakened the Balrog?

Why is it faster to reheat something than it is to cook it?



Assign Attribute to only specific attribute set Magento 2



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?create date and time attribute for product in magento 2Create default billing/shipping addressAttribute Set NamePrice not getting updated for second product…How to save only specific attribute value rather than saving the whole product in Magento2Magento 2.1.5 How to create Module which can use Eav functionalities (addAttribute in my case)Magento2 update layout according to attribute setAdding a Product Attribute in magento 2.1.7New created attributes not found, after use EavSetup to createShow only Specific Order Status by user in magento 2



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















Here's my code:



<?php
namespace DemoMymoduleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogSetupCategorySetupFactory;
use MagentoEavModelEntityAttributeSetFactory 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();

// CREATE ATTRIBUTE SET
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'NewAttributeSet',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();

// CREATE PRODUCT ATTRIBUTE
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'text_new',
[
'type' => 'varchar',
'label' => 'New Text',
'backend' => '',
'input' => 'text',
'wysiwyg_enabled' => false,
'source' => '',
'required' => false,
'sort_order' => 5,
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
'attribute_set_id' => 'NewAttributeSet',
]
);

$setup->endSetup();


?>


All working fine but the attribute is assigning to all attribute set and I want on only one attribute set which I have created: NewAttributeSet










share|improve this question






















  • >try to change attribute_set_id to 'attribute_set'

    – Chander Shekhar
    Nov 17 '17 at 7:17

















4















Here's my code:



<?php
namespace DemoMymoduleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogSetupCategorySetupFactory;
use MagentoEavModelEntityAttributeSetFactory 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();

// CREATE ATTRIBUTE SET
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'NewAttributeSet',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();

// CREATE PRODUCT ATTRIBUTE
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'text_new',
[
'type' => 'varchar',
'label' => 'New Text',
'backend' => '',
'input' => 'text',
'wysiwyg_enabled' => false,
'source' => '',
'required' => false,
'sort_order' => 5,
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
'attribute_set_id' => 'NewAttributeSet',
]
);

$setup->endSetup();


?>


All working fine but the attribute is assigning to all attribute set and I want on only one attribute set which I have created: NewAttributeSet










share|improve this question






















  • >try to change attribute_set_id to 'attribute_set'

    – Chander Shekhar
    Nov 17 '17 at 7:17













4












4








4








Here's my code:



<?php
namespace DemoMymoduleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogSetupCategorySetupFactory;
use MagentoEavModelEntityAttributeSetFactory 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();

// CREATE ATTRIBUTE SET
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'NewAttributeSet',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();

// CREATE PRODUCT ATTRIBUTE
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'text_new',
[
'type' => 'varchar',
'label' => 'New Text',
'backend' => '',
'input' => 'text',
'wysiwyg_enabled' => false,
'source' => '',
'required' => false,
'sort_order' => 5,
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
'attribute_set_id' => 'NewAttributeSet',
]
);

$setup->endSetup();


?>


All working fine but the attribute is assigning to all attribute set and I want on only one attribute set which I have created: NewAttributeSet










share|improve this question














Here's my code:



<?php
namespace DemoMymoduleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogSetupCategorySetupFactory;
use MagentoEavModelEntityAttributeSetFactory 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();

// CREATE ATTRIBUTE SET
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'NewAttributeSet',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();

// CREATE PRODUCT ATTRIBUTE
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'text_new',
[
'type' => 'varchar',
'label' => 'New Text',
'backend' => '',
'input' => 'text',
'wysiwyg_enabled' => false,
'source' => '',
'required' => false,
'sort_order' => 5,
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
'attribute_set_id' => 'NewAttributeSet',
]
);

$setup->endSetup();


?>


All working fine but the attribute is assigning to all attribute set and I want on only one attribute set which I have created: NewAttributeSet







magento-2.1






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '17 at 7:05









Syed Muneeb Ul HasanSyed Muneeb Ul Hasan

645317




645317












  • >try to change attribute_set_id to 'attribute_set'

    – Chander Shekhar
    Nov 17 '17 at 7:17

















  • >try to change attribute_set_id to 'attribute_set'

    – Chander Shekhar
    Nov 17 '17 at 7:17
















>try to change attribute_set_id to 'attribute_set'

– Chander Shekhar
Nov 17 '17 at 7:17





>try to change attribute_set_id to 'attribute_set'

– Chander Shekhar
Nov 17 '17 at 7:17










3 Answers
3






active

oldest

votes


















0














Try This One, Check the New Code Added.



<?php
namespace DemoMymoduleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogSetupCategorySetupFactory;
use MagentoEavModelEntityAttributeSetFactory 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();

// CREATE ATTRIBUTE SET
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'NewAttributeSet',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();

/*************** New Code Start ***************/
$attribute_group = 'New Attribute Group Job';
$this->eavSetupFactory->addAttributeGroup(MagentoCatalogModelProduct::ENTITY, $attributeSetId, $attribute_group);
/*************** New Code End ***************/

// CREATE PRODUCT ATTRIBUTE
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'text_new',
[
'type' => 'varchar',
'label' => 'New Text',
'backend' => '',
'input' => 'text',
'wysiwyg_enabled' => false,
'source' => '',
'required' => false,
'sort_order' => 5,
'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
'attribute_set_id' => 'NewAttributeSet',
]
);

/*************** New Code Start ***************/
$this->eavSetupFactory->addAttributeToSet(MagentoCatalogModelProduct::ENTITY,$attributeSetId,$attribute_group,'text_new',null);
/*************** New Code End ***************/

$setup->endSetup();




?>





share|improve this answer






























    0














    Simply change the below line




    'attribute_set_id' => 'NewAttributeSet',




    to




    'attribute_set' => 'AttributeSetName',




    Now your attribute get assigned to above mentioned attribute name.






    share|improve this answer






























      0














      To prevent Magento 2 from adding your attribute to all attribute sets (as of Magento 2.3.1):



      • You must set 'user_defined' => true, in the array passed to addAttribute().

      • You must not define a group key in the array, otherwise Magento will add your attribute, in the specified group, to all attribute sets.

      • You don't need to define an attribute_set_id key (nor attribute_set). It doesn't do anything.

      • You don't need to specify a sort_order key. It is only used when Magento adds the attribute to all attribute sets.

      • You need to call addAttributeToGroup() after addAttribute() to add it to the attribute set you want.

      The code would look like this:



      $eavSetup->addAttribute(
      MagentoCatalogModelProduct::ENTITY,
      'text_new',
      [
      'user_defined' => true,
      'type' => 'varchar',
      'label' => 'New Text',
      'input' => 'text',
      'required' => false,
      'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
      'used_in_product_listing' => true,
      'visible_on_front' => true,
      ]
      );

      $eavSetup->addAttributeToGroup(
      MagentoCatalogModelProduct::ENTITY,
      'NewAttributeSet',
      'General', // group
      'text_new',
      5 // sort order
      );





      share|improve this answer























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "479"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f201966%2fassign-attribute-to-only-specific-attribute-set-magento-2%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Try This One, Check the New Code Added.



        <?php
        namespace DemoMymoduleSetup;

        use MagentoEavSetupEavSetup;
        use MagentoEavSetupEavSetupFactory;
        use MagentoFrameworkSetupInstallDataInterface;
        use MagentoFrameworkSetupModuleContextInterface;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoCatalogSetupCategorySetupFactory;
        use MagentoEavModelEntityAttributeSetFactory 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();

        // CREATE ATTRIBUTE SET
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

        $attributeSet = $this->attributeSetFactory->create();
        $entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
        $data = [
        'attribute_set_name' => 'NewAttributeSet',
        'entity_type_id' => $entityTypeId,
        'sort_order' => 200,
        ];
        $attributeSet->setData($data);
        $attributeSet->validate();
        $attributeSet->save();
        $attributeSet->initFromSkeleton($attributeSetId);
        $attributeSet->save();

        /*************** New Code Start ***************/
        $attribute_group = 'New Attribute Group Job';
        $this->eavSetupFactory->addAttributeGroup(MagentoCatalogModelProduct::ENTITY, $attributeSetId, $attribute_group);
        /*************** New Code End ***************/

        // CREATE PRODUCT ATTRIBUTE
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
        MagentoCatalogModelProduct::ENTITY,
        'text_new',
        [
        'type' => 'varchar',
        'label' => 'New Text',
        'backend' => '',
        'input' => 'text',
        'wysiwyg_enabled' => false,
        'source' => '',
        'required' => false,
        'sort_order' => 5,
        'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
        'used_in_product_listing' => true,
        'visible_on_front' => true,
        'attribute_set_id' => 'NewAttributeSet',
        ]
        );

        /*************** New Code Start ***************/
        $this->eavSetupFactory->addAttributeToSet(MagentoCatalogModelProduct::ENTITY,$attributeSetId,$attribute_group,'text_new',null);
        /*************** New Code End ***************/

        $setup->endSetup();




        ?>





        share|improve this answer



























          0














          Try This One, Check the New Code Added.



          <?php
          namespace DemoMymoduleSetup;

          use MagentoEavSetupEavSetup;
          use MagentoEavSetupEavSetupFactory;
          use MagentoFrameworkSetupInstallDataInterface;
          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;
          use MagentoCatalogSetupCategorySetupFactory;
          use MagentoEavModelEntityAttributeSetFactory 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();

          // CREATE ATTRIBUTE SET
          $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

          $attributeSet = $this->attributeSetFactory->create();
          $entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
          $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
          $data = [
          'attribute_set_name' => 'NewAttributeSet',
          'entity_type_id' => $entityTypeId,
          'sort_order' => 200,
          ];
          $attributeSet->setData($data);
          $attributeSet->validate();
          $attributeSet->save();
          $attributeSet->initFromSkeleton($attributeSetId);
          $attributeSet->save();

          /*************** New Code Start ***************/
          $attribute_group = 'New Attribute Group Job';
          $this->eavSetupFactory->addAttributeGroup(MagentoCatalogModelProduct::ENTITY, $attributeSetId, $attribute_group);
          /*************** New Code End ***************/

          // CREATE PRODUCT ATTRIBUTE
          $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

          $eavSetup->addAttribute(
          MagentoCatalogModelProduct::ENTITY,
          'text_new',
          [
          'type' => 'varchar',
          'label' => 'New Text',
          'backend' => '',
          'input' => 'text',
          'wysiwyg_enabled' => false,
          'source' => '',
          'required' => false,
          'sort_order' => 5,
          'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
          'used_in_product_listing' => true,
          'visible_on_front' => true,
          'attribute_set_id' => 'NewAttributeSet',
          ]
          );

          /*************** New Code Start ***************/
          $this->eavSetupFactory->addAttributeToSet(MagentoCatalogModelProduct::ENTITY,$attributeSetId,$attribute_group,'text_new',null);
          /*************** New Code End ***************/

          $setup->endSetup();




          ?>





          share|improve this answer

























            0












            0








            0







            Try This One, Check the New Code Added.



            <?php
            namespace DemoMymoduleSetup;

            use MagentoEavSetupEavSetup;
            use MagentoEavSetupEavSetupFactory;
            use MagentoFrameworkSetupInstallDataInterface;
            use MagentoFrameworkSetupModuleContextInterface;
            use MagentoFrameworkSetupModuleDataSetupInterface;
            use MagentoCatalogSetupCategorySetupFactory;
            use MagentoEavModelEntityAttributeSetFactory 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();

            // CREATE ATTRIBUTE SET
            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

            $attributeSet = $this->attributeSetFactory->create();
            $entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
            $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
            $data = [
            'attribute_set_name' => 'NewAttributeSet',
            'entity_type_id' => $entityTypeId,
            'sort_order' => 200,
            ];
            $attributeSet->setData($data);
            $attributeSet->validate();
            $attributeSet->save();
            $attributeSet->initFromSkeleton($attributeSetId);
            $attributeSet->save();

            /*************** New Code Start ***************/
            $attribute_group = 'New Attribute Group Job';
            $this->eavSetupFactory->addAttributeGroup(MagentoCatalogModelProduct::ENTITY, $attributeSetId, $attribute_group);
            /*************** New Code End ***************/

            // CREATE PRODUCT ATTRIBUTE
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'text_new',
            [
            'type' => 'varchar',
            'label' => 'New Text',
            'backend' => '',
            'input' => 'text',
            'wysiwyg_enabled' => false,
            'source' => '',
            'required' => false,
            'sort_order' => 5,
            'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
            'used_in_product_listing' => true,
            'visible_on_front' => true,
            'attribute_set_id' => 'NewAttributeSet',
            ]
            );

            /*************** New Code Start ***************/
            $this->eavSetupFactory->addAttributeToSet(MagentoCatalogModelProduct::ENTITY,$attributeSetId,$attribute_group,'text_new',null);
            /*************** New Code End ***************/

            $setup->endSetup();




            ?>





            share|improve this answer













            Try This One, Check the New Code Added.



            <?php
            namespace DemoMymoduleSetup;

            use MagentoEavSetupEavSetup;
            use MagentoEavSetupEavSetupFactory;
            use MagentoFrameworkSetupInstallDataInterface;
            use MagentoFrameworkSetupModuleContextInterface;
            use MagentoFrameworkSetupModuleDataSetupInterface;
            use MagentoCatalogSetupCategorySetupFactory;
            use MagentoEavModelEntityAttributeSetFactory 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();

            // CREATE ATTRIBUTE SET
            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

            $attributeSet = $this->attributeSetFactory->create();
            $entityTypeId = $categorySetup->getEntityTypeId(MagentoCatalogModelProduct::ENTITY);
            $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
            $data = [
            'attribute_set_name' => 'NewAttributeSet',
            'entity_type_id' => $entityTypeId,
            'sort_order' => 200,
            ];
            $attributeSet->setData($data);
            $attributeSet->validate();
            $attributeSet->save();
            $attributeSet->initFromSkeleton($attributeSetId);
            $attributeSet->save();

            /*************** New Code Start ***************/
            $attribute_group = 'New Attribute Group Job';
            $this->eavSetupFactory->addAttributeGroup(MagentoCatalogModelProduct::ENTITY, $attributeSetId, $attribute_group);
            /*************** New Code End ***************/

            // CREATE PRODUCT ATTRIBUTE
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'text_new',
            [
            'type' => 'varchar',
            'label' => 'New Text',
            'backend' => '',
            'input' => 'text',
            'wysiwyg_enabled' => false,
            'source' => '',
            'required' => false,
            'sort_order' => 5,
            'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
            'used_in_product_listing' => true,
            'visible_on_front' => true,
            'attribute_set_id' => 'NewAttributeSet',
            ]
            );

            /*************** New Code Start ***************/
            $this->eavSetupFactory->addAttributeToSet(MagentoCatalogModelProduct::ENTITY,$attributeSetId,$attribute_group,'text_new',null);
            /*************** New Code End ***************/

            $setup->endSetup();




            ?>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '17 at 7:32









            Sujeet PanditSujeet Pandit

            32319




            32319























                0














                Simply change the below line




                'attribute_set_id' => 'NewAttributeSet',




                to




                'attribute_set' => 'AttributeSetName',




                Now your attribute get assigned to above mentioned attribute name.






                share|improve this answer



























                  0














                  Simply change the below line




                  'attribute_set_id' => 'NewAttributeSet',




                  to




                  'attribute_set' => 'AttributeSetName',




                  Now your attribute get assigned to above mentioned attribute name.






                  share|improve this answer

























                    0












                    0








                    0







                    Simply change the below line




                    'attribute_set_id' => 'NewAttributeSet',




                    to




                    'attribute_set' => 'AttributeSetName',




                    Now your attribute get assigned to above mentioned attribute name.






                    share|improve this answer













                    Simply change the below line




                    'attribute_set_id' => 'NewAttributeSet',




                    to




                    'attribute_set' => 'AttributeSetName',




                    Now your attribute get assigned to above mentioned attribute name.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 '17 at 7:33









                    TBI InfotechTBI Infotech

                    4,5971830




                    4,5971830





















                        0














                        To prevent Magento 2 from adding your attribute to all attribute sets (as of Magento 2.3.1):



                        • You must set 'user_defined' => true, in the array passed to addAttribute().

                        • You must not define a group key in the array, otherwise Magento will add your attribute, in the specified group, to all attribute sets.

                        • You don't need to define an attribute_set_id key (nor attribute_set). It doesn't do anything.

                        • You don't need to specify a sort_order key. It is only used when Magento adds the attribute to all attribute sets.

                        • You need to call addAttributeToGroup() after addAttribute() to add it to the attribute set you want.

                        The code would look like this:



                        $eavSetup->addAttribute(
                        MagentoCatalogModelProduct::ENTITY,
                        'text_new',
                        [
                        'user_defined' => true,
                        'type' => 'varchar',
                        'label' => 'New Text',
                        'input' => 'text',
                        'required' => false,
                        'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                        'used_in_product_listing' => true,
                        'visible_on_front' => true,
                        ]
                        );

                        $eavSetup->addAttributeToGroup(
                        MagentoCatalogModelProduct::ENTITY,
                        'NewAttributeSet',
                        'General', // group
                        'text_new',
                        5 // sort order
                        );





                        share|improve this answer



























                          0














                          To prevent Magento 2 from adding your attribute to all attribute sets (as of Magento 2.3.1):



                          • You must set 'user_defined' => true, in the array passed to addAttribute().

                          • You must not define a group key in the array, otherwise Magento will add your attribute, in the specified group, to all attribute sets.

                          • You don't need to define an attribute_set_id key (nor attribute_set). It doesn't do anything.

                          • You don't need to specify a sort_order key. It is only used when Magento adds the attribute to all attribute sets.

                          • You need to call addAttributeToGroup() after addAttribute() to add it to the attribute set you want.

                          The code would look like this:



                          $eavSetup->addAttribute(
                          MagentoCatalogModelProduct::ENTITY,
                          'text_new',
                          [
                          'user_defined' => true,
                          'type' => 'varchar',
                          'label' => 'New Text',
                          'input' => 'text',
                          'required' => false,
                          'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                          'used_in_product_listing' => true,
                          'visible_on_front' => true,
                          ]
                          );

                          $eavSetup->addAttributeToGroup(
                          MagentoCatalogModelProduct::ENTITY,
                          'NewAttributeSet',
                          'General', // group
                          'text_new',
                          5 // sort order
                          );





                          share|improve this answer

























                            0












                            0








                            0







                            To prevent Magento 2 from adding your attribute to all attribute sets (as of Magento 2.3.1):



                            • You must set 'user_defined' => true, in the array passed to addAttribute().

                            • You must not define a group key in the array, otherwise Magento will add your attribute, in the specified group, to all attribute sets.

                            • You don't need to define an attribute_set_id key (nor attribute_set). It doesn't do anything.

                            • You don't need to specify a sort_order key. It is only used when Magento adds the attribute to all attribute sets.

                            • You need to call addAttributeToGroup() after addAttribute() to add it to the attribute set you want.

                            The code would look like this:



                            $eavSetup->addAttribute(
                            MagentoCatalogModelProduct::ENTITY,
                            'text_new',
                            [
                            'user_defined' => true,
                            'type' => 'varchar',
                            'label' => 'New Text',
                            'input' => 'text',
                            'required' => false,
                            'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                            'used_in_product_listing' => true,
                            'visible_on_front' => true,
                            ]
                            );

                            $eavSetup->addAttributeToGroup(
                            MagentoCatalogModelProduct::ENTITY,
                            'NewAttributeSet',
                            'General', // group
                            'text_new',
                            5 // sort order
                            );





                            share|improve this answer













                            To prevent Magento 2 from adding your attribute to all attribute sets (as of Magento 2.3.1):



                            • You must set 'user_defined' => true, in the array passed to addAttribute().

                            • You must not define a group key in the array, otherwise Magento will add your attribute, in the specified group, to all attribute sets.

                            • You don't need to define an attribute_set_id key (nor attribute_set). It doesn't do anything.

                            • You don't need to specify a sort_order key. It is only used when Magento adds the attribute to all attribute sets.

                            • You need to call addAttributeToGroup() after addAttribute() to add it to the attribute set you want.

                            The code would look like this:



                            $eavSetup->addAttribute(
                            MagentoCatalogModelProduct::ENTITY,
                            'text_new',
                            [
                            'user_defined' => true,
                            'type' => 'varchar',
                            'label' => 'New Text',
                            'input' => 'text',
                            'required' => false,
                            'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                            'used_in_product_listing' => true,
                            'visible_on_front' => true,
                            ]
                            );

                            $eavSetup->addAttributeToGroup(
                            MagentoCatalogModelProduct::ENTITY,
                            'NewAttributeSet',
                            'General', // group
                            'text_new',
                            5 // sort order
                            );






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 days ago









                            user3409662user3409662

                            1464




                            1464



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Magento Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f201966%2fassign-attribute-to-only-specific-attribute-set-magento-2%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                                Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form