简体   繁体   English

在 WooCommerce 中为特定不同类别的产品设置项目数量为“x”的倍数

[英]Set item quantity to multiples of “x” for products in a specific different categories in WooCommerce

I am trying to display an alert message on the cart page of my web site managing 2 different quantity multiples for 2 different categories.我试图在我的 web 站点的购物车页面上显示一条警报消息,该站点管理 2 个不同类别的 2 个不同数量倍数。

Categories are:类别是:

  • Birre 33 cL (slug: birre-33-cl ID:187) for Multiple quantities of 6 Birre 33 cL (slug: birre-33-cl ID:187) 多数量 6
  • Birre 75 cL (slug: birre-75-cl ID:188) for Multiple quantities of 12 Birre 75 cL (slug: birre-75-cl ID:188) 适用于 12 多个数量

I need to count and manage them separately (you can buy 12/24/36... bottles of 33 cL and 6/12/18... bottles of 75 cL, but not 6 bottles of 33 cL and 6 bottle of 75 cL).我需要分别计数和管理(您可以购买 12/24/36... 瓶 33 cL 和 6/12/18... 瓶 75 cL,但不能购买 6 瓶 33 cL 和 6 瓶 75 cL)。

I have modified this code to suit my needs:我已修改此代码以满足我的需要:

//FILL THE BOX BIRRE 33 CL
function has_product_category( $product_id, $category_ids ) {
    $term_ids = array(); 

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

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 12;
    $total_products = 0;
    $category_ids = array( 187 );
    $found = false;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_product_category( $cart_item['product_id'], $category_ids ) ) {
            $total_products += $cart_item['quantity'];
            $found = true;
        }
    }
    if ( ( $total_products % $multiples ) > 0 && $found )
        wc_add_notice( sprintf( __('FILL YOUR 33 cL BOX: you have to buy multiples of %s.', 'woocommerce', 'woocommerce'), $multiples ), 'error' );
}

Unfortunately I am not able to manage to 2 different categories with different displayed message for each.不幸的是,我无法管理 2 个不同的类别,每个类别都有不同的显示消息。

Any help is appreciated.任何帮助表示赞赏。

Updated更新

The following will handle 2 different arrays of categories with different multiples:以下将处理具有不同倍数的类别的 2 个不同 arrays:

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $term_names_1 = array('Birre 75 cL'); // 1st array of term names
    $term_names_2 = array('Birre 33 cL'); // 2nd array of term names
    $multiples_6  = 6;  // 1st multiples (for 75 cl)
    $multiples_12 = 12; // 2nd multiples (for 33 cl)
    
    $total_products_1 = $total_products_2 = 0; // Initializing
    
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // count items | 75 cl (by 6)
        if ( has_product_categories( $cart_item['product_id'], $term_names_1 ) ) {
            $total_products_1 += $cart_item['quantity'];
        }
        // count items | 33 cl (by 12)
        elseif ( has_product_categories( $cart_item['product_id'], $term_names_2 ) ) {
            $total_products_2 += $cart_item['quantity'];
        }
    }
    
    // Notice for 75 cl (by 6) 
    if ( $total_products_1 > 0 && ( $total_products_1 % $multiples_6 ) > 0 ) {
        wc_add_notice( sprintf( 
            __('You need to buy in quantities of %s items from %s %s'), 
            $multiples_6, explode(', ', $term_names_1), _n('category', 'categories', count($multiples_6))
        ), 'error' );
    }
    
    // Notice for 33 cl (by 12) 
    if ( $total_products_2 > 0 && ( $total_products_2 % $multiples_12 ) > 0 ) {
        wc_add_notice( sprintf( 
            __('You need to buy in quantities of %s items from %s %s'), 
            $multiples_12, explode(', ', $term_names_2), _n('category', 'categories', count($multiples_12))
        ), 'error' );
    }  
}

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于 Woocommerce 中特定类别中的产品,将项目数量设置为“x”的倍数 - Set item quantity to multiples of “x” for products in a specific category in Woocommerce 更改woocommerce中特定产品的购物车项目数量 - Change the cart item quantity for specific products in woocommerce WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce 以编程方式为 WooCommerce 中的特定变量产品设置最小、最大和步数 - Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce 在 WooCommerce 中为特定类别的产品设置自定义数量参数 - Set custom quantity arguments to products from specific category in WooCommerce 将总购物车数量限制为 WooCommerce 中的特定倍数 - Restrict total cart quantity to specific multiples in WooCommerce Woocommerce 按特定类别获取产品 - Woocommerce get products by specific categories 在 WooCommerce 中为特定产品或类别设置最小订单金额 - Set minimum Order amount for specific Products or Categories in WooCommerce 当2个不同类别的产品时,更改woocommerce购物车页面中的“数量”文本 - Change the “Quantity” text in the woocommerce cart page when 2 products from different categories WooCommerce 中特定产品类别的十进制数量步长 - Decimal quantity step for specific product categories in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM