0

Hi we have one US website and one CA website

However, for some reason I have to find a Magento function for currency conversion. I have CAD want to transfer to USD in the US store view. I did set up the Magento currency conversion rate table as well.

So, how do I call this function out to do the conversion?

THanks

1

To convert currency you can use Magento\Framework\Pricing\PriceCurrencyInterface.

Module’s block class:

<?php
namespace VendorName\ModuleName\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;
    protected $_currency;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\Currency $currency,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrencyInterface,
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;        
        $this->_priceCurrencyInterface = $priceCurrencyInterface;
        parent::__construct($context, $data);
    }


    /**
     * Covert currency code
     *
     * @return It return price according to current store from base currency
     */ 
    public function convertPrice($amount,$currency)
    {
        $store = $this->_storeManager->getStore()->getStoreId();
        return $priceCurrencyObject->convert($amount, $store, $currency);
    }
}
?>

To get other currncy info storewise:

    // retutn Currency Symbol:
    $this->_currency->getCurrencySymbol();

    // return store base currency code
    $this->_storeManager->getStore()->getBaseCurrencyCode();

    // return default store currency code
    $this->_storeManager->getStore()->getDefaultCurrencyCode();

    // return array of installed currencies for the scope
    $this->_storeManager->getStore()->getAllowedCurrencies();

    // return current currency rate
    $this->_storeManager->getStore()->getCurrentCurrencyRate();

Template (.phtml) file:

echo $block->convertPrice($amount,$currency);

Hope this helps!

| improve this answer | |
1

To set the currency based on the website you have to set the default currency by website from configurations.

Go to

Admin->Stores->Configuration->General->Currancy Setup

Select the US Website from the Store View selector from top left of the Configuration page and set the

Default Display Currency to US Dollar and for Allowed Currencies only select the US Dollar. Check this screenshot.

enter image description here

Procced the same for the CA Website.

Clear the cache and check.

| improve this answer | |

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.