显示WooCommerce迷你购物车中的商品数量

时间:2016-10-13 16:54:57

标签: php html wordpress woocommerce cart

我正在为我的移动标题创建购物车链接。我只想在购物车图标后显示购物车数量。没有文字,只有一个数字。

我该怎么做?

我的代码是这样的:

 <div class="mobile-cart">
 <a href="mywebsite.com/cart">
 <img src="cart.png">
 Here should be the code for no. of items
 </a>
 </div>

感谢。

1 个答案:

答案 0 :(得分:1)

您只需要使用 get_cart_contents_count() 类中的 WC_cart 方法,这样:

<div class="mobile-cart">
    <a href="mywebsite.com/cart">
         <img src="cart.png">
         <span><?php echo WC()->cart->get_cart_contents_count( ); ?></span>
     </a>
</div>

参考:WC_Cart class - get_cart_contents_count() method