douchun5969 2019-04-29 19:33
浏览 106
已采纳

WooCommerce中特定产品类别的最小购物车商品数量

In WooCommerce, I need to set up a minimum quantity for each item of a product category. I searched the forum and found some code that works fine except it only counts the Quantity for a product category in total:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    $minimum = 5; //Qty product

    if ( WC()->cart->cart_contents_count < $minimum ) {
        $draught_links = array();

        foreach(WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];

            $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $draught_links[] = $term->name;
            }   
        }

        if (in_array("Noten", $draught_links)){
            $on_draught = true;
        }else{
            $on_draught = false;
        }

        if( is_cart() ) {
            if($on_draught){
                wc_print_notice( 
                    sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' , 
                         $minimum , 
                         WC()->cart->cart_contents_count
                    ), 'error' 
                );
            }
        } else {
            if($on_draught){
                wc_add_notice( 
                    sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' , 
                        $minimum , 
                        WC()->cart->cart_contents_count
                    ), 'error' 
                );
            }
        }
    }
}

For example if I have two products (A and B) of belonging to the same product category and set up minimum quantity for this category to 5, the error message for the customer won't appear in this case:

  • Product A: 3
  • Product B: 2

I need a min quantity of 5 for every single product of that category.

Do you have an idea how to change and optimize the following code?

  • 写回答

1条回答 默认 最新

  • dqx76962 2019-04-29 22:47
    关注

    Since WooCommerce 3, your actual code is outdated and not convenient… There is multiple ways:

    1) The best way: Set up the minimum quantity at product level (for a product category):

    // On single product pages
    add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
    function min_qty_filter_callback( $args, $product ) {
        $category = 'Noten'; // The targeted product category
        $min_qty  = 5; // The minimum product quantity
    
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
        if( has_term( $category, 'product_cat', $product_id ) ){
            $args['min_value'] = $min_qty;
        }
        return $args;
    }
    
    // On shop and archives pages
    add_filter( 'woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2 );
    function min_qty_loop_add_to_cart_args( $args, $product ) {
        $category   = 'Noten'; // The targeted product category
        $min_qty    = 5; // The minimum product quantity
    
        $product_id = $product->get_id();
    
        if( has_term( $category, 'product_cat', $product_id ) ){
            $args['quantity'] = $min_qty;
        }
        return $args;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.

    2) Alternative way: Checking cart items and displaying an error message (similar to your code):

    add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
    function wc_min_item_required_qty() {
        $category      = 'Noten'; // The targeted product category
        $min_item_qty  = 5; // Minimum Qty required (for each item)
        $display_error = false; // Initializing
    
        // Loop through cart items
        foreach(WC()->cart->get_cart() as $cart_item ) {
            $item_quantity = $cart_item['quantity']; // Cart item quantity
            $product_id    = $cart_item['product_id']; // The product ID
    
            // For cart items remaining to "Noten" producct category
            if( has_term( $category, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
                wc_clear_notices(); // Clear all other notices
    
                // Add an error notice (and avoid checkout).
                wc_add_notice( sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.', $min_item_qty , $item_quantity ), 'error' );
                break; // Stop the loop
            }
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.


    To make it work for parent product category too, you will also add this custom function:

    // Custom conditional function that handle parent product categories too
    function has_product_categories( $categories, $product_id = 0 ) {
         // Initializing
        $parent_term_ids = $categories_ids = array();
        $taxonomy        = 'product_cat';
    
        if( is_string( $categories ) ) {
            $categories = (array) $categories;
        }
    
        $product_id = $product_id > 0 ? $product_id : get_the_id();
    
        // Convert categories term names and slugs to categories term ids
        foreach ( $categories as $category ){
            if( is_numeric( $category ) ) {
                $categories_ids[] = (int) $category;
            } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
                $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
            }
        }
    
        // 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 your active child theme (or active theme). Tested and work.

    Then in the existing code, you will replace:

    has_term( $category, 'product_cat', $product_id )
    

    by

    has_product_categories( $category, $product_id )
    

    That will allow you to handle parent product categories too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据