You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress(WooCommerce)站点无法使用Pantheon Varnish缓存求助

Fixing WooCommerce-Pantheon Varnish Caching Conflict (For Varnish Newbies)

Hey there! I feel your pain—dealing with caching conflicts when you're new to Varnish is no fun. Let's break down how to solve this issue where WooCommerce is setting cookies for anonymous users and breaking your Pantheon Varnish cache.

The Root Problem

WooCommerce automatically sets cookies (like woocommerce_items_in_cart or wp_woocommerce_session_*) for all users, even if their cart is empty. Pantheon's Varnish setup skips caching any page that sends a Set-Cookie header, which is why your site isn't being cached when WooCommerce is active. Since you can't modify Pantheon's Varnish config, we need to fix this from the WooCommerce/WordPress side.

Practical Solutions

1. Block WooCommerce Cookies for Empty Carts (Code Snippet)

Add this code to your theme's functions.php file (or use a code snippet plugin to avoid messing with theme files directly):

add_action( 'init', 'disable_woocommerce_anonymous_empty_cart_cookies' );
function disable_woocommerce_anonymous_empty_cart_cookies() {
    // Only target anonymous users with empty carts
    if ( ! is_user_logged_in() && WC()->cart->is_empty() ) {
        // Stop WooCommerce from setting session cookies
        remove_action( 'init', array( WC()->session, 'set_customer_session_cookie' ) );
        // Stop WooCommerce from setting cart-related cookies
        remove_action( 'wp_loaded', array( WC()->cart, 'set_cart_cookies' ) );
        // Clear any existing empty-cart cookies if needed
        if ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
            unset( $_COOKIE['woocommerce_items_in_cart'] );
            setcookie( 'woocommerce_items_in_cart', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
        }
    }
}

This code checks if the user is logged out and has an empty cart, then disables the WooCommerce functions that set unnecessary cookies. After adding this, run curl -I dev-example.pantheonsite.io again—you should see the Set-Cookie header disappear for anonymous users with empty carts, letting Varnish cache those pages.

2. Use a Pantheon/WooCommerce-Friendly Cache Plugin

Look for a cache plugin built to work with both Pantheon and WooCommerce. These tools are designed to automatically handle cookie logic: they'll only let WooCommerce set cookies when a user adds items to their cart, and let Varnish cache all other pages. No manual code needed if you go this route!

3. Verify Pantheon's Default Cache Exclusions

Pantheon already excludes key WooCommerce pages (like cart, checkout, and account pages) from caching, which is good—those pages shouldn't be cached anyway. The issue is your non-WooCommerce pages being affected, so the cookie fix above should handle that. But double-check that your critical WooCommerce pages aren't being cached accidentally (test this by adding an item to your cart and confirming the page updates correctly).

Pro Tips for Testing

  • Always back up your functions.php file before adding code snippets.
  • Use curl -I dev-example.pantheonsite.io after making changes to confirm the Set-Cookie header is gone for anonymous users.
  • If you still see cookies, check if other plugins are setting them too—sometimes other extensions can interfere with caching.

内容的提问来源于stack exchange,提问作者jpshayes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:24:00