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;
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
add a comment |
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
>try to change attribute_set_id to 'attribute_set'
– Chander Shekhar
Nov 17 '17 at 7:17
add a comment |
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
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
magento-2.1
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
add a comment |
>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
add a comment |
3 Answers
3
active
oldest
votes
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();
?>
add a comment |
Simply change the below line
'attribute_set_id' => 'NewAttributeSet',
to
'attribute_set' => 'AttributeSetName',
Now your attribute get assigned to above mentioned attribute name.
add a comment |
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 toaddAttribute()
. - 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 (norattribute_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()
afteraddAttribute()
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
);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
?>
add a comment |
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();
?>
add a comment |
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();
?>
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();
?>
answered Nov 17 '17 at 7:32
Sujeet PanditSujeet Pandit
32319
32319
add a comment |
add a comment |
Simply change the below line
'attribute_set_id' => 'NewAttributeSet',
to
'attribute_set' => 'AttributeSetName',
Now your attribute get assigned to above mentioned attribute name.
add a comment |
Simply change the below line
'attribute_set_id' => 'NewAttributeSet',
to
'attribute_set' => 'AttributeSetName',
Now your attribute get assigned to above mentioned attribute name.
add a comment |
Simply change the below line
'attribute_set_id' => 'NewAttributeSet',
to
'attribute_set' => 'AttributeSetName',
Now your attribute get assigned to above mentioned attribute name.
Simply change the below line
'attribute_set_id' => 'NewAttributeSet',
to
'attribute_set' => 'AttributeSetName',
Now your attribute get assigned to above mentioned attribute name.
answered Nov 17 '17 at 7:33
TBI InfotechTBI Infotech
4,5971830
4,5971830
add a comment |
add a comment |
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 toaddAttribute()
. - 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 (norattribute_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()
afteraddAttribute()
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
);
add a comment |
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 toaddAttribute()
. - 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 (norattribute_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()
afteraddAttribute()
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
);
add a comment |
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 toaddAttribute()
. - 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 (norattribute_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()
afteraddAttribute()
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
);
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 toaddAttribute()
. - 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 (norattribute_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()
afteraddAttribute()
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
);
answered 2 days ago
user3409662user3409662
1464
1464
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
>try to change attribute_set_id to 'attribute_set'
– Chander Shekhar
Nov 17 '17 at 7:17