5

Subject. Which classes I need to use to deal with it?

It seems that I don't understand much about how attributes work.

For example I use this code to add attribute to one attribute set and to one attribute group:

/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attribute->setAttributeSetId($attributeSetId);
$attribute->setAttributeGroupId($groupId);

Thanks.

UPD: My problem not in 'how to get attribute set ids', I already have a few. I'm more interesting in the part of code where // add your code here. I think if we set the value by method $attribute->setAttributeSetId(), it override previous attribute set link in the attribute. I'm trying to find some solution like $attribute->addAttributeSetId() or $attribute->setAttributeSetIds(), but there are none

UPD2: Found the way: Using \Magento\Eav\Api\AttributeManagementInterface->assign(). Full code in answer below.

2 Answers 2

3

Found a simple solution.

my method:

public function assignAttributeToSets($attribute_code, $attribute_set_ids) {
    foreach ($attribute_set_ids as $set_id) {
        $group_id = $this->config->getAttributeGroupId($set_id, 'Product Details');
        $this->attributeManagement->assign(
            'catalog_product',  // entity type code
            $set_id,
            $group_id,
            $attribute_code,
            null
        );
    }
}

And constructor:

public function __construct(
    \Magento\Catalog\Model\Config $config,
    \Magento\Eav\Api\AttributeManagementInterface $attributeManagement
)
{
    $this->config = $config;
    $this->attributeManagement = $attributeManagement;
}
1

Try following way:

/**
 * EAV setup factory
 *
 * @var \Magento\Eav\Setup\EavSetupFactory
 */
private $eavSetupFactory;

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

And now

/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
foreach ( $attributeSetIds as $attributeSetId) {
    // add your code here
}

[Update]

Check following class

Magento/Eav/Setup/EavSetup.php

Here you can see your desire method like

getAttributeSetId()
getAttributeGroupId()

So using previous example you can access these method using $eavSetup object. like

$eavSetup->getAttributeSetId($entityTypeId, $setId)
$eavSetup->getAttributeGroupId($entityTypeId, $setId, $groupId)

For update attribute set use

updateAttributeSet()

1
  • Check updated answer.
    – Sohel Rana
    Commented May 3, 2017 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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