3

I want to convert product price from the current currency to base currency.

For Example, if the product price is 5000 INR. I want to convert 5000 INR to USD.

I don't know which core function will be best used for. Can anyone give me examples? Any function which is used for convert price to current to base and base to current?

Purpose: I am adding a product to cart programmatically, if a current currency is USD then it's working properly but if I switch currency as INR, added product price convert INR amount to USD. So I want to convert INR amount to USD.

1
  • could you please help me . I have posted a new question (compare store currency & getCurrentCurrency) please check it on my profile . Commented Sep 26, 2020 at 13:47

4 Answers 4

5

Solution:

public function convertPrice($amount = 0, $store = null, $currency = null)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $priceCurrencyObject = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface'); 
    //instance of PriceCurrencyInterface
    $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
    //instance of StoreManagerInterface
    if ($store == null) {
        $store = $storeManager->getStore()->getStoreId(); 
        //get current store id if store id not get passed
    }
    $rate = $priceCurrencyObject->convert($amount, $store, $currency); 
    //it return price according to current store from base currency
 
    //If you want it in base currency then use:
    $rate = $this->_priceCurrency->convert($amount, $store) / $amount;
    $amount = $amount / $rate;
     
    return $amount;

}

Here, $amount is the amount to which you want to convert.

$store is the store id, from which store’s base currency you want to convert.

$currency is the currency whom you want to convert, if you passed null then it takes current currency.

7
  • @SureshChikani, I have added another solution, I hope it will work. Commented Jun 23, 2018 at 10:41
  • Also check by return $amount insted of return $priceCurrencyObject->round($amount); @SureshChikani Commented Jun 23, 2018 at 10:46
  • Hope your issue is resolved @SureshChikani Commented Jun 23, 2018 at 10:53
  • Let us continue this discussion in chat. Commented Jun 23, 2018 at 10:54
  • You should never use ObjectManager
    – Black
    Commented Jun 23, 2020 at 13:21
5

You can use below code in your helper or in a function

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Directory\Model\CurrencyFactory
     */
    protected $currencyFactory;

    /**
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory
    ){
        $this->storeManager = $storeManager;
        $this->currencyFactory = $currencyFactory;
    }


    public function convertToBaseCurrency($price)
    {
        //you can also pass INR code here insted of below current store currency
        $currentCurrency = $this->storeManager->getStore()->getCurrentCurrency()->getCode();

        $baseCurrency = $this->storeManager->getStore()->getBaseCurrency()->getCode();

        $rate = $this->currencyFactory->create()->load($currentCurrency)->getAnyRate($baseCurrency);
        $returnValue = $price * $rate;

        return $returnValue;
    }
2
  • I have used same code but facing the issue, it will not return proper value. Commented Jun 23, 2018 at 10:20
  • could you please help me . I have posted a new question (compare store currency & getCurrentCurrency) please check it on my profile . Commented Sep 26, 2020 at 13:48
2

Magento itself provides all the convert configuration we only need to use it.

The simplest way to archive this!

class Price 
{

    protected $_priceHelper;


    public function __construct(
        \Magento\Framework\Pricing\Helper\Data $priceHelper,

    ) {
        $this->_priceHelper = $priceHelper;
    }

    public function yourFunction(){
        /* Your code  */
        $this->convertToBaseCurrency(100);
    }

    /**
     * @param $price
     * @return int|float
     */
    public function convertToBaseCurrency($price)
    {
        $formattedCurrencyValue = $this->_priceHelper->currency($price, false, false);
        return $formattedCurrencyValue;
    }
}
1
  • You also get the price with currency code or without just going to currency function in priceHelper. Commented Jan 23, 2020 at 11:44
0

Try not to use Objectmanager. It is just for fast implementation

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// if 1 USD = 2 INR 
// and current store currency is USD
$amount = 45;
$priceCurr = $objectManager->create('\Magento\Framework\Pricing\PriceCurrencyInterface');
echo $priceCurr->convertAndFormat($amount, false, 3, null, "INR"); 
// output $90
  1. amount
  2. include container set false or true ( < span >$90 < /span>).
  3. set precision (3).
  4. Specify scope.
  5. Convert currency to (INR), By default this will convert to current store currency from base currency.

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.