5

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?

5

You can retrieve attribute set ID by using following code:

protected $_attributeSetCollection;

public function __construct(
     ...
     ,\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attributeSetCollection
)
    {
        ...
        $this->_attributeSetCollection = $attributeSetCollection;
    }


public function getAttrSetId($attrSetName)
    {
        $attributeSet = $this->_attributeSetCollection->create()->addFieldToSelect(
                    '*'
                    )->addFieldToFilter(
                            'attribute_set_name',
                            $attrSetName
                    );
        $attributeSetId = 0;
        foreach($attributeSet as $attr):
            $attributeSetId = $attr->getAttributeSetId();
        endforeach;
        return $attributeSetId;
    }

You can call getAttrSetId method by passing the attribute set name for which you want to retrieve attribute set ID. eg. $this->getAttrSetId("YourAttributeSetName");

  • you can reduce response size with select 'attribute_set_id' instead of '*' and avoid a loop on the object with combine getFirstItem and toArray methods. See: magento.stackexchange.com/a/214278/24845 – Nolwennig Feb 20 '18 at 16:28
  • You should also add ->addFieldToFilter('entity_type_id', 4) so the correct attribute_set_id for "product" is returned. – seanbreeden Oct 8 '18 at 16:39
5

Based on Rizwan Dhuka answer, you can:

  • reduce response size with select 'attribute_set_id' instead of '*'

  • avoid a loop on the object with combine getFirstItem and toArray methods

    namespace Nolwennig\CustomCatalog;
    
    use \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
    
    class CustomProduct
    {
    
      protected $_attributeSetCollection;
    
      /**
       *
       * ...
       * @param AttributeSetCollectionFactory $attributeSetCollection
       */
      public function __construct(..., CollectionFactory $attributeSetCollection)
      {
        ...
        $this->_attributeSetCollection = $attributeSetCollection;
      }
    
      /**
       *
       * @param string $attributeSetName
       * @return int attributeSetId
       */
      public function getAttributeSetId($attributeSetName)
      {
        $attributeSetCollection = $this->_attributeSetCollection->create()
          ->addFieldToSelect('attribute_set_id')
          ->addFieldToFilter('attribute_set_name', $attributeSetName)
          ->getFirstItem()
          ->toArray();
    
        $attributeSetId = (int) $attributeSetCollection['attribute_set_id'];
        // OR (see benchmark below for make your choice)
        $attributeSetId = (int) implode($attributeSetCollection);
    
        return $attributeSetId
      }
    }
    

Quick (n' dirty?) benchmarks

  • $attributeSetId = (int) implode($attributeSetCollection);

    .------------------------------------------------------------. 
    | Version   | System time (s) | User time (s) | Memory (MiB) |
    |-----------+-----------------+---------------+--------------|
    |    7.2    |      0.035      |     0.012     |     17.96    |
    |    7.1    |      0.050      |     0.012     |     20.98    |
    |    hhvm   |      0.088      |     0.234     |     88.93    |
    |    5.6    |      0.017      |     0.061     |     20.59    |
    '------------------------------------------------------------'
    

    source: 3v4l.org/uRTNh


  • $attributeSetId = $attributeSetCollection['attribute_set_id'];

    .------------------------------------------------------------. 
    | Version   | System time (s) | User time (s) | Memory (MiB) |
    |-----------+-----------------+---------------+--------------|
    |    7.2    |      0.128      |     0.011     |     17.55    | 
    |    7.1    |      0.131      |     0.011     |     20.86    | 
    |    hhvm   |      0.100      |     0.204     |     88.75    | 
    |    5.6    |      0.017      |     0.060     |     20.64    | 
    '------------------------------------------------------------'
    

source: 3v4l.org/8Yq6j

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.