0

I have created a module that autoselects a currency based on the user's country using a plugin for Magento\Store\Model\Store::getDefaultCurrencyCode() .

On the first page load it will show the default website's currency, then on the second page load the currency will have been applied. If I disable full page cache then the correct currency will be selected on the first page load.

I guess that I need another plugin to update the page cache but I'm not sure what needs editing.

Here's the really simplified version of my plugin class:

<?php

namespace <vendor>\<module>\Plugin\Store\Model;

use Magento\Framework\UrlInterface;
use Magento\Framework\App\ObjectManager;

class Store
{
    protected $default_currency;

    public function aroundGetDefaultCurrencyCode(\Magento\Store\Model\Store $subject, callable $proceed)
    {
        $helper = ObjectManager::getInstance()->get('<vendor>\<module>\Helper\Data');
        $this->default_currency = $proceed();

        if ($helper->isEnabled())
            $this->getCurrencyCodeByIp($this->default_currency);

        return $this->default_currency;
    }

Here's my frontend/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Store\Model\Store">
        <plugin name="AutoCurrency_Switcher" type="<vendor>\<module>\Plugin\Store\Model\Store" sortOrder="0" />
    </type>
</config>
0

You could try creating an Observer so that when you enter the page your code block executes. I'm not entirely sure what the Observed event would be, but it may be worth looking in to.

  • Cool, i'll look into it and post any changes here – Callum Dec 20 '18 at 10:25

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.