Bootstrap Templates Bundle

Sunday, April 19, 2015

How to show number of items in cart and total in WordPress Header or Menu Section



Add this code to you Functions.php file 


//check if woocommerce plugin is activated
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
function is_woocommerce_activated() {
if ( class_exists( 'woocommerce' ) ) {
return true;
}
else {
return false;
}
}
}


//showing cart header menu item
function andro_cart_menu_item() {
if(is_woocommerce_activated()){
?>

<li class="border"><a class="cart collapsed" data-target=".shopping-bag" href="javascript:;">
<span class="cart-icon"><?php esc_attr_e('Cart','andro');?></span>

<?php
global $woocommerce;
$my_cart_count = $woocommerce->cart->cart_contents_count;

echo '<span class="cart-number-box';
if ($my_cart_count > 0) {
echo ' active';
}
echo '">';echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
echo '</span>';
?>
</a></li>
<?php }
}

function andro_cart_shopping_bag(){
if(is_woocommerce_activated()){
?>
<div class="shopping-bag">
<?php
if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) {
the_widget( 'WC_Widget_Cart', 'title=' );
} else {
the_widget( 'WooCommerce_Widget_Cart', 'title=' );
} ?>
</div>
<?php
}
}

add_filter( 'add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

if ( ! function_exists( 'woocommerce_header_add_to_cart_fragment' ) ) {
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>

<?php
echo '<span class="cart-number-box';
$my_cart_count = $woocommerce->cart->cart_contents_count;
if ($my_cart_count > 0) {
echo ' active';
}
echo '">';
?>
<?php
echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
?></span>


<?php

$fragments['span.cart-number-box'] = ob_get_clean();

return $fragments;
} // End woocommerce_header_add_to_cart_fragment()
}



After that add this code to your header.php 

To show the cart count:
<?php echo sprintf (_n( '%d item', '%d items', $woocommerce->cart->cart_contents_count ), $woocommerce->cart->cart_contents_count ); ?> - <?php echo $woocommerce->cart->get_cart_total(); ?>

or 

<?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>


To show the cart bag or cart Widget: 

<?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>


Sample Header: 

<div class="top-cart">
<div id="cart" class="btn-group btn-block">
<button type="button" data-toggle="dropdown" data-loading-text="Loading..." class="btn btn-inverse btn-block btn-lg dropdown-toggle">
<span class="top-cart-contain">
<span class="title-cart"></span>
<span id="cart-total">

<?php echo sprintf (_n( '%d item', '%d items', $woocommerce->cart->cart_contents_count ), $woocommerce->cart->cart_contents_count ); ?> - <?php echo $woocommerce->cart->get_cart_total(); ?>

<?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>
</span>
<i class="fa fa-caret-down"></i>
</span>
</button>


<ul class="dropdown-menu pull-right">
<li class="arrow-cart"></li>
<li>
<?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>
</li>
</ul>
</div>
</div>

3 comments:

  1. Hi Hasib, I am trying to achieve a similar thing you explained above. I have included a custom field (number) to product page to which i allocated a default number. I would like to show this custom field in the header section of the theme which is directly related to Update cart event. So if a certain product is added to cart this custom field gets updated similar to cart based on default value defined in WC Product Field Group. Basically it’s like a commission per product a user makes when adding to cart. Similarly when removing the item from cart this custom field subtracts the commission. Any pointers on how this can be achieved would be appreciated. Thanks

    ReplyDelete
  2. Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us. txtcartapp

    ReplyDelete

Wokiee React eCommerce Template