1

I want to set base currency programmatically in magento2. kindly suggest me a solution.

3
  • Please Accept the answer if it helps you in anyway or resolved your issue. thanks ! Commented Aug 21, 2018 at 9:23
  • have you tried this tutorial by the mageplaza mageplaza.com/devdocs/…
    – Mohit Rane
    Commented Aug 8, 2019 at 12:12
  • @MohitRane, mageplaza tutorial is for getting the values, the question is how to set base currency, how that can be done?
    – Manjunath
    Commented Aug 9, 2019 at 6:46

1 Answer 1

2

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.

5
  • This doesn't work for me @Shubham, Commented Aug 8, 2019 at 11:12
  • This code doesn't work for me.
    – Manjunath
    Commented Aug 9, 2019 at 6:43
  • @jafarpinjar : Please let me know the Magento Version you are using. also pasting your code snippet will help me to assist you better. As last time i checked this was working for me. Commented Aug 9, 2019 at 21:07
  • @ShubhamMathur, I used above code in magento 2.3.1 Commented Aug 10, 2019 at 8:56
  • @ShubhamMathur, could you check it, is it worked for you? Commented Aug 12, 2019 at 7:33

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.