I have created couple of custom Attribute Set and now I want to assign a group in those attribute sets programmatically, so is there any way to retrieve attribute set IDs based on name?
How to Retrieve Custom Attribute Set ID by Name
attribute-setmagento-2.1programmatically
Related Solutions
Yes, this is possible.
First, set these keys in your $data array to following values to avoid adding attribute to all attribute sets:
'user_defined' => true,
'group' => ''
Then add attribute to your attribute set:
$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);
You can try this code to apply default attribute set to custom attribute. It's working fine for me.
class UpgradeData implements UpgradeDataInterface
{
/**
* @var \Magento\Catalog\Model\Product\Attribute\Management
*/
private $attributeManagement;
/**
* @var AttributeSetFactory
*/
protected $attributeSetFactory;
/**
* @var CategorySetupFactory
*/
protected $categorySetupFactory;
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
\Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory,
\Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory,
\Magento\Catalog\Model\Product\Attribute\Management $attributeManagement
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->categorySetupFactory = $categorySetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->attributeManagement = $attributeManagement;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
/**
*checking the current version of the module
*this function is implemented from ModuleContextInterface
*/
$attributeSet = $this->attributeSetFactory->create();
$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$data = [
'attribute_set_name' => 'Attribute set name',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
];
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
$attributeSet->initFromSkeleton($attributeSetId);
$attributeSet->save();
$AttributeSetId = $attributeSet->getId();
$groupName = 'Attribute Group name';
$attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
$categorySetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 100);
$attributeGroupId = $categorySetup->getAttributeGroupId($entityTypeId, $AttributeSetId, $groupName);
}
}
Best Answer
You can retrieve attribute set ID by using following code:
You can call getAttrSetId method by passing the attribute set name for which you want to retrieve attribute set ID. eg.
$this->getAttrSetId("YourAttributeSetName");