6

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) . '
'; } }
3
  • have you set for attributes, //Product attributes -> Storefrontend Properties -> Used in Product Listing Set To Yes from admin? Commented Dec 9, 2016 at 12:16
  • yes i have already set that.my attributes are also coming but need to show attributes based on attributeset.
    – rajat kara
    Commented Dec 9, 2016 at 14:27
  • pls refer this - magento.stackexchange.com/questions/137917/…
    – sandip
    Commented Jan 21, 2017 at 19:35

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.