Code Snippets

Show cart contents / total

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file, as this will be wiped entirely when you update the theme.

To display the cart contents and total in your template, use something like:

// Use in conjunction with
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?><?php echo WC()->cart->get_cart_total(); ?></a>

To ajaxify your cart viewer so it updates when an item is added (via ajax) use:

* Show cart contents / total Ajax
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
<a class="cart-customlocation" href="<?php echo esc_url(wc_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>
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;

14 replies on “Show cart contents / total”

[…] I am using woocommerce and wordpress for an e-commerce website. I want to be able to display the number of items and total price in a user's cart anywhere on the page. Normally — and if you use one of the woo themes — this shows up in the menu navigation bar. However, I am using an almost completely blank theme, and I do not know how I can get the item / price total information and display it in html. Their documentation gives this snippet: […]


[…] in most cases — and if you use probably the most woo issues — this shows up within the menu navigation bar. then again, i’m the usage of a nearly completely blank theme, and i have no idea how i can get the article / worth complete knowledge and show it in html. Their documentation offers this snippet: […]


[…] I am using wordpress 4.5.2 and WooCommerce 2.5.5 Using the following link I have added fixed custom fee to WooCommerce cart total : I have displayed one drop-down list on the frontend. It contains different values like 5,10,15,etc.. I want to add the custom fee to WooCommerce cart total which is selected from this drop-down. It means custom fee value will be dynamic. Using the following link I have displayed cart content in the sidebar but, custom fee value is not added to this total. […]


Comments are closed.