Question

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:

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

Was it helpful?

Solution 2

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:

$blockStore->myOwnMethod();

OTHER TIPS

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">
    <body>
        <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>
            <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"
                       template="Magento_Catalog::product/list/addto/compare.phtml"/>
            </block>
            <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"/>
            </block>
            <action method="setToolbarBlockName">
                <argument name="name" xsi:type="string">product_list_toolbar</argument>
            </action>
        </block>
    </body>
</page>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top