Check if cart is NOT empty in Woocommerce 3 Check if cart is NOT empty in Woocommerce 3 wordpress wordpress

Check if cart is NOT empty in Woocommerce 3


add_action( 'wp_footer', 'vazio' );  function vazio() {     if (sizeof( WC()->cart->get_cart() ) > 0 ) {        // do something     }   }

This will check to see if there are items in the cart. You can add an else statement or check for equivalence as needed.


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

To check the cart content you can also use this code:

global $woocommerce;if ( $woocommerce->cart->cart_contents_count != 0 ) {    // cart has content} else {   // cart is empty}


For ajax add to cart you will also want to add this to your JS file:

$('body').on( 'added_to_cart', function(){    if( ! $(this).hasClass('has_items') ) {        // do something    }});