I want to add some custom code in my catalog category view page. I don't want to override product/list.phml file . But just display my custom code in catagory view page.
How to add it?
Create catalog_category_view.xml
app/code/Vendor/Module/view/frontend/layout/catalog_category_view.xml
<?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>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="custom.block" template="Vendor_Module::myCustomFile.phtml" />
</referenceContainer>
</body>
</page>
Now create myCustomFile.phtml and place your code
app/code/Vendor/Module/view/frontend/templates/myCustomFile.phtml
<?php
echo "This is my custom file for product list page";
?>
Please try the below code.
<?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>
<referenceContainer name="content">
<block after="product.info.price" class="Vendor/Module/Block/Module" name="extra" template="Vendor_Module::extra.phtml" />
</referenceContainer>
</body>
</page>
Add the below code in calatog_category_view.xml
and replace the block name and template name with your custom block and template name
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="category.product.addto" as="addto">
<block as="product_list_blockname"
class="<vendor>\<module>\Block\Product\<ClassName>"
name="product.list.<ClassName>"
template="<template path>"/>
</block>
</block>
</block>
</referenceContainer>
</body>
</page>