woocommerce / storefront

Official theme for WooCommerce
https://wordpress.org/themes/storefront/
972 stars 471 forks source link

#2113 breaks refreshing the cart on non-WC cached pages #2114

Closed A-Printer closed 1 year ago

A-Printer commented 1 year ago

2113 has an unintended consequence: it prevents the cart being refreshed on non-WooCommerce pages that are cached with a disk cache solution such as WP Super Cache. Until the 4.5.0 release, a cached page could potentially be served with the "wrong" cart information (the information from the user that triggered the page being cached) but that was not a problem since wc-cart-fragments would update the cart with the correct information almost immediately. This change breaks this for non-WooCommerce pages. Pages from WooCommerce do still get the correct cart information, but other pages (for example a contact page) do not anymore. This makes impossible to cache any non-WooCommerce page.

albarin commented 1 year ago

Hi @A-Printer, sorry to hear you are having issues with the latest Storefront release! Could you provide more detailed steps to reproduce it? It would be a great help for us to better understand the issue. Thanks a lot!

A-Printer commented 1 year ago

Hi Alba, I should have been more clear. Here goes:

WooCommerce used to enqueue wc-cart-fragments on every page. Since version 7.8, that was changed to only enqueue them when the cart widget is present. Which was fine.

Now #2113 adds a filter that limits the cart sync to WooCommerce pages only. But in Storefront, the cart widget is present on all pages, not only WooCommerce pages.

To be clear: some pages display the cart widget but are not WooCommerce pages. The best example for this kind of page would be a Contact Us or About Us page. Not store pages per se, but they still display the cart widget.

This is fine when no file caching system (such as WP Super Cache) is present, as the cart won't be updated from these pages anyway. BUT the very nice side benefit of the previous extra cart sync was that cached pages would have the cart always synced with the correct values (the cached value may be different, as it may have been cached by a user with a different cart content).

With this change, the non-WC pages don't get their cart widget updated, and display potentially inaccurate cart information.

webwit commented 1 year ago

Hi, this 4.5.0 update broke things on my site as well.

First, the cart contents are no longer shown when hovering the cart icon. Looking with the browser inspector, the html for it is empty suggesting it isn't filled with javascript any longer.

Second, WP Rocket has an extra plugin called "WP Rocket | Deactivate WooCommerce Refresh Cart Fragments Cache". This makes sure that if user A has 3 items in a cart and causes a page to cache, then user B doesn't get "3 items in cart" while he does not. This no longer works and visitors get a cart item count they do not have.

Looking at the git changes, in inc/class-storefront.php it does this: add_filter( 'woocommerce_get_script_data', array( $this, 'limit_cart_sync_to_wc_pages' ), 10, 2 );

I outcommented it and everything works again. Conclusion: limit_cart_sync_to_wc_pages doesn't seem to work (maybe only on homepage, about us etc like suggested in the previous post).

A-Printer commented 1 year ago

PR #2115 fixes things with the cart widget, but not with the cart that is present in the menu, here on the right of the screen.