Magento-2如何将价格从当前货币转换为基础货币

我想将产品价格从当前货币转换为基础货币。例如,如果产品价格为5000 INR。我想将5000 
INR转换为USD

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;

}

在这里,$amount是您要转换为的金额。

$store 是商店ID,您要从中转换商店的基础货币。

$currency 是要转换的货币,如果您传递null,则它将采用当前货币。

赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

相关文章

Please Login to comment