1

How we can set Currency by default Billing Address Country. I was trying to implement the same using the solution mentioned in

How to set Currency by default Billing Address Country in Magento 1.9?.

But now I got stuck in the following error.

Fatal error: Uncaught Error: Class 'Mage' not found. Becuase " Mage::app()->getStore()->setCurrentCurrencyCode($mapping[$countryCode]);" not supported in magento 2.

I was trying to do change the currency on cutomer login based on billing addrsss country with the help of observer.

Can anybody help on how we Can set the store currency in Magento 2?

1 Answer 1

0

You can use setCurrencycode($code) method to set your store currency programmatically.

Just Inject following class: Magento/Store/Model/Store

in there a function is defined as:

public function setCurrentCurrencyCode($code)
{
     $code = strtoupper($code);
     if (in_array($code, $this->getAvailableCurrencyCodes())) {
            $this->_getSession()->setCurrencyCode($code);

            $defaultCode = $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode();
            $this->_httpContext->setValue(Context::CONTEXT_CURRENCY, $code, $defaultCode);
      }
        return $this;
  }

Just Call this function anywhere you want and pass the currency code in the parameter.

1
  • Not worked for me Commented Aug 8, 2019 at 11:49

Your Answer

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

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