How can we retrieve the attribute set name for a product. I want to use it on product detail and listing page.
Magento 2 – Get Attribute Set Name in Product Listing and Detail Page
attribute-setmagento2product
Related Solutions
Magento has the ability to display the special price when it is set on a product:
This is styled in the Magento frontend with the markup
<p class="special-price">
<span class="price-label">Special Price</span>
<span class="price" id="product-price-423">$224.00</span>
</p>
So using the following CSS should allow you to style it how you see fit:
.special-price .price {
/* your rules here */
}
I suggest you should use Magento\Eav\Api\AttributeManagementInterface to get all attributes by attribute set id
To do this, we need to override Magento\Catalog\Block\Product\ListProduct class and inject this class into the constructor. See more detail here:
- app/code/YourVendor/YourModule/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="YourVendor\YourModule\Block\Catalog\Product\ListProduct" />
</config>
- app/code/YourVendor/YourModule/Block/Catalog/Product/ListProduct.php
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Url\Helper\Data;
use Magento\Eav\Api\AttributeManagementInterface;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
private $attributeManagement;
/**
* @param Context $context
* @param PostHelper $postDataHelper
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @param Data $urlHelper
* @param AttributeManagementInterface $attributeManagement
* @param array $data
*/
public function __construct(
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
AttributeManagementInterface $attributeManagement,
array $data = []
) {
$this->attributeManagement = $attributeManagement;
parent::__construct(
$context,
$postDataHelper,
$layerResolver,
$categoryRepository,
$urlHelper,
$data
);
}
/**
* Get Attributes by Attribute Set ID
* @param $attributeSetId
* @return \Magento\Eav\Api\Data\AttributeInterface[]
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getAttributesBySetId($attributeSetId)
{
$attributes = $this->attributeManagement->getAttributes(\Magento\Catalog\Model\Product::ENTITY,
$attributeSetId);
return $attributes;
}
}
- app/design/frontend/YourCustomTheme/templates/product/list.phtml
........
<?php $attributes = $block->getAttributesBySetId($_product->getAttributeSetId()); ?>
<?php
foreach ($attributes as $attribute) {
echo ($attribute->getFrontendLabel()).':::';
}
?>
..........
Hopefully this will solve your issue
Regards
Best 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 constructorNow, we can call in product detail page:
$block->getAttributeSetName();
Listing page
We need to override
\Magento\Catalog\Block\Product\ListProduct
blockWe can call
$block->getAttributeSetName($_product)
.