1

In my extension, the default storeview is English and I want to set Arabic storeview, only one time when the customer comes first time on my website. I'm using controller_action_predispatch event to check customer comes the first time or not, if not, customer can move to English storeview as well. Following is my piece of code:

Namespace\Modulename\etc\frontend\events.xml

<event name="controller_action_predispatch">
    <observer name="switch_store" instance="Namespace\Modulename\Observer\SwitchStore" shared="false" />
</event>

Namespace\Modulename\Observer\SwitchStore.php

protected $storeManager;

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

public function execute(\Magento\Framework\Event\Observer $observer) {
    if ($this->firstVisit()) { // Customer's first visit logic inside firstVisit() function
        $storeId = $this->storeManager->getStore($target_store)->getId();
        $this->storeManager->setCurrentStore($storeId);
    }
    return $this;
}

This code is working fine for the first time, but when the user tries to switch English storeview, he/she would be redirected to the same storeview, that is Arabic. I debugged so many times, but in logs, I'm getting perfect output and not reflecting the change on the frontend. I think this is a cache issue, but not sure. Please let me know if anyone has a solution for this.

Thanks in anticipate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.