skip to Main Content

Using the code below, the customer is able to remove an item from the cart when on checkout, but if removing all items – the customer should be re-directed to the shop page.

I have tried the code below which does not work correctly, the customer is still left on the "The checkout is not available when your cart is empty", which is followed by a button to take the customer to the shop.

The idea is not just to re-direct, but also to display a message using wc_add_notice.

The first part of the code is this:

add_filter('woocommerce_cart_item_name', 'product_thumbnail_and_remove_on_checkout', 20, 3 );
    function product_thumbnail_and_remove_on_checkout( $product_name, $cart_item, $cart_item_key ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $cart = WC()->cart->get_cart();
        foreach ($cart as $cart_key => $cart_value){
            if ($cart_key == $cart_item_key){
                $product_id = $cart_item['product_id'];
                $_product = $cart_item['data'] ;
                $remove_product = sprintf(
                    '<a class="remove" style="float:left;margin-right:5px;" href="%s" title="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s">&times;</a>',
                    esc_url(wc_get_cart_remove_url( $cart_key)),
                    __( 'Remove From Order', 'woocommerce' ),
                    esc_attr($product_id),
                    esc_attr($_product->get_sku()),
                    esc_attr($cart_item_key)
                );
            }
        }
        $product = $cart_item['data'];
        $thumbnail = $product->get_image(array(50, 50));
        $image_html = '<div class="product-item-thumbnail">' . $thumbnail . '</div>';
        $product_name_link = '<a class="pnoc" href="' . $product->get_permalink() . '">' . $product_name . '</a>';
        $product_name = $remove_product . $image_html . $product_name_link;
    }
    return $product_name;
}

The code I am trying to make work:

add_action( 'wp_enqueue_scripts', 'cart_js_on_checkout', 10 );
function cart_js_on_checkout() {

    if ( is_checkout() ) {
    wp_enqueue_script( 'wc-cart' );
    }
}

add_action( 'template_redirect', 'redirect_empty_checkout_to_shop' );
function redirect_empty_checkout_to_shop() {

    if ( is_checkout() && WC()->cart->is_empty() && ! is_wc_endpoint_url( 'order-pay' ) && ! is_wc_endpoint_url( 'order-received' ) ) {

    wp_safe_redirect( wc_get_page_permalink( 'shop') );
    
    wc_clear_notices();
    wc_add_notice( __( 'The checkout is not available when your cart is empty. Happy shopping!', 'woocommerce' ), 'info' );
    
    }
}

2

Answers


  1. If I understand right:
    You want to redirect the user right after the last item is removed from the shopping cart?

    Now on my copy of WordPress and Woo, when I remove items from the cart the page isn’t reloaded. Only part of the page is updated with AJAX ( I think you can change this in Woo).

    So the ‘template hook’ probably isn’t being called between the remove cart and what you see on the page. You may still want the ‘template hook’ part, in case the user navigates to the cart when it’s empty. That will redirect them out of there.

    But if it’s happing in AJAX on the cart page you can try this in addition to what you have above:

    function redirect_cart_when_empty() {
        echo '<script type="text/javascript"> 
    ;( function( $, window, document, undefined ) {
       "use strict";
       $( document ).ready( function() {
             $(document.body).on("wc_cart_emptied", function(){
                  window.location.replace("'.wc_get_page_permalink( 'shop').'");
              });
        } );
     } ) ( jQuery, window, document );
    </script>';
    }
    add_action('woocommerce_before_cart_table', 'redirect_cart_when_empty');
    

    I’ll try to explain what we want, so if it doesn’t work you maybe can figure it out. Basically we can use a javascript (jQuery ) trigger from Woo on the front end ( client side ) to initiate some simple javascript that will do the redirect/reload.

    There isn’t much documentation on these hooks but the one I chose hopefully gets fired by Woo when the cart is emptied. Once we have some Javascript going and we can tell the cart is empty we can simply redirect the page using to the Shop permalink.

    I just put my "Standard" jQuery wrapper stuff around it… I’m not an expert at jQuery, I just dabble a little.

    I’m not sure exactly when this hook is used, but there are a couple other that could be used such as removed_from_cart, cart_totals_refreshed etc. Which wouldn’t be as clean but you could then check the total in the cart and do the redirect etc… Maybe there is an "empty cart button" that triggers it? I don’t know.

    One last note is I think window.location.replace() is preferable, because that will remove using the back button to go back to the empty cart and get redirected back to the shop. That just saves the user a little hassle had we just used window.location.href instead.

    You could also simply reload the page and allow the "template hook" to do the redirect with window.location.reload(); then you don’t need to mix PHP in with the javascript.

    Login or Signup to reply.
  2. You were near. Replace your last function with the following, that will redirect to shop page when cart is emptied in checkout (or in cart), displaying a notice in shop page:

    add_action( 'template_redirect', 'redirect_empty_checkout_to_shop' );
    function redirect_empty_checkout_to_shop() {
        if ( ( ( is_checkout() && ! is_wc_endpoint_url() ) || is_cart() ) && WC()->cart->is_empty() ) {
            // We add a WC session variable to display the notices in shop
            WC()->session->set('display_redir_notice', '1' );
            // The redirection
            wp_safe_redirect( wc_get_page_permalink( 'shop') );
            exit(); // exit silently
        } elseif ( is_shop() && WC()->session->get('display_redir_notice') ) {
            // Display the notice in shop page
            wc_add_notice( __( 'The checkout is not available when your cart is empty. Happy shopping!', 'woocommerce' ), 'info' );
            // Remove the WC session variable
            WC()->session->__unset('display_redir_notice');
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

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