How to show number of items in cart and total in WordPress Header or Menu Section



Add this code to you Functions.php file 


//check if woocommerce plugin is activated
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
function is_woocommerce_activated() {
if ( class_exists( 'woocommerce' ) ) {
return true;
}
else {
return false;
}
}
}


//showing cart header menu item
function andro_cart_menu_item() {
if(is_woocommerce_activated()){
?>


  • }

    function andro_cart_shopping_bag(){
    if(is_woocommerce_activated()){
    ?>

    if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) {
    the_widget( 'WC_Widget_Cart', 'title=' );
    } else {
    the_widget( 'WooCommerce_Widget_Cart', 'title=' );
    } ?>

    }
    }

    add_filter( 'add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

    if ( ! function_exists( 'woocommerce_header_add_to_cart_fragment' ) ) {
    function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>

    echo ' $my_cart_count = $woocommerce->cart->cart_contents_count;
    if ($my_cart_count > 0) {
    echo ' active';
    }
    echo '">';
    ?>
    echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
    ?>



    $fragments['span.cart-number-box'] = ob_get_clean();

    return $fragments;
    } // End woocommerce_header_add_to_cart_fragment()
    }



    After that add this code to your header.php 

    To show the cart count:
    cart->cart_contents_count ), $woocommerce->cart->cart_contents_count ); ?> - cart->get_cart_total(); ?>

    or 




    To show the cart bag or cart Widget: 




    Sample Header: