当前位置: 首页 > 知识库问答 >
问题:

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

柯昆杰
2023-03-14

在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
  • 产品乙:2

我需要一个最小的数量5的每一个产品的类别。

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

共有2个答案

谭健柏
2023-03-14

要使其适用于特定产品类别项目总数,请改用此代码:

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

艾凯捷
2023-03-14

由于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 )

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

 类似资料:
  • 问题内容: 我有上面的代码。 如果我在cart_item上运行print_r,我将得到一个多维数组: 我如何只获得product_id? 我试过$ test = 没用 问题答案: 要获取 foreach循环中的每个购物车商品(对于简单产品): 如果是可变产品,请获取 : 或在两种情况下 ( Woocommerce 3+中 的 Object在哪里) : 更新: 循环外使用产品ID 1)打破循环 (仅

  • 在WooCommerce中,我试图为特定产品类别的购物车项目设置最低数量。 基于“WooCommerce中特定产品类别的最低购物车项目数量”,以下是我的代码尝试: 它不能像我希望的那样工作,因为为特定产品类别中的第一个购物车项目设置了最低数量,但不是为该特定产品类别中的所有项目全局设置。感谢您的帮助。

  • 我怎样才能使它像预期的那样工作呢? 下面是它在这个链接上的实际工作方式。

  • 我正在尝试将我的WooCommerce档案页面上特定产品类别的add to cart按钮更改为“Read More”按钮,该按钮将链接到产品页面(但不链接到单个产品页面本身)。 使用“将添加到购物车按钮替换为WooCommerce 3中的shop pages中的read more链接到product页面”的答案代码,如何将其限制为仅有一个产品类别(本例中术语名称为“Classs”)? 感谢任何帮助

  • 勾选了复选框的产品实际上是不可购买的,这是期望的结果。 我遇到的问题是,当我在产品目录页面上为可购买的产品(那些没有勾选的)点击“添加到购物车”时,我被重定向到产品页面,并且默认的WooCommerce消息“对不起,这个产品不能被购买。”出现。应该发生的是,当单击“添加到购物车”按钮时,产品会自动添加到购物车中。 同样从单品页面,我可以毫无问题地添加可购推车。