5

I want to customize return value of $block->getProductDetailsHtml($_product) (in list.phtml) using plugin method, not overrite list.phtml

3

Block Magento\Catalog\Block\Product\ListProduct extends Magento\Catalog\Block\Product\AbstractProduct. AbstractProduct have getProductDetailsHtml($_product) so you need to add plugin for AbstractProduct

Vendor/Module/etc/frontend/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

        <type name="Magento\Catalog\Block\Product\AbstractProduct">
            <plugin name="vendor.module.category.products.list" type="Vendor\Module\Plugin\Block\Product\AbstractProduct" />
        </type>

    </config>

Vendor/Module/Plugin/Block/Product/AbstractProduct.php

    <?php

    namespace Vendor\Module\Plugin\Block\Product;

    class AbstractProduct
    {
        public function afterGetProductDetailsHtml(
            \Magento\Catalog\Block\Product\AbstractProduct $subject,
            $result
        ) {
             // $result contains the original values
             // do the stuff to change $results 
             // easy for your reference I just pass below string
             return '<b>test</b>';
        }
    }       

Note: We can use after, before, around so you need to decide which is the best suite for you. Refer this link for how to use.

I have checked, it works

easy for your reference

  • 2
    di.xml should be under: Vendor/Module/etc/frontend/di.xml – Khoa TruongDinh Jul 19 '17 at 6:44
  • yep you right @KhoaTruongDinh, I'll update – Bilal Usean Jul 19 '17 at 6:44
  • is this still working, i have tried with 2.2.3.But does not working. – Amit Bera Apr 29 '18 at 14:32
1

Vendor/Module/etc/di.xml

<type name="Magento\Catalog\Block\Product\ListProduct">
    <plugin name="block-product-list" type="Vender\Module\Plugin\ProductList"/>
</type>

Vendor/Module/Plugin/ListProduct.php

<?php
namespace Vender\Module\Plugin;

class ProductList
{   

    public function aroundGetProductDetailsHtml(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        \Closure $proceed,
        \Magento\Catalog\Model\Product $product
    ) {
        $result = $proceed($product);          
        return $result . 'Add DetailsHtml code here';       
    }               
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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