// 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' ) ) );
}
}
inithook will be launched every time. usetemplate_redirect
==============Updates=============
In the new woocommerce, they updated the functionality, and now you can use the following function to directly get a count of the contents of the basket.
WC()->cart->cart_contents_count
source
share