0
class Data extends AbstractHelper
{
    protected $_storeManager;
    protected $_currencyFactory;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory,
        ) {
        $this->_storeManager = $storeManager;
        $this->_currencyFactory = $currencyFactory;
        parent::__construct($context);
    }

    function convertRate(){
        $quoteAmmount = 15;
        $baseCurrencyCode = $this->_storeManager->getStore()->getBaseCurrency()->getCode();
        $quoteCurrency = 'CAD';
        $rateToBase = $this->_currencyFactory->create()->load($quoteCurrency)->getAnyRate($baseCurrencyCode);
        $totalAmount = $quoteAmmount * $rateToBase;
    }
}

This is my custom code and I want to convert my CAD quote price to my store base currency which is USD. But this code returned 0.
What is the problem?

1
  • Please check my answer. Nov 6, 2018 at 8:44

2 Answers 2

0

Take a look at vendor/magento/module-directory/Helper/Data.php

public function currencyConvert($amount, $from, $to = null)
{
    if (empty($this->_currencyCache[$from])) {
        $this->_currencyCache[$from] = $this->_currencyFactory->create()->load($from);
    }
    if ($to === null) {
        $to = $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }
    $converted = $this->_currencyCache[$from]->convert($amount, $to);
    return $converted;
}

I think you can use this function:

$this->directoryHelper->currencyConvert($quoteAmmount, $quoteCurrencyCode, $baseCurrencyCode);
7
  • 1
    i am getting this error : Undefined rate from \"CAD-USD\" Nov 6, 2018 at 9:51
  • Seems that we need to set the rate: STORES > Currency Rates Nov 6, 2018 at 9:54
  • should i import these rates manually or is there any way to set them programmatically ?? Nov 6, 2018 at 9:58
  • You can set these rates manually, just press the import button. We can set the cron job on configuration: *STORES > Configuration > General > Currency Setup > Scheduled Import Settings * Nov 6, 2018 at 10:03
  • 1
    i add rate manually for CAD but getting same error : Undefined rate from \"CAD-USD\" Nov 6, 2018 at 10:05
0

Try to use this below code :

class Data extends AbstractHelper
{
    protected $_storeManager;
    protected $_priceCurrency;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Pricing\Helper\Data $priceCurrency,
        ) {
        $this->_storeManager = $storeManager;
        $this->_priceCurrency = $priceCurrency;
        parent::__construct($context);
    }

    function convertRate(){
        $quoteAmmount = 15;
        $rate = $this->_priceCurrency->currency($quoteAmmount, true, true);
        return $rate;
    }
}
7
  • this is not working for me :( Nov 6, 2018 at 9:51
  • Is there any error or system.log ? Nov 6, 2018 at 10:09
  • No, but amount is not converting. As per my requirement i am having 15 CAD and want to convert it in USD. But above code is returning 15 without any change Nov 6, 2018 at 10:13
  • Check my updated answer. remove var/ generated or generation folder. Nov 6, 2018 at 10:33
  • In above code where we tell from currency and to currency ? Nov 6, 2018 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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