2

We have 2 store views (en and us) on our website. we have around 5 currencies in that. we need to do the following functionality on our website.

When customers switch the currency to $ (US Dollar) then it should also open the US store view automatically.

It's not related to GEO Location but related to currency switch.

Can you please help us with that. Any help would be appreciated.

Thanks in advance..!

4

I have research and found one solution to this. Suppose you have two store view one for the UK and the second one for the US. The UK is your default store view with GBP currency and you want to change this to review when USD select then you can do using Event & Observer

Create event.xml file in the below path

app/code/Namespace/Yourmodulename

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="force_storecode_redirect" instance="Namespace\Yourmodulename\Observer\ForceStorecodeRedirectObserver" shared="false" />
</event>
</config>

After that create the Observer file in the below path.

Namespace\Yourmodulename\Observer

<?php
namespace Namespace\Yourmodulename\Observer;

use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\Url\DecoderInterface;

class ForceStorecodeRedirectObserver implements ObserverInterface
{
    protected $storeManager;
    protected $url;

    /** @var array $storeCodes - array of existing storecodes*/
    protected $storeCodes = [];

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $url,
        EncoderInterface $urlEncoder,
        DecoderInterface $urlDecoder
    ) {
        $this->storeManager = $storeManager;
        $this->url = $url;
        $this->storeCodes = array_keys($this->storeManager->getStores(false, true));
        $this->urlEncoder = $urlEncoder;
        $this->urlDecoder = $urlDecoder;
    }

    public function encodeUrl($currentUrl)
    {
        return $this->urlEncoder->encode($currentUrl);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        if(!empty($_POST) && (count($_POST) > 0) && (isset($_POST['currency']))){
            $currentStore = $this->storeManager->getStore()->getCode();
            $baseUrl = $this->storeManager->getStore()->getBaseUrl();
            $switchCurrency = $_POST['currency'];

            if($currentStore!='us'){
                if($switchCurrency == "USD"){

                    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                    $request = $objectManager->get('Magento\Framework\App\Request\Http');

                    $currentUrl = $request->getServer('HTTP_REFERER');
                    $storeUrl ="";

                    $domainName = $_SERVER['HTTP_ORIGIN'];
                    $explodeArray = explode($domainName,$currentUrl);
                    $storeUrl = "https://subdomain.com".$explodeArray[1]."?___store=us";

                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: " . $storeUrl);

                    $storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
                    $currency = "USD";
                    if ($currency) {
                        $storeManager->getStore()->setCurrentCurrencyCode($currency);
                    }
                    exit;
                }
            }
        }

    }
}

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.