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

Showing the number of items in WooCommerce mini cart

我正在为我的移动页眉创建一个购物车 link。我只想在购物车图标后显示购物车商品的数量。没有文字,只有数字。

我该怎么做?

我的代码是这样的:

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

谢谢。

你只需要使用get_cart_contents_count()中的方法WC_cartclass,这个方式:

<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