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?

  • Please check my answer. – Rohan Hapani Nov 6 '18 at 8:44
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);
  • i am getting this error : Undefined rate from \"CAD-USD\" – iqbal malik Nov 6 '18 at 9:51
  • Seems that we need to set the rate: STORES > Currency Rates – Khoa TruongDinh Nov 6 '18 at 9:54
  • should i import these rates manually or is there any way to set them programmatically ?? – iqbal malik Nov 6 '18 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 * – Khoa TruongDinh Nov 6 '18 at 10:03
  • i add rate manually for CAD but getting same error : Undefined rate from \"CAD-USD\" – iqbal malik Nov 6 '18 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;
    }
}
  • this is not working for me :( – iqbal malik Nov 6 '18 at 9:51
  • Is there any error or system.log ? – Rohan Hapani Nov 6 '18 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 – iqbal malik Nov 6 '18 at 10:13
  • Check my updated answer. remove var/ generated or generation folder. – Rohan Hapani Nov 6 '18 at 10:33
  • In above code where we tell from currency and to currency ? – iqbal malik Nov 6 '18 at 10:39

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.