Inject class into plugin
-
15-04-2021 - |
题
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?
解决方案
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;
}
}
其他提示
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;
}
}