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?