php Woocommerce购物车数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Woocommerce购物车数量相关的知识,希望对你有一定的参考价值。

//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>

以上是关于php Woocommerce购物车数量的主要内容,如果未能解决你的问题,请参考以下文章

php WooCommerce:购物车数量和总数

php WOOCOMMERCE在购物车中重新加载商品数量

php WooCommerce - 在Ajax之后更新购物车中的商品数量和总数

php [WooCommerce Core]覆盖循环模板并显示添加到购物车按钮旁边的数量。

php [WooCommerce Core]覆盖循环模板并显示添加到购物车按钮旁边的数量。

WooCommerce - 更改数量触发购物车中的 AJAX 调用