How to override VirtualType block in Magento 2, I want to override following virtual type block with my own block,
<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>
So I tried to override it from my custom module di.xml
like this,
<preference for="Magento\Catalog\Block\Product\ListProduct" type="My\Vendor\Block\Product\ListProductSearch" />
But it does not work.
So, what is the proper way of overriding a virtual type class in Magento 2?
Best Answer
You have to use:
as Raphael mentioned.
To make this singular block working properly, you need also to add
view/frontend/layout/catalogsearch_result_index.xml
file with following content: