0

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>&nbsp;<?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>&nbsp;'.$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).'&nbsp;'.$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

-1

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="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><i class="fa fa-shopping-cart "></i>&nbsp;<?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>&nbsp;'.$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).'&nbsp;'.$woocommerce->cart->get_cart_total().'</a>';
return $cart_total;
}


}
  • Hiya. Thanks for that. I'm guessing that code goes in the shortcode part of my code above but when I put that in it came up with an error : ParseError thrown syntax error, unexpected end of file Not sure what that means or how to fix it though? Also the if statement you've written says if count is more than 0 but I believe that may be wrong as I don't want anything to show on the page if the cart is empty so need something more along the lines of if get_cart_contents_count() = 0 or something like that? – Mark Bowen Jul 20 '17 at 19:31
  • I apologies. I have things muddle up in my head. The > 0 is obviously correct. Unfortunately I am getting that error above though. – Mark Bowen Jul 20 '17 at 19:33
  • Okay sorry got it working now. I had left the old shortcode in and it was playing up because the function was being called twice. One slight problem though is that when you add something to the cart now the link doesn't come up immediately. You have to refresh the page once for the text to appear and then on all subsequent add to cart button presses it updates fine. Is there a way to get it to show as soon as the first add to cart button is pressed? Many thanks for the help on this, it's much appreciated. – Mark Bowen Jul 20 '17 at 19:38
  • I have updated the code, Not sure about the link coming up right away so try the amended code if not revert back to what posted first. – Sam Jul 20 '17 at 19:49
  • Hi Sam. Thank you for that code. It came up with an error so I'll check I didn't do something silly again. Will let you know how I get on. – Mark Bowen Jul 21 '17 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.