1

I am trying to set base currency pro-grammatically with below code.

class Currency extends \Magento\Framework\App\Action\Action
{
  protected $_storeManager;
   public function __construct(
     \Magento\Backend\Block\Template\Context $context, 
     \Magento\Store\Model\StoreManagerInterface $storeManager,
     array $data = []
      ) {     
      $this->_storeManager = $storeManager;
      parent::__construct($context, $data);
   }
   public function execute() {  
    $currencyCode = 'USD';
    $this->_storeManager->getStore()->setCurrentCurrencyCode($currencyCode);

  }
}

The above code is not setting the base currency, can anyone tell me where i am wrong? Thanks in Advance!!.

Update

I followed Rohan answer, then getting below error.

enter image description here

Here product prices in basket showing with changed currency, but order summary still showing with the old currency.

0

2 Answers 2

3
+50

For base currency change, I would like to recommend you to change from admin configuration.

Store -> Configuration -> General -> Currency Setup -> Currency Options -> Base Currency

For programmatically, You need to inject this below code in your construct :

/**
 * @var \Magento\Framework\View\Result\PageFactory
 */
protected $_configInterface;

public function __construct(
    ....
    \Magento\Framework\App\Config\ConfigResource\ConfigInterface $configInterface
    ....
) {
    ....
    $this->_configInterface = $configInterface;
    ....
}

Then, you need to use this below line in your function :

$currency_code = "USD";
$this->_configInterface->saveConfig('currency/options/base', $currency_code, $store = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);

Hope, It will useful for you.

7
  • Tried your answer, didn't work @Rohan
    – Manjunath
    Aug 12, 2019 at 9:27
  • Can you please remove generated and clean cache? Because, I tried and it's working fine so. Aug 12, 2019 at 9:30
  • You want to change value of Store -> Configuration -> General -> Currency Setup -> Currency Options -> Base Currency but programmatically. Right? Aug 12, 2019 at 9:31
  • Yes cleared generated and flushed cache also, this code setting code in db, but in frontend all prices are showing with INR only
    – Manjunath
    Aug 12, 2019 at 9:32
  • Yes exactly, Once that is changed, All product prices show in that currency?
    – Manjunath
    Aug 12, 2019 at 9:32
0

Try this

   $this->_storeManager->getStore()->setBaseCurrentCurrencyCode($currencyCode);

OR

   $this->_storeManager->getStore()->setBaseCode($currencyCode);
1
  • No, not working @padhiyar..
    – Manjunath
    Aug 9, 2019 at 7:01

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.