I was wondering if someone could possibly help me out with a little something?
I found this code online :
// 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="<?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' );
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;
}
Which allows me to use the [cart_total] shortcode anywhere on my site. It works fine but what I would like to do is to not spit out anything at all when the cart is empty, so basically the output will only show when someone adds something to their cart. This is just purely for aesthetic reasons.
I'm fairly certain I need to make use of an if statement to check against the cart_contents_count but just can't figure out where to place the if statement. I've tried in about 12 different ways now and none have worked. Nearly all are throwing errors. I literally only just know enough code to be dangerous so have no idea when I'm doing something right or wrong. Only when it works do I know I've 'got' it!! ;-)
Any help with this would be massively appreciated.
Many thanks.
Best wishes,
Mark