7

We have overwritten Magento\Catalog\Block\Product\ListProduct to be able to check if displayed products match a certain category.

We have set the preference in our module /etc.di.xml like this:

<preference for="Magento\Catalog\Block\Product\ListProduct"
            type="Vendor\ModuleName\Block\Catalog\ListProduct"/>

And we have overwritten the ListProduct Class like this:

namespace Vendor\ModuleName\Block\Catalog;

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
protected $_customerSession;
protected $categoryFactory;

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = [],
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->_customerSession = $customerSession;
    $this->categoryFactory = $categoryFactory;
    parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
}

protected function _getProductCollection() {
...
}
...

Problem:

The problem is that this class overrides ListProduct on all category pages except on search result page, where it still uses Magento\Catalog\Block\Product\ListProduct and not te overwritten one.

How can we overwritte ListProduct class for search results page?

3 Answers 3

3

May be this code is reason for it:

vendor\magento\module-catalog-search\etc\frontend\di.xml

<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Magento\Catalog\Block\Product\ListProduct">
    <arguments>
        <argument name="catalogLayer" xsi:type="object">Magento\Catalog\Model\Layer\Search</argument>
    </arguments>
</virtualType>

please try here with your overridden class:

<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Vendor\ModuleName\Block\Catalog\ListProduct">
2
0

For people wondering about this, Rutvee Sojitra solution works well ! on your module you need to create a di.xml in /etc/frontend

and parse this code :

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="VENDOR\MODULE-NAME\Block\CatalogSearch\ListProduct">
        <arguments>
            <argument name="catalogLayer" xsi:type="object">Magento\Catalog\Model\Layer\Search</argument>
        </arguments>
    </virtualType>
</config>  
0

i solve the problem by using preference but not virtualtype

<preference for="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="VENDOR\YOURMOUDLE\Block\Product\ListProduct" />

Your Answer

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

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