You can check the carts content then show the cart if it has some items
// Add a WooCommerce Cart Total shortcode [cart_total]
add_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-contents" href="https://wordpress.stackexchange.com/questions/274073/<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><i class="fa fa-shopping-cart "></i> <?php echo $woocommerce->cart->cart_contents_count;?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_shortcode( 'cart_total','cart_total_function' );
global $woocommerce;
if ( $woocommerce->cart->get_cart_contents_count() != 0 ) {
function cart_total_function( $cart_total ){
global $woocommerce;
$cart_total="<a class="cart-contents" href="#"><i class="fa fa-shopping-cart "></i> ".$woocommerce->cart->cart_contents_count.'</a>';
/* Original code */
// $cart_total="<a class="cart-contents" href="".$woocommerce->cart->get_cart_url().'" title="View your shopping cart">'.sprintf(_n('%d item', '%d item(s) ', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count).' '.$woocommerce->cart->get_cart_total().'</a>';
return $cart_total;
}
}