Redirect an Empty Checkout Page in WooCommerce

Woocommerce decorative picture

Redirect an Empty Checkout Page in WooCommerce

When users remove items from their cart, they are more likely to leave your website. To prevent this, redirect them to products that may capture their interest and keep them engaged.

function redirect_on_empty_cart_after_item_remove() {
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $(document.body).on('click', '.remove', function(e){
                e.preventDefault();

                // Wait for WooCommerce to process the cart item removal
                setTimeout(function(){
                    // Check if the cart is empty
                    $.get(wc_add_to_cart_params.ajax_url, {
                        action: 'check_cart_empty'
                    }, function(is_empty) {
                        if (is_empty === '1') { // If the cart is empty
                            // Redirect to a page with suggested products
                            var redirectUrl = "<?php echo esc_url(get_permalink(get_option('woocommerce_shop_page_id'))); ?>"; // Customize this URL

                            // Example to fetch related or featured products (Optional)
                            $.get(wc_add_to_cart_params.ajax_url, {
                                action: 'get_related_products'
                            }, function(data) {
                                if (data) {
                                    redirectUrl = data; // Redirect to the related product
                                }
                                window.location.href = redirectUrl; // Perform the redirection
                            });
                        }
                    });
                }, 500); // Delay to ensure the cart is updated
            });
        });
    </script>
    <?php
}
add_action('wp_footer', 'redirect_on_empty_cart_after_item_remove');

// Function to check if the cart is empty after an item is removed
function check_cart_empty() {
    echo WC()->cart->is_empty() ? '1' : '0'; // Return 1 if cart is empty, 0 if not
    wp_die();
}
add_action('wp_ajax_check_cart_empty', 'check_cart_empty');
add_action('wp_ajax_nopriv_check_cart_empty', 'check_cart_empty');

// Function to get related products (optional, you can adjust this)
function get_related_products() {
    // Get the first product in the cart
    $cart_items = WC()->cart->get_cart();
    
    if (!empty($cart_items)) {
        $first_cart_item = reset($cart_items);
        $product_id = $first_cart_item['product_id'];
        
        // Fetch related products (default WooCommerce behavior)
        $related_products = wc_get_related_products($product_id, 1); // Adjust number of products here
        
        if (!empty($related_products)) {
            $related_product_id = $related_products[0];
            $related_product_url = get_permalink($related_product_id);
            
            echo $related_product_url; // Send back the related product URL
        }
    }
    wp_die();
}
add_action('wp_ajax_get_related_products', 'get_related_products');
add_action('wp_ajax_nopriv_get_related_products', 'get_related_products');

Post Your Comment


Premium Service in Every Plan

Experience Unmatched Comfort, Discover Timeless Luxury

RW Host logo
Our System Administrators and R&D team are committed to developing innovative solutions every day to enhance security and optimize server efficiency.

Contact Us

Days :
Hours :
Minutes :
Seconds

—Unlock Superior Hosting Performance —

Enhance VPS
25% OFF