Display cart item count

Last updated on Friday, January 27th, 2012

Step 1

Navigate to Settings → eShop → Product Detail.
Set “Add to cart, without going to cart page.” to “No”.

Step 2

Add the following to your theme’s functions.php file:

// Return the total amount of items in the shopping cart
function get_myeshop_cart_itemcount(){
	global $blog_id;
	$total_items_in_cart = 0;
	if(isset($_SESSION['eshopcart'.$blog_id])) {
		$item_array = $_SESSION['eshopcart'.$blog_id];
		foreach($item_array as $item) {
			$total_items_in_cart = $total_items_in_cart + $item['qty'];
		}
	}
	return $total_items_in_cart;
}
// Display number of items in shopping cart
function display_my_cart_items() {
	$cart_item_count = get_myeshop_cart_itemcount();
	if ( $cart_item_count > 1 ) printf('%d %s', $cart_item_count, __('items', 'theme'));
	else echo __('Your cart is empty','theme');
}

Step 3

Add something like:

<span id="cart-items"><?php display_my_cart_items(); ?></span>

to your theme’s template files wherever you want it to show.

Code courtesy of yzlow

WordPress Installation and Set Up

Top