3

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?

2

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">

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.