I'm trying to switch to "euro" in magento2 index.php for some visitors, but i didn't find how to set currency, this is my code :

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$currencyManager = $objectManager->get('Magento\Directory\Model\Currency'); 

2 Answers 2


public function __construct(

     \Magento\Framework\ObjectManagerInterface $objectmanager


     $this->_objectManager = $objectmanager;


 $currency = isset($post->currency)?$post->currency:'INR';

                    $storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
                    if ($currency) {

I have created a module on my local store that adds a predispatch event. It requires the currency settings on your store to get correctly configured:

  • you need the currency you want to be auto-selected to be within the allowed currencies of your system
  • you also need to have your currency rates to be defined (Stores / Currency Rates)

At this point, without adding the changes below, you should see a currency switcher in your store (this is if your theme is Luma anyway)

Then, you may add the below event in your system. The currency is hard-coded to EUR in my code but you can change this using geoip (I guess this part was something you know)

 <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <event name="controller_action_predispatch">
            <observer name="currency_initialisation_predispatch" instance="Mbs\CurrencySwitcher\Observer\PreDispatchObserver" shared="false" />

and the observer code as per below


namespace Mbs\CurrencySwitcher\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class PreDispatchObserver implements ObserverInterface
     * @var \Magento\Store\Model\StoreManagerInterface
    private $storeManager;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;

     * @inheritDoc
    public function execute(Observer $observer)
        // this can be coming from geoip PHP function
        // php.net/manual/en/ref.geoip.php
        $currency = 'EUR';

        if ($this->getStoreCurrency() !== $currency) {

    private function getStoreCurrency()
        return $this->storeManager->getStore()->getCurrentCurrencyCode();

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.