3

I need to switch the currency based on user IP location so I'm using this auto currency extension, it works only when I disabled the FPC otherwise it just cache the previous content and never gets changed.

I'm looking to programmatically clear the varnish cache by the cache tag of the store currency. I tried the below logic in the observer (controller_action_predispatch) but it does not work.

protected $config;
protected $purgeCache;

public function __construct(
    \Magento\PageCache\Model\Config $config,
    \Magento\CacheInvalidate\Model\PurgeCache $purgeCache
) {
    $this->config = $config;
    $this->purgeCache = $purgeCache;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $tags = [];
    $pattern = "((^|,)%s(,|$))";
    $tags[] = sprintf($pattern, 'store');
    $this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
}

Thanks in advance.

2 Answers 2

1
+50

Instead of trying to invalidate cache via Magento which is under the 2 difficult things in CS:

  1. Naming things
  2. Cache invalidation
  3. Off-by one errors :)

Instead leverage Varnish itself with a module which implements the same maxmind GeoIP db within Varnish to set proper HTTP Headers like so:

set req.http.X-Country-Code = geoip.country_code(req.http.X-Forwarded-For);

Full Varnish config example in original sources.

NOTE: Your host setup may need adjusting, and Varnish will need to be recompiled which can be a task in itself with versions specific for the module and Magento.

Source:

4
  • 1
    superb idea! I'll try this and let you know the results Commented Jun 12, 2018 at 16:19
  • 1
    I import the geoip vmod in varnish and successfully get the country code of the user and set in the header request. I read the request header in Magento and used it. This method also faces the same issue. When I first load the site in France server it returns the country code as FR, after that I load the site in India, it returns again FR when I refresh the cache that time only it returns IN for India. Commented Jun 13, 2018 at 8:54
  • magento.stackexchange.com/a/23528/69 since Magento uses cookies for currency you'll need some vcl re-tooling most likely.
    – B00MER
    Commented Jun 13, 2018 at 10:16
  • still, I'm not successful. If you have detailed working answer, please share with me. I'm using M2.2.2 EE version. Commented Jun 13, 2018 at 14:30
0

If you need to implement a feature that needs to bypass the FPC in Magento 2, the recommended way is to write a Punch Hole implementation for your requirement.

Magento 2 has a structured way to get this written: I have an example at: https://bitbucket.org/magstaging/punchhole

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.