如何获得添加到WooCommerce购物车中的步骤数量产品的价值?

时间:2018-10-30 08:06:44

标签: woocommerce count cart step quantities

我使用此代码来过滤添加到特定类别(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()函数的代码:
Set item quantity to multiples of “x” for products in a specific category in Woocommerce

// 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) );
}

0 个答案:

没有答案