我想将产品价格从当前货币转换为基础货币。例如,如果产品价格为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,则它将采用当前货币。