Friday, June 21, 2013

Show cart contents / total at WooCommerce header in Wordpress?

Add the following bit of code to your header file exactly where you want to display the cart total items value.

<?php global $woocommerce; ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?>
</a>

<!---Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)---->

<?php
add_filter('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'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?>
</a>

<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>

Keep Smiling.
Naresh.

2 comments:

  1. Blog and blog would be of advantage, Thank you for providing information about the update. I understand the concept of Trackback. If you have made your blog can be super site., If there could be a great success in Moraere comments and trackbacks or it get, in order to load a lot of people.




    eCommerce platform

    ReplyDelete
  2. Thank you for sharing excellent information. Your website is very cool. Fully useful your blog post... Online shopping website in india

    ReplyDelete