Advertisement
humayun180

Showing items in cart and total in woocommerc

Mar 22nd, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2. http://hasibtutorial.blogspot.com/2015/04/how-to-show-number-of-items-in-cart-and.html
  3.  
  4. Add this code to you Functions.php file
  5.  
  6.  
  7. //check if woocommerce plugin is activated
  8. if ( ! function_exists( 'is_woocommerce_activated' ) ) {
  9.  function is_woocommerce_activated() {
  10.   if ( class_exists( 'woocommerce' ) ) {
  11.    return true;
  12.   }
  13.   else {
  14.    return false;
  15.   }
  16.  }
  17. }
  18.  
  19.  
  20. //showing cart header menu item
  21. function andro_cart_menu_item() {
  22. if(is_woocommerce_activated()){
  23. ?>
  24.  
  25. <li class="border"><a class="cart collapsed" data-target=".shopping-bag" href="javascript:;">
  26.  <span class="cart-icon"><?php esc_attr_e('Cart','andro');?></span>
  27.  
  28.  <?php
  29.  global $woocommerce;
  30.  $my_cart_count = $woocommerce->cart->cart_contents_count;
  31.  
  32.   echo '<span class="cart-number-box';
  33.   if ($my_cart_count > 0) {
  34.    echo ' active';
  35.   }
  36.   echo '">';echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
  37.   echo '</span>';
  38.  ?>
  39.  </a></li>
  40. <?php }
  41. }
  42.  
  43. function andro_cart_shopping_bag(){
  44.  if(is_woocommerce_activated()){
  45.   ?>
  46.   <div class="shopping-bag">
  47.   <?php
  48.    if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) {
  49.     the_widget( 'WC_Widget_Cart', 'title=' );
  50.    } else {
  51.     the_widget( 'WooCommerce_Widget_Cart', 'title=' );
  52.    } ?>
  53.   </div>
  54. <?php
  55.  }
  56. }
  57.  
  58. add_filter( 'add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
  59.  
  60. if ( ! function_exists( 'woocommerce_header_add_to_cart_fragment' ) ) {
  61.  function woocommerce_header_add_to_cart_fragment( $fragments ) {
  62.   global $woocommerce;
  63.   ob_start();
  64.  ?>
  65.  
  66.  <?php
  67.  echo '<span class="cart-number-box';
  68.  $my_cart_count = $woocommerce->cart->cart_contents_count;
  69.  if ($my_cart_count > 0) {
  70.  echo ' active';
  71.  }
  72.  echo '">';
  73.  ?>
  74.  <?php
  75.   echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
  76.  ?></span>
  77.  
  78.  <?php
  79.  
  80.   $fragments['span.cart-number-box'] = ob_get_clean();
  81.  
  82.   return $fragments;
  83.  } // End woocommerce_header_add_to_cart_fragment()
  84. }
  85.  
  86.  
  87. After that add this code to your header.php
  88.  
  89. To show the cart count:
  90. <?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(); ?>
  91.  
  92. or
  93.    
  94. <?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>
  95.  
  96. To show the cart bag or cart Widget:
  97.  
  98. <?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>
  99.  
  100. Sample Header:
  101.  
  102. <div class="top-cart">
  103.     <div id="cart" class="btn-group btn-block">
  104.       <button type="button" data-toggle="dropdown" data-loading-text="Loading..." class="btn btn-inverse btn-block btn-lg dropdown-toggle">
  105.        <span class="top-cart-contain">
  106.         <span class="title-cart"></span>
  107.         <span id="cart-total">
  108.  
  109.         <?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(); ?>
  110.        
  111.         <?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>
  112.         </span>
  113.         <i class="fa fa-caret-down"></i>
  114.        </span>
  115.       </button>
  116.  
  117.       <ul class="dropdown-menu pull-right">
  118.        <li class="arrow-cart"></li>
  119.         <li>
  120.       <?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>
  121.        </li>
  122.       </ul>
  123.     </div>
  124.    </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement