0

I created an plugin for afterGetProductPriceHtml() and it works so far. My di.xml:

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

And the plugin:

<?php
namespace MyVendor\MyModule\Plugin;

class Streichpreis
{
    public function afterGetProductPriceHtml(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $result,
        \Magento\Catalog\Model\Product $_product
    ) {
        $streichpreistyp=$_product->getAttributeText('streichpreistyp');
        $basePrice = $_product->getPriceInfo()->getPrice('regular_price');
        $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getValue();
        $specialPrice = $_product->getPriceInfo()->getPrice('special_price')->getValue();

        $out="";
        if ($streichpreistyp!="" && $specialPrice < $regularPrice) {

            $displayregular = number_format($regularPrice, 2, ',', '.')." €";
            $streichpreistyp='<span class="streichpreistyp">'.$streichpreistyp.'</span>';
            $displayregular='<span class="streichpreis"><span class="price">'.$displayregular.'</span><span>';
            $percent=(($regularPrice - $specialPrice) / $regularPrice) * 100;
            $percent=round($percent);
            $percent='<span class="percent-of">-'.$percent.'</span>';
            $out=$streichpreistyp.$displayregular.$percent;
        }
        return $result.$out;
    }
}

The part, that I am not really proud of is this:

$displayregular = number_format($regularPrice, 2, ',', '.')." €";

How can I incect another class into the plugin? To get the formatted price, I need to use

\Magento\Framework\Pricing\PriceCurrencyInterface

to format the price, but I am not able to add a __construct function into the plugin. What is the correct way to use another class in a plugin?

2 Answers 2

1

You can use below code.

<?php
namespace MyVendor\MyModule\Plugin;

class Streichpreis {
    public function __construct(
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency) {
        $this->priceCurrency = $priceCurrency;

    }
    public function afterGetProductPriceHtml(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $result,
        \Magento\Catalog\Model\Product $_product
    ) {
        $streichpreistyp = $_product->getAttributeText('streichpreistyp');
        $basePrice = $_product->getPriceInfo()->getPrice('regular_price');
        $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getValue();
        $specialPrice = $_product->getPriceInfo()->getPrice('special_price')->getValue();

        $out = "";
        if ($streichpreistyp != "" && $specialPrice < $regularPrice) {

            $displayregular = number_format($regularPrice, 2, ',', '.') . " €";
            $streichpreistyp = '<span class="streichpreistyp">' . $streichpreistyp . '</span>';
            $displayregular = '<span class="streichpreis"><span class="price">' . $displayregular . '</span><span>';
            $percent = (($regularPrice - $specialPrice) / $regularPrice) * 100;
            $percent = round($percent);
            $percent = '<span class="percent-of">-' . $percent . '</span>';
            $out = $streichpreistyp . $displayregular . $percent;
        }
        return $result . $out;
    }
}
1

You can use constructor like this:

<?php
namespace MyVendor\MyModule\Plugin;

class Streichpreis
{
public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    ) {
        parent::__construct( $context, $data );
    }

    public function afterGetProductPriceHtml(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $result,
        \Magento\Catalog\Model\Product $_product
    ) {
        $streichpreistyp=$_product->getAttributeText('streichpreistyp');
        $basePrice = $_product->getPriceInfo()->getPrice('regular_price');
        $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getValue();
        $specialPrice = $_product->getPriceInfo()->getPrice('special_price')->getValue();

        $out="";
        if ($streichpreistyp!="" && $specialPrice < $regularPrice) {

            $displayregular = number_format($regularPrice, 2, ',', '.')." €";
            $streichpreistyp='<span class="streichpreistyp">'.$streichpreistyp.'</span>';
            $displayregular='<span class="streichpreis"><span class="price">'.$displayregular.'</span><span>';
            $percent=(($regularPrice - $specialPrice) / $regularPrice) * 100;
            $percent=round($percent);
            $percent='<span class="percent-of">-'.$percent.'</span>';
            $out=$streichpreistyp.$displayregular.$percent;
        }
        return $result.$out;
    }
}
2
  • I didnt test your answer, but the answer of rakesh-varma worked for me.
    – Mario
    Mar 25, 2021 at 11:42
  • kindly up-vote my answer Mar 25, 2021 at 12:18

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.