How to retrieve custom attribute set ID by name

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


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");