skip to Main Content

I got this code to clear cart and it’s working perfectly for logged-in users but not guests. For guests, I have to leave the checkout page and load another page to clear the cart.

How to auto empty cart when leaving checkout for registered customers and guest?

This is the code (picked from stack overflow):

<?php

//we inserted a script in wordpress and put it in the head
add_action('wp_head', 'woocommerce_clear_cart_url');
function woocommerce_clear_cart_url()
{
   global $woocommerce;
   //we used the jquery beforeunload function to detect if the user leaves that page
?>
   <script>
      jQuery(document).ready(function($) {
         $(window).bind('beforeunload', function() {
            //Used WordPress to help jQuery determine the page we're targeting 
            //pageID is the page number of the page we're targeting
            <?php if (!is_page(pageID)):
               //empty the cart
               $woocommerce->cart->empty_cart(); ?>
            <?php endif; ?>
         });
      });
   </script>
<?php } ?>

2

Answers


  1. For this you can try it by pure php code. In this you would required template_redirect hook.

    Add below code in function.php

    <?php
    
    add_filter('template_redirect', function () {
       if (!is_checkout() && !WC()->cart->is_empty()) {
          WC()->cart->empty_cart();
       }
       if (is_cart() && WC()->cart->is_empty()) {
          wp_redirect(home_url());
          exit;
       }
       if (is_cart() && !WC()->cart->is_empty()) {
          wp_redirect(wc_get_checkout_url());
          exit;
       }
    });
    

    Note: Please ensure you have done fast checkout once product gets added to cart directly send them to checkout page. So this could make more sence.

    Login or Signup to reply.
  2. It looks like your code is partially working but not fully clearing the cart for guest users when they leave the checkout page. To ensure both logged-in users and guests experience the same functionality, there are a couple of key changes we can make.

    Issues with the Current Code:

    Guest Users: The current script only clears the cart after the guest loads another page. This is because the code runs server-side and waits for a new page to be loaded.

    Use of beforeunload Event: While beforeunload is used, it’s not an ideal solution because it doesn’t consistently fire or perform the action fast enough before a user leaves the page.

    Suggested Solution

    We can make some modifications to your code to properly clear the cart for both registered users and guests. One approach is to send an AJAX request when the user leaves the checkout page, which works better than using beforeunload. Here’s a working solution:

    // Insert the script into WordPress and put it in the head
    add_action('wp_footer', 'woocommerce_clear_cart_url'); // Use wp_footer instead of wp_head
    function woocommerce_clear_cart_url() {
        if (is_checkout()) { // Only add the script if we're on the checkout page
    ?>
        <script>
            jQuery(document).ready(function($) {
                $(window).on('beforeunload', function() {
                    // AJAX call to clear the cart
                    $.ajax({
                        url: "<?php echo admin_url('admin-ajax.php'); ?>",
                        type: 'POST',
                        data: {
                            action: 'clear_cart_action'
                        },
                        async: false
                    });
                });
            });
        </script>
    <?php
        }
    }
    
    // Add the function to clear the cart via AJAX
    add_action('wp_ajax_clear_cart_action', 'clear_cart_action'); // For logged-in users
    add_action('wp_ajax_nopriv_clear_cart_action', 'clear_cart_action'); // For guests
    
    function clear_cart_action() {
        WC()->cart->empty_cart(); // Clear the cart
        wp_die(); // Required to end the request
    }
    

    You can follow this method from this post.

    https://howtoinwordpress.com/how-to-automatically-clear-cart-when-users-leave-the-checkout-page-in-wordpress/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search