我正在研究Woocommerce主题,我需要仅获取购物车中的物品数量,而无需总计或其他加价。我尝试使用
<?php echo WC()->cart->get_cart_contents_count(); ?>
但是它返回两个spans.amount和span.count,并且在span.count内部也有字符串“ items”
有没有一种方法可以只返回购物车中的物品数量,最好以int形式返回?
更新:
因此,使用以下代码时:
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>">
<?php echo WC()->cart->get_cart_contents_count(); ?>
</a>
返回了上面的额外标记,但是通过注释掉锚标记,如下所示:
<!-- <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"> -->
<?php echo WC()->cart->get_cart_contents_count(); ?>
<!-- </a> -->
仅返回一个整数。
那是为什么?