cancel
Showing results for 
Search instead for 
Did you mean: 

around plugin for getSavePercent method for TierPrices

SOLVED

around plugin for getSavePercent method for TierPrices

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?

---
Problem Solved Click Accept as Solution!:Magento Community India Forum
Tags (2)
1 REPLY

Re: around plugin for getSavePercent method for TierPrices

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
    );
}