我使用此代码过滤添加到某个类别(id = 70)的购物车的产品。
我的目标是计算添加到属于“70”类别的产品的购物车的步骤数量。
例子:
最小数量:0
步数:100
我将产品添加到购物车中,数量为 300。所以步数是3。
有没有办法获取添加到购物车的产品的步骤数量?
// Utility function that count specific product category cart items
add_shortcode('palletcompleto', 'cat_cart_count');
function cat_cart_count( $term_ids ) {
$quantity = 0; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], array( 70 ) ) ) {
$quantity += $cart_item['step_quantity'];
}
}
// Returning category count
return $quantity == 0 ? false : $quantity;
}
我正在使用has_product_category()
该线程中的一些类似函数的代码:
将商品数量设置为 Woocommerce 中特定类别的产品的“x”倍数
// Custom conditional function that checks also for parent product categories
function has_product_category( $product_id, $category_ids ) {
$term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$term_ids[] = $term->parent; // Set the parent product category
$term_ids[] = $term->term_id;
} else {
$term_ids[] = $term->term_id;
}
}
return array_intersect( $category_ids, array_unique($term_ids) );
}