I have following code in di.xml
<type name="Magento\Catalog\Pricing\Price\TierPrice"> <plugin name="tierPriceSavePercentPlugin" type="Mycomp\Common\Plugin\TierPricePlugin" sortOrder="5"/> </type>
and the code in plugin file as following
<?php namespace Mycomp\Common\Plugin; use Magento\Catalog\Model\Product; use Magento\Customer\Api\GroupManagementInterface; use Magento\Customer\Model\Session; use Magento\Framework\Pricing\Adjustment\CalculatorInterface; use Magento\Framework\Pricing\Amount\AmountInterface; class TierPricePlugin { protected $scopeConfig; /** * @param Product $saleableItem */ public function __construct( Product $saleableItem, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; $this->priceInfo = $saleableItem->getPriceInfo(); } /** * Adding precision value from config setting * @param AmountInterface $amount * @return float */ public function aroundGetSavePercent(\Magento\Catalog\Pricing\Price\TierPrice $subject, \Closure $proceed, $amount) { /* System config value set for the Tier Price Display */ $precision = $this->scopeConfig->getValue('mycomp_common/tier_prices/precision', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $returnValue = $proceed($amount); echo $this->priceInfo->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)->getValue(); exit; } }
Following code line is always returning zero
echo $this->priceInfo->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE) ->getValue();
Ideally the above mentioned code line should return the Final price of the product.
What is wrong with my code?
Solved! Go to Solution.
You are currently trying to use Magento\Framework\Pricing\SaleableInterface for which there is no preference set so the ObjectManager is unable to find a concrete class that fulfils this interface. This leads to the error you are seeing when your TierPrice plugin class is being constructed.
Looking at Magento\Framework\Pricing\Price\AbstractPrice I believe you do not need SaleableInterface at all as you can get it from the TierPrice $subject.
So you would end up with:
public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; } public function aroundGetSavePercent(\Magento\Catalog\Pricing\Price\TierPrice $subject, \Closure $proceed, $amount) { $precision = $this->scopeConfig->getValue('mymodule_common/tier_prices/precision', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); return round( 100 - ((100 / $subject->getProduct()->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue()) * $amount->getBaseAmount()),$precision ); }
You are currently trying to use Magento\Framework\Pricing\SaleableInterface for which there is no preference set so the ObjectManager is unable to find a concrete class that fulfils this interface. This leads to the error you are seeing when your TierPrice plugin class is being constructed.
Looking at Magento\Framework\Pricing\Price\AbstractPrice I believe you do not need SaleableInterface at all as you can get it from the TierPrice $subject.
So you would end up with:
public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; } public function aroundGetSavePercent(\Magento\Catalog\Pricing\Price\TierPrice $subject, \Closure $proceed, $amount) { $precision = $this->scopeConfig->getValue('mymodule_common/tier_prices/precision', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); return round( 100 - ((100 / $subject->getProduct()->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue()) * $amount->getBaseAmount()),$precision ); }