Table of Contents:

Categories:

How to Fix WooCommerce Empty Cart After Redirect (Cloudflare Cookie Issue)

How to Fix WooCommerce Empty Cart After Redirect (Cloudflare Cookie Issue)
WooCommerce Add to Cart Redirect: Avoiding Cookie Loss with JavaScript

Solve WooCommerce cart cookie issues on Cloudflare with a simple JavaScript redirect. This guide shows how to replace standard 302 redirects with a soft redirect that preserves cart session data. Works with Bricks Builder and other page builders.

Fix WooCommerce Empty Cart After Redirect

The Problem: Empty Cart After Adding Products

If you’re running WooCommerce with Cloudflare caching, you might encounter a frustrating issue: customers add products to their cart, get redirected to the cart page, but find it completely empty. This problem occurs specifically when WooCommerce’s “Redirect to cart page after successful addition” setting is enabled.

Why Does This Happen?

The root cause lies in how Cloudflare handles HTTP redirects and cookies. Here’s what happens behind the scenes:

  • When a customer adds a product, WooCommerce creates cart cookies (like woocommerce_cart_hash and wp_woocommerce_session_)
  • WooCommerce then triggers a 302 redirect to send the customer to the cart page
  • During this redirect, Cloudflare’s caching layer may strip or not properly forward these newly created cookies
  • The cart page loads without the necessary session data, resulting in an empty cart

This is particularly problematic because the cookies are created and the redirect happens almost simultaneously, leaving no time for the cookies to be properly set in the browser before the redirect occurs.

The Solution: JavaScript-Based Soft Redirect

Instead of using WooCommerce’s built-in PHP 302 redirect, we can implement a JavaScript-based redirect that:

  • Allows the page to fully load with all cookies properly set
  • Waits for the DOM to be ready before redirecting
  • Gives the browser time to process and store cart session data
  • Bypasses Cloudflare’s cookie-stripping behavior during server-side redirects

This approach ensures that cart cookies are established in the browser before the redirect happens, maintaining the cart session throughout the navigation process.

// Turn off the standard WOOCOMMERCE Radirect
add_filter('woocommerce_add_to_cart_redirect', '__return_false');

// make our Radirect via JavaScript
add_action('wp_footer', 'custom_cart_redirect_script');
function custom_cart_redirect_script() {
    // Перевіряємо чи є параметр add-to-cart або added-to-cart
    if (isset($_GET['add-to-cart']) || isset($_GET['added-to-cart']) || isset($_POST['add-to-cart'])) {
        ?>
        <script type="text/javascript">
            (function() {
                // Looking forward to full download of the page
                if (document.readyState === 'loading') {
                    document.addEventListener('DOMContentLoaded', redirect);
                } else {
                    redirect();
                }
                
                function redirect() {
                    setTimeout(function() {
                        window.location.href = '<?php echo esc_url(wc_get_cart_url()); ?>';
                    }, 200);
                }
            })();
        </script>
        <?php
    }
}

 

If this doesn’t work, add temporarily Debug to see which parameters are transmitted:

add_action('wp_footer', 'custom_cart_redirect_script_debug');
function custom_cart_redirect_script_debug() {
    ?>
    <script type="text/javascript">
        console.log('GET params:', window.location.search);
        console.log('Current URL:', window.location.href);
    </script>
    <?php
    
    // PHP debug
    if (!empty($_GET)) {
        echo '<!-- GET params: ' . print_r($_GET, true) . ' -->';
    }
}

Open the browser console (F12) and see what is displayed after adding the product. Reset me the result – I’ll tell you the right condition! Retryclaude does not have the abity to Run the Code It Generates Yet.

(WooCommerce Empty Cart After Redirect) Questions and answers:

filandor_vadim