Redirect Empty Cart to Home and Checkout to Cart

Had an issue on a WooCommerce setup where the cart page was redirecting to the checkout page and the cart page would redirect to the checkout page again. A continues redirect loop. How could we redirect empty cart to home and checkout to cart?

Checkout Redirect to Home & Cart Redirect to Checkout

So we needed an option to redirect empty cart to home. This besides the cart skip redirects we already had. I check out a few solutions with our current code base setup.

One by Etzel Storfer Web Development :

// ===========================================================================
//  Redirect Empty Checkout to Home 
// ===========================================================================

add_action('template_redirect', 'redirection_function');

function redirection_function(){
    global $woocommerce;

    if( is_checkout() && 0 == sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count) && !isset($_GET['key']) ) {
        wp_redirect( home_url() ); 
        exit;
    }
}

which did not work as checkout still went to cart and cart back to checkout. Another option which is quite similar:

add_action("template_redirect", 'redirection_function');
function redirection_function(){
   global $woocommerce;
   if( is_cart() && WC()->cart->cart_contents_count == 0){
       wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
   }
}

had the same result. Redirects kept on going as it did not overcome current ones set up.

Enhancing Existing Redirect

This was because an existing redirect to checkout when cart was hit. The solutions were not bad at all. They just did not work in our setup. So I tweaked the code for to make an exception when an empty cart was there. This by adding

if( is_cart() && WC()->cart->cart_contents_count == 0) {
        wp_redirect( home_url() ); 
    } 
    
    else {

to an existing method that made it this in the end:

// Global redirect to check out when hitting cart page unless cart empty
add_action( 'template_redirect', 'redirect_to_checkout_if_cart' );
function redirect_to_checkout_if_cart() {

    if ( !is_cart() ) return;
    global $woocommerce;

    // Getting the checkout for the current language.
    // Checking whether icl_object_id() exists as a
    // function since its not a WordPress or theme function.

    if( is_cart() && WC()->cart->cart_contents_count == 0) {
        wp_redirect( home_url() ); 
    } 
    
    else {
        $current_language_checkout_url = ! function_exists( 'icl_object_id' ) ? get_permalink(
            icl_object_id(
                url_to_postid(
                    $woocommerce->cart->get_checkout_url()
                ),
                'post',
                false,
                ICL_LANGUAGE_CODE
            )
        ) : $woocommerce->cart->get_checkout_url();

        wp_redirect( $current_language_checkout_url, '301' );

        exit;
    }
}

So this means you could very well use one of the two earlier mentioned functions just fine. I just needed it integrated in an earlier set up method.

Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.