Magento – Magento 2 : How to get the product attributes based on attribute set id on listing page

attribute-setattributesmagento2product-attribute

I am trying to get product attribute in product listing page based on attribute set id but its not working properly. can anyone help on this.

 $attributes = $_product->getAttributes();

  foreach ($attributes as $attribute) {
// var_dump($attribute->getAttributeModel());
   $value = $attribute->getFrontend()->getValue($_product);
   if ($attribute->getIsVisibleOnFront()) {
       echo $attribute->getFrontendLabel().' : '.$attribute->getFrontend()->getValue($_product) . '
'; } }

Best Answer

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:

  1. 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>
  1. 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;
    }
}
  1. 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

Related Topic