10

In WooCommerce, I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page. Can anyone have the solution ?

Here is the code I have tried, but it does not work:

function my_empty_cart() {
  global $woocommerce;

    if (isset( $_GET['empty-cart'] ) ) { 
        wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'product' ) ) );
    }
}
add_action( 'init', 'my_empty_cart' );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Lipsa
  • 397
  • 3
  • 7
  • 26

6 Answers6

15

// old woocommerce : use sizeof( $woocommerce->cart->cart_contents) to check cart content count

// In new woocommerce 2.1+ : WC()->cart->cart_contents_count to check cart content count

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' ) ) );
    }
}

init hook will run everytime. use template_redirect

==============Updates=============

In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.

WC()->cart->cart_contents_count

Pushpak Patel
  • 808
  • 6
  • 13
7

2021 Update

Since WooCommerce version 3 use the following to redirect to shop page when trying to access cart page and cart is already empty:

add_action( 'template_redirect', 'empty_cart_redirect' );
function empty_cart_redirect(){
    if( is_cart() && WC()->cart->is_empty() ) {
        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
        exit();
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Notes - Obsolete and deprecated code:

  • global $woocommerce; use with $woocommerce->cart is replaced simply by WC()->cart
  • sizeof($woocommerce->cart->cart_contents) == 0 is replaced by simple WC()->cart->is_empty()
  • woocommerce_get_page_id() is replaced by wc_get_page_id()

When removing all cart items on cart page, to make the redirection active, some additional jQuery code is required, see: Redirect to shop if cart is emptied on cart page in WooCommerce 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
5

Just tested this myself as I needed something similar.

function cart_empty_redirect_to_shop() {
    global $woocommerce;

    if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
        wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
    }
}

add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
adamj
  • 4,672
  • 5
  • 49
  • 60
1

I tried @Pushpak's solution, but it doesn't work anymore. To check the cart content please use this code:

global $woocommerce;
if ( $woocommerce->cart->cart_contents_count != 0 ) {
    // cart has content
} else {
    // cart is empty
}
honk31
  • 3,895
  • 3
  • 31
  • 30
1

2018 - 25 - Sept

working for me today::

function cart_empty_redirect_to_shop() {
  global $woocommerce, $woocommerce_errors;

if ( is_cart() && sizeof($woocommerce->cart->cart_contents) == 0) { 
        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); 
     exit;
    }
}
add_action( 'template_redirect', 'cart_empty_redirect_to_shop' );
  • It seems as a very old question. Anyway its' good that you tried to answer which may help others. But try to answer for newer questions. – smilyface Sep 25 '18 at 08:48
0

Simply go to woocommerce folder and navigate to the CART folder.. in there is a cart-empty.php

then locate the following:

<p><a class="button1 btn btn-normal" href="<?php echo get_permalink.....

it would say getpermalink(woocommerce_.....

simply change that to

<p><a class="button1 btn btn-normal" href="<?php echo get_permalink('THE PAGE ID YOU WANT TO REDIRECT TO');

and bobs your uncle.. it will redirect to page with id you specify.

Let me know if you dont understand.

Juan
  • 1
  • I recommend adding some code to your function.php file. When you update the plugin, you'll have to redo this...flip that. – Uncle Iroh Nov 17 '15 at 01:01