0

I want to add custom code in my catalog search page. I don't want to override product/list.phml file .

But, Just display my custom code in catalog search result page after product price. How to add it?

enter image description here

I have used this plugin code.It's working in only list product. But, when I search that product I can't find content of my custom phtml.

di.xml :

<type name="Magento\Catalog\Block\Product\ListProduct">
        <plugin name="vendor-module-category-timer" type="VendorName\ModuleName\Plugin\CategoryTimer" sortOrder="16"/>
    </type>

CategoryTimer.php :

public function aroundGetProductDetailsHtml(
    \Magento\Catalog\Block\Product\ListProduct $subject,
    \Closure $proceed,
    \Magento\Catalog\Model\Product $product
) {
    return "call2";
}
  • my guess is the data you render in your plugin are not available in search page. I'd need to see the code that works to give a better answer? – Herve Tribouilloy Nov 24 '18 at 9:03
1

You need to create <virtualType> plugin for that. Try this below code :

Add this code in your di.xml file :

<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
        <plugin name="vendorName_moduleName_VirtualPlugin"
                type="vendorName\moduleName\Plugin\MyPlugin" />
    </virtualType>

And then you need to create this below plugin file :

public function aroundGetProductDetailsHtml(
    \Magento\Catalog\Block\Product\ListProduct $subject,
    \Closure $proceed,
    \Magento\Catalog\Model\Product $product
) {
    return "call2";
}

I hope it will helpful for you..

  • It's not working – Dharmik Ponda Nov 27 '18 at 17:56
  • Sorry.. I forgot to change plugin code. – Rohan Hapani Nov 27 '18 at 18:00
  • Please check my updated answer. – Rohan Hapani Nov 27 '18 at 18:00
  • Okay. I'm checking and inform you. – Dharmik Ponda Nov 27 '18 at 18:09
2

Create Magento_CatalogSearch folder under your custom theme

Add Magento_CatalogSearch\layout\catalogsearch_result_index.xml and put below code:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="catalogsearch.product.addto">
            <block class="Magento\Framework\View\Element\Template" name="product.data.custom" template="Magento_CatalogSearch::custom.phtml" after="catalogsearch.product.addto" />
        </referenceBlock>
    </body>
</page>

Add custom.phtml file with your content/text/code

Magento_CatalogSearch/templates/custom.phtml

Change Magento_CatalogSearch to YoucPackage_YourModule in case you are trying code in custom module.

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.