//make a php file add this code and link function php
//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()){
?>
<a href="#" id="cart_btn" class="dropdown-toggle" data-toggle="dropdown">
<img src="<?php echo get_template_directory_uri();?>/assests/images/bag.png" alt="">
<?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>
<?php }
}
function andro_cart_shopping_bag(){
if(is_woocommerce_activated()){
?>
<div class="shopping-bag dropdown-menu">
<?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 '">';
?><span>
<?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()
}
//add it to header or where you need show counter
<div class="bag top_item">
<?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>
<?php if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>
</div>