Question

I was able to add some additional information to the products in the category and the search-result page with a plugin. Unfortunately it doesn't work for the widgets (product list widgets, related products etc.).

My di.xml:

<?xml version="1.0"?>
<config>
    <type name="Magento\Catalog\Block\Product\ListProduct">
      <plugin name="add_custom_block" type="MyVendor\MyModule\Plugin\FreeShipping" sortOrder="1" disabled="false" />
    </type>
</config>

My Plugin/FreeShipping.php:

<?php
namespace MyVendor\MyModule\Plugin;

class FreeShipping
{
    public function afterGetProductPrice(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result,
        \Magento\Catalog\Model\Product $_product
    ) {
        $shippingperitem=$_product->getShippingPeritem();
        $freeshipping=false;
        $storpickuponly=$_product->getStorepickupOnly();
        if (floatval($shippingperitem)<=0.000001) {
            $freeshipping=true;
        }
        $productId = $_product->getId(); //parent product id..
        $_configChild = $_product->getTypeInstance()->getUsedProducts($_product);
        $getChildId = [];

        foreach ($_configChild as $child) {
            if (floatval($child->getShippingPeritem())>0.000001) {
                $freeshipping=false;
            }
            if ($child->getStorepickupOnly()==true) {
                $storpickuponly==true;
            }
        }
        $shippinginfo="";
        if ($freeshipping==true && $storpickuponly==false) {
            $shippinginfo = "<div id='free-shipping-banner active'>";
            $shippinginfo .= __("Free shipping");
            $shippinginfo .= "</div>";
        } else {
            $shippinginfo = "<div id='free-shipping-banner'>";
            $shippinginfo .= "</div>";
        }
        return $result.$shippinginfo;
    }
}

Do I need to specify another plugin for another class?

Was it helpful?

Solution

you can try plugin for the Magento\Catalog\Block\Product\AbstractProduct class. All other class like Crosssell Upsell NewWidget are extending this class and AbstractProduct class has getProductPrice() as public function so you can make plugin.

UPDATE

In widget phtml file getProductPriceHtml() used so you have to make plugin for the getProductPriceHtml()

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