How can we retrieve the attribute set name for a product. I want to use it on product detail and listing page.
1 Answer
We can use \Magento\Eav\Api\AttributeSetRepositoryInterface
to get attribute set name.
Detail Page
We need to override the \Magento\Catalog\Block\Product\View
block. Inject this class on the constructor
/** @var \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet **/
protected $attributeSet;
public function __construct(
......
\Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet
......
) {
......
$this->attributeSet = $attributeSet;
}
//Build method to get attribute set
public function getAttributeSetName() {
$product = $this->getProduct();
$attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
return $attributeSetRepository->getAttributeSetName();
}
Now, we can call in product detail page: $block->getAttributeSetName();
Listing page
We need to override \Magento\Catalog\Block\Product\ListProduct
block
/** @var \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet **/
protected $attributeSet;
public function __construct(
......
\Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet
......
) {
......
$this->attributeSet = $attributeSet;
}
public function getAttributeSetName($product) {
$attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
return $attributeSetRepository->getAttributeSetName();
}
We can call $block->getAttributeSetName($_product)
.
-
$attributeSet and $product are undefined variables, I am very new to magento2 and I am not able to understand what exactly I need to write Sep 24, 2016 at 4:58
-
-
I'm trying to implement it in product list block, but it is not working as exact, making some modifications Sep 24, 2016 at 5:36
-
-
You can update your answer with current issue when following my answer. Sep 24, 2016 at 5:38