0

I want to override category.products.list to display 1 custom attribute value to each product in list.

and i know i can override product/list.phtml to my module and there i can customize code in loop to display that attribute value.

but in case tomorrow any other module override list.phtml then my changes are gone.

so is there any way, that any module override list.phtml my code to display custom attribute value to each product should appear?

  • You can override product/list.phtml in your theme. – KKR Aug 30 at 7:11
1

You can use pluginenter link description here on Magento\Catalog\Block\Product\List::getTemplate().

If you using after plugin on getTemplate then if anything modification comes from Other 3rd party modules using Layout will not be affected as template file location of Block finally comming from this method.

Plugin Class

<?php

namespace StackExchange\Magento\Plugin\Magento\Catalog\Block\Product;

class ListProduct
{

    public function afterGetTemplate(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result
    ) {

        if($subject->getNameInLayout() == 'category.products.list'){
            // StackExchange_Magento::product/list.phtml  is your template file 
            return "StackExchange_Magento::product/list.phtml";
        }
        return $result;
    }
}

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Product\ListProduct">
        <plugin disabled="false" name="StackExchange_Magento_Plugin_Magento_Catalog_Block_Product_ListProduct" sortOrder="10" type="StackExchange\Magento\Plugin\Magento\Catalog\Block\Product\ListProduct"/>
    </type>
</config>

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.