If I need to create a method under Magento\Catalog\Test\Block\Product\ListProduct Class

This class has methods such as: getProductItem,getProductNames,getProductsCount,getSortByValues

I Would like to make myOwnMethod in my custom module without overwriting it the class ListProduct and use in:


  • You need to change that block class in the XML and then point to your custom block class which extends the Magento\Catalog\Test\Block\Product\ListProduct block class (if you want to keep functionality) – André Ferraz Nov 29 '18 at 16:44

The first thing you want to do is create a class which extends ListProduct. In your custom module's Vendor/Module/Block/Product/ListProduct.php

class ListProduct extends \Magento\Catalog\Block\ListProduct {

    public function myOwnMethod() { … }


And then, you need to replace the references to that block in the layout. I believe the layout that uses this block is catalog_category_view, so in your module's Vendor/Module/view/frontend/layout/catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="category.products.list" remove="true"/>
        <block class="Vendor\Module\Block\Product\ListProduct" name="category.products.list.extend" as="product_list" template="Vendor_Module::product/list.phtml">
            <container name="category.product.list.additional" as="additional" />
            <block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
                <block class="Magento\Framework\View\Element\Template" name="category.product.type.details.renderers.default" as="default"/>
            <block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="category.product.addto" as="addto">
                <block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare"
                       name="category.product.addto.compare" as="compare"
            <block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
                <block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
            <action method="setToolbarBlockName">
                <argument name="name" xsi:type="string">product_list_toolbar</argument>

Notice that this is nearly a perfect copy of the old catalog_category_view.xml but the previous block was removed, and replaced with a new block that has a different class and template

| improve this answer | |
  • I don't want to remove the current category.products.list or change template="Vendor_Module::product/list.phtml" in my vendor all I want is to be able to have a method available to use in any Theme list.phtml without having to add directly to` vendor\magento\module-catalog\Block\Product\ListProduct.php` file. Like appending a new Method onto \Magento\Catalog\Block\ListProduct ready to be used whenever is required. – Juliano Vargas Nov 29 '18 at 20:56

I think this is probably the only solution I've got welcome new one's please!

Create Block :Vendor/Module/Block/Product/ListProduct.php

in any Theme: .../app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/list.phtml

Call Block:

$blockStore = $this->getLayout()->createBlock('Vendor/Module/Block/Product/ListProduct');

Use your custom method from Block like so:

| improve this answer | |

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.