2

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.

3

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.

1

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;
    }
  • I have used same code but facing the issue, it will not return proper value. – Suresh Chikani Jun 23 '18 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.