3

I'm trying to create plugin for getProductPrice but \Magento\Catalog\Block\Product\ListProduct Block Class not generate Interceptor Class like Magento\Catalog\Block\Product\ListProduct\Interceptor.

My 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 name="addSimplePriceUnit" type="MagArs\PriceUnit\Plugin\Product\Simple"/>
    </type>
</config>

My plugin Class

namespace MagArs\PriceUnit\Plugin\Product;
    class Simple {
        public function beforeGetProductPrice(\Magento\Catalog\Block\Product\ListProduct $subject, $result){
            return $result;
        }
    }

moduel.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MagArs_PriceUnit" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

I am using magento enterprise version 2.2.5

7
  • Can you please add your di.xml and plugin code here Commented Oct 15, 2018 at 9:59
  • @RutveeSojitra i update my question please look into my question. Commented Oct 15, 2018 at 10:05
  • Please share the full code of your plugin class and module.xml as well Commented Oct 15, 2018 at 10:15
  • @RutveeSojitra i update my question. Commented Oct 15, 2018 at 10:19
  • You want to get specific product price in plugin ? Commented Oct 15, 2018 at 10:41

3 Answers 3

3

I solve this by using virtual types plugins.

I just add in my di.xml file like this.

<?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 name="addSimplePriceUnit" type="MagArs\PriceUnit\Plugin\Product\Simple" />
    </type>
    <virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
        <plugin name="addSimplePriceUnitVirtualPlugin"
                type="MagArs\PriceUnit\Plugin\Product\Simple" />
    </virtualType>
</config>

and plugin class here

class Simple extends AbstractPriceUnit {
    public function afterGetProductPrice(\Magento\Catalog\Block\Product\ListProduct $product, $result){
        return $result."<h2>some html</h2>";
    }
}

I found the solution from the following link

1
  • 1
    I had exactly the same problem, this helped me a lot! Thanks.
    – xpoback
    Commented Nov 29, 2020 at 12:52
0

Please try replacing your code with:

class Simple {
     public function 
    beforeGetProductPrice(\Magento\Catalog\Block\Product\ListProduct $subject, \Magento\Catalog\Model\Product $product){
          //you have to add your logic
      }
        }

Check after running below this commands:

rm -rf generated/*

php bin/magento c:c

php bin/magento c:f
5
  • you just add \Magento\Catalog\Model\Product $product in parameters and return the $result which make no sense. can you please elaborate this. Commented Oct 15, 2018 at 10:12
  • I have edited @ArshadMuhammad please check what you want to add custom logic which you have to add in method Commented Oct 15, 2018 at 10:37
  • And add in question if you are facing any error Commented Oct 15, 2018 at 10:38
  • I am just updating my version 2.2.5 to 2.2.6 let see what happens Commented Oct 15, 2018 at 11:08
  • not working with 2.2.6. Did you test this your self. Commented Oct 15, 2018 at 11:33
0

There is no issue you just need to delete generated folder and then check.

If the cache is enabled then you need to refresh cache using

php bin/magento cache:clean

Update your di.xml with this code,

<?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 name="addSimplePriceUnit" sortOrder="1" disabled="false" type="MagArs\PriceUnit\Plugin\Product\Simple"/>
    </type>
</config>

Create Plugin MagArs\PriceUnit\Plugin\Product\Simple.php

<?php

namespace MagArs\PriceUnit\Plugin\Product;

class Simple
{
    public function beforeGetProductPrice(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result
    ) {
        return $result;
    }

}

After this you can check your productlist page.

4
  • did you test the code see the generated interceptor class. I did refresh cache and di compile but no luck Commented Oct 15, 2018 at 10:56
  • Did you remove generated folder? Commented Oct 15, 2018 at 10:59
  • yes i remove generated folder. I am using 2.2.5 version Commented Oct 15, 2018 at 11:02
  • did you test this code your self. Commented Oct 15, 2018 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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