4

This question already has an answer here:

I'm trying to override the list product page. My objective is add certain information on each product above the add to cart button for example.

I already did with the single page product, just overrided catalog_product_view.xml and added my block where I wanted.

But with list product I'm watching the catalog_category_view.xml and I really don't know how to it.

<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">

product/list.phtml This template is doing all the work. So I only think to copy this template on my vendor's module and write my stuff where I want.

Is this the propper way? Can't I only write my block and indicate magento to use it? I don't like to copy all this 'core' template and write inside modifying it

marked as duplicate by Raphael at Digital Pianism, Rakesh Jesadiya, Murtuza Zabuawala, 7ochem, Keyul Shah Dec 5 '16 at 17:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

you can rewrite the block

Step # 1 – Create a di.xml file in a folder FME/Test/etc

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Catalog\Block\Product\ListProduct" type="FME\Test\Block\Rewrite\Product\ListProduct" />
</config>

Step # 2 - The next step to overriding Magento2 block is to create a ListProduct.php Block file in the folder FME/Test/Block/Rewrite/Product

<?php
    /**
    * Rewrite Product ListProduct Block
    * @category    FME
    * @package     FME_Test
    * @author      Fme Extensions Development Company
    */

    namespace FME\Test\Block\Rewrite\Product;
    class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
        {
            public function __construct()
                {
                    echo "you can do this in block"; die();
                }
        }

Reference

override phtml file

create checkout_cart_index.xml inside :

app/code/Webkul/Hello/view/frontend/layout folder

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance dc" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="checkout.cart">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Webkul_Hello::cart.phtml</argument>
        </action>
    </referenceBlock>
</page>

Here,

checkout.cart : is block name, whose template file you want to override.

Webkul_Hello::cart.phtml : is a template file of your module.

Now, create cart.phtml file at location:

app/code/Webkul/Hello/view/frontend/templates folder

<h1>Shopping Cart</h1>
//write your stuff here

reference

  • Following this solution, I encounter that I have to do all the work in the function: getProductDetailsHtml adding my stuff and I don't have so much control. How can I add my custom template in this case? – slayerbleast Dec 5 '16 at 10:45
  • @slayerbleast doing module or theme? – Qaisar Satti Dec 5 '16 at 10:53
  • for module, on app/code/... – slayerbleast Dec 5 '16 at 10:56
  • @slayerbleast i added the example for that too... – Qaisar Satti Dec 5 '16 at 11:00
6

You have to just write below xml code in your module file, In your module layout file,

app/code/Vendor/Modulename/view/frontend/layout/catalog_category_view.xml File,

<?xml version="1.0"?>
<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>
            <!-- for magento catalog list overrider -->
            <referenceBlock name="category.products.list" template="Vendor_Modulename::product/list.phtml" />          
    </body>
</page>
  • You can override using above method, please let me know if you have any issue – Rakesh Jesadiya Dec 5 '16 at 11:00
  • Unable to over-ride class using this way. – Liam Mitchell Sep 20 at 2:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.