1

On the Product view page you have a default tab 'More information'. In this tab you see attributes from the default Attribute Set.

I want to show those attributes on the Category list page, under the Product Titel for example.

How do I achieve this?

1 Answer 1

1

To do this you first need to create a new module.

Add this block class to your module: /app/code/Your/Module/Block/Product/View/Attributes.php

<?php

namespace Your\Module\Block\Product\View;


use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;

class Attributes extends  \Magento\Catalog\Block\Product\View\Attributes
{
    protected $_productRepo;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        PriceCurrencyInterface $priceCurrency,
        ProductRepositoryInterface $productRepository,
        array $data = []
    )
    {
        $this->_productRepo = $productRepository;

        parent::__construct($context, $registry, $priceCurrency, $data);
    }

    public function setProductById($productId)
    {
        $this->_product = $this->_productRepo->getById($productId);

        return $this;
    }
}

This simply adds a method to the stock Attributes block that allows us to specify which product to use.

Now copy \vendor\magento\module-catalog\view\frontend\templates\product\list.phtml to your module in this location {module_dir}\view\frontend\templates\product\list.phtml. Add this code where you want the attributes to appear (line 72 for after the title).

<?php echo $block->getChildBlock('attribute.set')->setProductById($_product->getId())->toHtml() ?>

Lastly create the file {module_dir}\view\frontend\layout\catalog_category_view.xml with this contents:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="category.products.list">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Your_Module::product/list.phtml</argument>
            </action>
            <block class="Your\Module\Block\Product\View\Attributes" name="attribute.set" template="Magento_Catalog::product/view/attributes.phtml"/>
        </referenceBlock>
    </body>
</page>

Tested in 2.1.2

2
  • Great, thanks for your explanation. So first I have to built a module like described here isn't it?
    – meez
    Commented Dec 13, 2016 at 4:55
  • Correct, that's what you need to do. Commented Dec 13, 2016 at 12:58

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.