Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento 2: currency conversion

19 Jan 2022 » magento2, php
<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', -1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$currencyFactory = $objectManager->get(\Magento\Directory\Model\CurrencyFactory::class);
$curentCurrencyCode =  $storeManager->getStore()->getCurrentCurrency()->getCode();
$rate = $currencyFactory->create()
            ->load($curentCurrencyCode)
            ->getAnyRate('INR');

$convertedPrice = 100 * $rate;

   echo $convertedPrice;


die(__FILE__."::".__LINE__);

Thanks.