wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
687 stars 215 forks source link

Improve compatibility with Aelia Tax Display #4749

Open juricazuanovic opened 2 years ago

juricazuanovic commented 2 years ago

Before submitting an issue please check that you’ve completed the following steps:

Describe the bug The tax/VAT information and prices are not displayed correctly, so our compatibility is not working as expected. When the plugin is enabled, the first visit to the prduct page is cached and the dynamic cookie aelia_customer_country is not set or is set after the loading of the page has finished or interacted with. This causes the tax information and prices to be displayed incorrectly for EU and non-EU customers.

To Reproduce Steps to reproduce the behavior:

  1. Install WooCommerce and Aelia Tax Display plugin
  2. In Aelia Tax Display set up countries for excl. VAT, and countries for incl. VAT
  3. Use VPN for excl. VAT or incl. VAT countries and go directly to one of the products in incognito browser
  4. See error

Expected behavior Customers from outside EU should see prices excluding VAT, while citizens within EU should see price including VAT.

Screenshots https://i.imgur.com/H0wbopD.jpeg

Additional context Tickets: https://secure.helpscout.net/conversation/1784109311/325440 https://secure.helpscout.net/conversation/1355525714/219373/

Temporary solution is to make aelia_customer_country cookie mandatory using this snippet from Vasilis https://snippi.com/s/cdhrcyt

Backlog Grooming (for WP Media dev team use only)

daigo75 commented 2 years ago

@juricazuanovic I'm the author of the Aelia plugins, for which WP Rocket includes an integration. I thought of chiming in, to help addressing the issue.

Current status

Based on the information collected from the customer, it looks like, at the moment, WP Rocket works as follows:

  1. When the Aelia Currency Switcher is active, and its geolocation option is enabled, the content for the first visit (identified by the absence of the country and currency cookies) is served uncached. This is to allow the geolocation to identify visitor's country and currency. The cookies are then used to serve cached content from the second visit onward.
  2. When the Aelia Currency Switcher is active, and its geolocation option is disabled, WP Rocket serves cached content from the first visit. If the country and currency cookies are set, WP Rocket takes them into account. If not, it serves whatever "default" cached content it stored earlier.

This logic is also compatible, to a certain extent, with our other two plugins, Tax Display by Country and Prices by Country, which are standalone products, and use the same geolocation logic as the Currency Switcher. The "gap" that we noticed is that WP Rocket doesn't take into account the fact that these two plugins also provide a geolocation feature.

Due to that, when the Tax Display by Country and Prices by Country plugins are used without the Currency Switcher, or when the Currency Switcher's geolocation is disabled, WP Rocket simply adopts the second approach, serving cached content right from the start, and doesn't let the geolocation run.

How to improve support with the geolocation logic in the Aelia plugins

The Tax Display by Country and Prices by Country include a geolocation feature that is always enabled. It can't be disabled, because that would make the plugins pointless. Due to that, the logic used by WP Rocket to determine if the geolocation is active should be changed as follows:

  1. When the Currency Switcher is active and its geolocation is enabled. This check is already implemented. OR
  2. When the Tax Display by Country plugin is active (its geolocation is always enabled). OR
  3. When the Prices by Country plugin is active (its geolocation is always enabled). OR
  4. When more than one of the above conditions is satisfied.​

That should be the only change needed. The rest of the WP Rocket logic, which fetches the value of country and currency cookies, will work as it is, because all the plugins use the same ones.

Wickedy123 commented 11 months ago

Hi, I'm interested in the prognosis for this issue, as my web site relies on WP Rocket pre-load caching and Aelia Tax Display by Country, or failing that some advice on how to get it to work better. I have a couple customers every week commenting either that prices are inconsistent between product catalog and single product pages (wrong tax amount), or the basket/cart suddenly empties itself out, which I suspect may also be related somehow.

I also find WP Rocket typically fails to create a pre-load cache using the "wp-rocket-preload-dynamic-cookies" helper plugin - I have it set to create GB and NL versions of the aelia_customer_country cookie, but it generally only generates the cookie-free pages. These are of no use to any site visitor, given they all have a value set for aelia_customer_country and therefore need country-specific versions of cached pages. I have cloned my site to a test server to investigate this problem and allow WP Rocket support to take a look directly, but the problem disappears on the clone server. There I suspect it's solved by the changing of the URL (re-installation of WP Rocket alone is not effective). The only way I know to get rid of the problem is to clone the site to a new server with new URL. I have even tried permanently migrating the LIVE site to an entirely new server and changing the DNS A records etc, but it seems that without changing the site's URL changing, the problem just does not go away.

daigo75 commented 11 months ago

I'm still available if there are any questions for me.

daigo75 commented 11 months ago

@piotrbak It's a shot in the dark, but perhaps the issue could be fixed by adding the following lines to the WP Rocket code.

Files to modify:

  1. wp-rocket/inc/3rd-party/plugins/ecommerce/aelia-tax-display-by-country.php
  2. wp-rocket/inc/3rd-party/plugins/ecommerce/aelia-prices-by-country.php

Line 15 add_filter( 'rocket_cache_mandatory_cookies', 'rocket_add_aelia_mandatory_cookies' );

This goes just before the endif; line.

Bottom of the file

/**
 * Add the customer country to the list of mandatory cookies before generating caching files
 *
 * @param array $cookies Mandatory cookies to serve the cache.
 * @return array Updated cookies list
 */
function rocket_add_aelia_mandatory_cookies( $cookies ) {
    $cookies[] = 'aelia_customer_country';

    return $cookies;
}

Why this should work This is a hypothetical fix, based on the logic followed by the Currency Switcher integration. That integration checks if the geolocation is enabled in the Currency Switcher (see function rocket_add_aelia_currencyswitcher_mandatory_cookie( $cookies )) and, in that case, it adds the currency to the mandatory cookies. This seems to be what makes WP Rocket check for the presence of the cookie before trying to serve cached content. When the cookie is absent, WP Rocket lets the site load, so that the Currency Switcher can set it.

The same logic applies to the Tax Display by Country and Prices by Country plugin, which work exactly the same way. However, WP Rocket doesn't set any "mandatory cookies" for them, therefore it just carries on serving whatever content it had cached. This patch corrects the behaviour, ensuring that WP Rocket expects the cookies that those plugins always set, before making any decision on what content should be served.

piotrbak commented 11 months ago

Hello @daigo75 thanks for your input. Could you send me the license or coupon for your products to piotr[at]wp-media.me?

Looks like we don't have an active license. We'd proceed with the tests and enhancement then.

daigo75 commented 11 months ago

@piotrbak I shall send you the details for the licence shortly. I will send it for all the plugins, so you're covered.

Edit Done, I sent you the licences and the download links.