繁体   English   中英

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

[英]Minimum cart item quantity for specific product categories in WooCommerce

在 WooCommerce 中,我需要为产品类别的每个项目设置最低数量。 我搜索了论坛,发现一些代码工作正常,除了它只计算产品类别的总数量:

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

例如,如果我有两个属于同一产品类别的产品(A 和 B),并将该类别的最小数量设置为 5,则在这种情况下不会出现客户的错误消息:

  • 产品A:3
  • 产品 B:2

对于该类别的每个产品,我需要至少 5 个。

您知道如何更改和优化以下代码吗?

自 WooCommerce 3 以来,您的实际代码已过时且不方便……有多种方法:

1). 最好的方法:在产品级别设置最小数量(对于产品类别):

// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
    $categories = array('Noten'); // The targeted product category(ies)
    $min_qty    = 5; // The minimum product quantity

    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if( has_term( $categories, '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 ) {
    $categories = array('Noten'); // The targeted product category
    $min_qty    = 5; // The minimum product quantity

    $product_id = $product->get_id();

    if( has_term( $categories, 'product_cat', $product_id ) ){
        $args['quantity'] = $min_qty;
    }
    return $args;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。 测试和工作。

2). 替代方法:检查购物车项目并显示错误消息(类似于您的代码)

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $categories    = array('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( $categories, '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
        }
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。 测试和工作。


为了使其也适用于父产品类别,您还将添加此自定义函数:

// 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 文件。 测试和工作。

然后在现有代码中,您将替换:

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

经过

has_product_categories( $category, $product_id )

这将允许您也处理父产品类别。

要使其适用于某个产品类别的项目总数,请改用以下代码:

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $categories    = '30'; // The targeted product category slug or ID
    $max_item_qty  = 10; // Minimum Qty required (for each item)
    $display_error = false; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        
        $product_id    = $cart_item['product_id']; // The product ID
        
    if( has_term( $categories, 'product_cat', $product_id )) {
        $item_quantity += $cart_item['quantity']; // Cart item quantity
    }
        // For cart items remaining to "Pizza" product category
        if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity > $max_item_qty ) {
            
            wc_clear_notices(); // Clear all other notices
            
            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf( 'We can only take orders of up to 10 burgers per customer', $max_item_qty , $item_quantity ), 'error' );
            break; // Stop the loop
        }
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM