获取 Woocommerce 中的购物车项目数

Get the line cart items count in Woocommerce

我正在尝试找到一种从 woocommerce 购物篮中获取唯一购物车数量的方法。查看文档后,它只显示了一种获取总数的方法

get_cart_contents_count( )

有谁知道获取唯一计数的方法。例如,如果我添加 50 件产品 A 和 20 件产品 B,我只希望购物车数量为 2

这会成功(计算购物车订单项)

echo '<p>Line cart items count: ' . count( WC()->cart_get_cart() ) . '</p>';

已测试并有效。


对于订单项计数,它将是 (来自 WC_Order $order 对象):

echo '<p>Line order items count: ' . count( $order->get_items() ) . '</p>';