【问题标题】:Set item quantity to multiples of “x” for products in a specific different categories in WooCommerce对于 WooCommerce 中特定不同类别的产品,将项目数量设置为“x”的倍数
【发布时间】:2021-03-19 11:36:16
【问题描述】:

我正在尝试在我的网站的购物车页面上显示一条警报消息,管理 2 个不同类别的 2 个不同数量倍数。

分类如下:

  • Birre 33 cL (slug: birre-33-cl ID:187) 用于多个 6 瓶
  • Birre 75 cL (slug: birre-75-cl ID:188) 用于多个 12 瓶

我需要分别计数和管理(您可以购买 12/24/36...瓶 33 cL 和 6/12/18...瓶 75 cL,但不能购买 6 瓶 33 cL 和 6一瓶 75 cL)。

我已修改 this code 以满足我的需要:

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

不幸的是,我无法管理 2 个不同的类别,每个类别显示不同的消息。

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart taxonomy-terms


    【解决方案1】:

    更新

    以下将处理 2 个具有不同倍数的不同类别数组:

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

    代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

    • 非常感谢您的帮助!最初,您编写的代码在我的网站上不起作用:购物车中没有显示任何数量的两个类别的通知!然后我尝试用 $category_ids_1/2 更改 $term_names_1/2 并且现在可以完美运行!
    • @2sr Oups 对不起! ...我将函数 has_product_category() 更改为更好的函数 has_product_categories() 来处理产品类别名称、slug 或 Id。现在它工作得很好。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    • 很抱歉,我无法使编辑后的代码正常工作。只有我在第一条评论中为您写的解决方案在我的网站上有效(因此带有 category_ids 的“旧”代码)。新的既不适用于类别名称或 slug,也不适用于更改 category_ids。
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多