Get Attribute ids by the Product id in Magento 2 using Attribute Set Finder Interface.
Full Path of Interface:
Magento\Catalog\Api\AttributeSetFinderInterface
Get attribute set ids by product ids using method name findAttributeSetIdsByProductIds(array $productIds). Pass Array of ids to fetch attribute id using the method.
<?php namespace Jesadiya\AttributeId\Model; use Exception; use Magento\Catalog\Api\AttributeSetFinderInterface; class GetAttributeIds { /** * @var AttributeSetFinderInterface */ private $attributeSetFinder; public function __construct( AttributeSetFinderInterface $attributeSetFinder ) { $this->attributeSetFinder = $attributeSetFinder; } /** * @param array * @return array * @throws Exception */ public function getAttributeIdbyProductIds(array $sku) { $attributeId = []; try { $attributeId = $this->attributeSetFinder->findAttributeSetIdsByProductIds($sku); } catch (Exception $exception) { throw new Exception($exception->getMessage()); } return $attributeId; } }
Now If you want to find product id for the only single product,
You have to set SKU as array,
$sku = [’24-MB01′];
$result = $this->getAttributeIdbyProductIds($sku);
Output:
Array of attribute id of the product.