TransWikia.com

How to override Virtual Type class in Magento 2

Magento Asked by nuwaus on November 8, 2021

How to override VirtualType block in Magento 2, I want to override following virtual type block with my own block,

<virtualType name="MagentoCatalogSearchBlockSearchResultListProduct" type="MagentoCatalogBlockProductListProduct">
   <arguments>
       <argument name="catalogLayer" xsi:type="object">MagentoCatalogModelLayerSearch</argument>
   </arguments>
</virtualType>

So I tried to override it from my custom module di.xml like this,

<preference for="MagentoCatalogBlockProductListProduct" type="MyVendorBlockProductListProductSearch" />

But it does not work.

So, what is the proper way of overriding a virtual type class in Magento 2?

4 Answers

Just because I couldnt find it anywhere else

I did override:

<preference for="MagentoCatalogBlockProductListProduct" type="MyVendorBlockProductListProduct" />
<preference for="MagentoCatalogSearchBlockSearchResultListProduct" type="MyVendorBlockProductListProduct" />

So now, I can extend the base ListProduct.php Block either override functionality or extend it.

then the layout My/Vendor/view/frontend/layout/catalogsearch_result_index.xml

<body>
    <referenceBlock name="search_result_list">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">My_Vendor::product/list.phtml</argument>
        </action>
    </referenceBlock>
</body>

so in this layout update file Im also overriding the template and I can use the new methods added in my first preference.

Answered by Ezri Hiccup on November 8, 2021

You have to use:

<preference for="MagentoCatalogSearchBlockSearchResultListProduct" type="VendorModuleBlockProductListProductSearch" />

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:

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="search_result_list">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Magento_Catalog::product/list.phtml</argument>
        </action>
    </referenceBlock>
</body>

Answered by Bartłomiej Szubert on November 8, 2021

I'm not sure how exactly the xml configurations are merged and sorted, but this type of problem usually comes down to sequence of the modules being loaded.

I would try adding sequence to your module.xml :

<sequence>
        <module name="Magento_CatalogSearch"/>
</sequence>

The preference described by Raphael above is working fine for me.

Answered by Langley on November 8, 2021

If you want to override the virtual type in your example, you need to use:

<preference for="MagentoCatalogSearchBlockSearchResultListProduct" type="MyVendorBlockProductListProductSearch" />

Instead of:

<preference for="MagentoCatalogBlockProductListProduct" type="MyVendorBlockProductListProductSearch" />

In your example, you override the original type and not the virtual type, that's the reason why it's not working.

Answered by Raphael at Digital Pianism on November 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP