woocommerce/docs/utilities/configuring-caching-plugins.md

101 lines
4.3 KiB
Markdown
Raw Normal View History

2023-11-29 18:48:05 +00:00
---
2023-11-30 19:08:18 +00:00
post_title: Configuring caching plugins for WooCommerce
2023-11-29 18:48:05 +00:00
---
2023-11-20 17:13:47 +00:00
## Excluding Pages from the Cache
Oftentimes if using caching plugins theyll already exclude these pages. Otherwise make sure you exclude the following pages from the cache through your caching systems respective settings.
- Cart
- My Account
- Checkout
These pages need to stay dynamic since they display information specific to the current customer and their cart.
## Excluding WooCommerce Session from the Cache
If the caching system youre using offers database caching, it might be helpful to exclude `_wc_session_` from being cached. This will be dependent on the plugin or host caching so refer to the specific instructions or docs for that system.
## Excluding WooCommerce Cookies from the Cache
Cookies in WooCommerce help track the products in your customers cart, can keep their cart in the database if they leave the site, and powers the recently viewed widget. Below is a list of the cookies WooCommerce uses for this, which you can exclude from caching.
| COOKIE NAME | DURATION | PURPOSE |
| --- | --- | --- |
| woocommerce_cart_hash | session | Helps WooCommerce determine when cart contents/data changes. |
| woocommerce_items_in_cart | session | Helps WooCommerce determine when cart contents/data changes. |
| wp_woocommerce_session_ | 2 days | Contains a unique code for each customer so that it knows where to find the cart data in the database for each customer. |
| woocommerce_recently_viewed | session | Powers the Recent Viewed Products widget. |
| store_notice[notice id] | session | Allows customers to dismiss the Store Notice. |
Were unable to cover all options, but we have added some tips for the popular caching plugins. For more specific support, please reach out to the support team responsible for your caching integration.
### W3 Total Cache Minify Settings
Ensure you add mfunc to the Ignored comment stems option in the Minify settings.
### WP-Rocket
WooCommerce is fully compatible with WP-Rocket. Please ensure that the following pages (Cart, Checkout, My Account) are not to be cached in the plugins settings.
We recommend avoiding JavaScript file minification.
### WP Super Cache
WooCommerce is natively compatible with WP Super Cache. WooCommerce sends information to WP Super Cache so that it doesnt cache the Cart, Checkout, or My Account pages by default.
### Varnish
```varnish
if (req.url ~ "^/(cart|my-account|checkout|addons)") {
return (pass);
}
if ( req.url ~ "\\?add-to-cart=" ) {
return (pass);
}
```
2023-11-20 17:16:47 +00:00
## Troubleshooting
2023-11-20 17:13:47 +00:00
2023-11-20 17:16:47 +00:00
### Why is my Varnish configuration not working in WooCommerce?
2023-11-20 17:13:47 +00:00
Check out the following WordPress.org Support forum post on[ how cookies may be affecting your varnish coding](https://wordpress.org/support/topic/varnish-configuration-not-working-in-woocommerce).
2023-11-20 17:16:47 +00:00
```text
2023-11-20 17:13:47 +00:00
Add this to vcl_recv above "if (req.http.cookie) {":
2023-11-29 19:40:54 +00:00
# Unset Cookies except for WordPress admin and WooCommerce pages
2023-11-20 17:13:47 +00:00
if (!(req.url ~ "(wp-login|wp-admin|cart|my-account/*|wc-api*|checkout|addons|logout|lost-password|product/*)")) {
unset req.http.cookie;
}
2023-11-29 19:40:54 +00:00
# Pass through the WooCommerce dynamic pages
2023-11-20 17:13:47 +00:00
if (req.url ~ "^/(cart|my-account/*|checkout|wc-api/*|addons|logout|lost-password|product/*)") {
return (pass);
}
2023-11-29 19:40:54 +00:00
# Pass through the WooCommerce add to cart
2023-11-20 17:13:47 +00:00
if (req.url ~ "\?add-to-cart=" ) {
return (pass);
}
2023-11-29 19:40:54 +00:00
# Pass through the WooCommerce API
2023-11-20 17:13:47 +00:00
if (req.url ~ "\?wc-api=" ) {
return (pass);
}
2023-11-29 19:40:54 +00:00
# Block access to php admin pages via website
2023-11-20 17:13:47 +00:00
if (req.url ~ "^/phpmyadmin/.*$" || req.url ~ "^/phppgadmin/.*$" || req.url ~ "^/server-status.*$") {
error 403 "For security reasons, this URL is only accesible using localhost (127.0.0.1) as the hostname";
}
Add this to vcl_fetch:
2023-11-29 19:40:54 +00:00
# Unset Cookies except for WordPress admin and WooCommerce pages
2023-11-20 17:13:47 +00:00
if ( (!(req.url ~ "(wp-(login|admin)|login|cart|my-account/*|wc-api*|checkout|addons|logout|lost-password|product/*)")) || (req.request == "GET") ) {
unset beresp.http.set-cookie;
}
```
2023-11-20 17:16:47 +00:00
### Why is my Password Reset stuck in a loop?
2023-11-20 17:13:47 +00:00
This is due to the My Account page being cached, Some hosts with server-side caching dont prevent my-account.php from being cached.
If youre unable to reset your password and keep being returned to the login screen, please speak to your host to make sure this page is being excluded from their caching.