我试图从WooCommerce中的特定产品类别获取购物车中的商品数量.
我正在为酿酒厂做一个网站.它含有酒精和非酒精产品.所有的葡萄酒都属于'葡萄酒'或类别ID 34的主要类别,其下有许多子类别和产品.
对于任何属于此类别的东西......我需要知道此类别下购物车中有多少件物品.如果有六瓶葡萄酒,无论它们是相同的产品ID还是6种不同的产品ID.我需要从'wine'类别或34类别ID中获得6个数字.
我试过这个没有成功.
我是WooCommerce的新手,也是面向对象的新手.
谢谢
function cat_cart_count( $cat_name ) {
// $cat_name variable is for you normally "tshirts" or "shorts"
global $woocommerce; $cat_count = 0;
// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) {
$_product_id = $values['product_id']; // product ID
$_product_qty = $values['quantity']; // product quantity
// Getting categories of the product (could be more than one)
$terms = get_the_terms( $_product_id, 'product_cat' );
// Checking this product has a category
if ( $terms && …
Run Code Online (Sandbox Code Playgroud)