为特定WooCommerce产品类别的购物车项目设置最小数量 [英] Set a minimum quantity for cart items from specific WooCommerce product category

查看:29
本文介绍了为特定WooCommerce产品类别的购物车项目设置最小数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我试图为特定产品类别中的购物车商品设置最小数量.

基于"

解决方案

您首先需要计算目标产品类别中的项目数...然后,当项目数低于定义的最小值时,您将显示错误通知:

  add_action('woocommerce_check_cart_items','wc_min_item_required_qty');函数wc_min_item_required_qty(){$ category ='游戏';//目标产品类别$ min_qty = 4;//所需的最低数量(针对每个项目)$ qty_count = 0;//初始化//遍历购物车项目foreach(WC()-> cart-> get_cart()as $ item){//计算目标产品类别中的商品if(has_term($ category,'product_cat',$ item ['product_id'])){$ qty_count + = $ item ['quantity'];}}//显示错误通知,避免结帐if($ qty_count!= 0&& $ qty_count< $ min_qty){wc_clear_notices();//清除所有其他通知//添加错误通知(并避免结帐).wc_add_notice(sprintf(__(您应该从%s类别中至少选择%s个项目.","woocommerce"),< strong>".$ min_qty.</strong>",< strong>".$ category.'</strong>'), '错误' );}} 

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

In WooCommerce, I am trying to set a minimum quantity for cart items from a specific product category.

Based on "Minimum cart item quantity for a specific product category in WooCommerce", here is my code attempt :

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $category      = 'games'; // The targeted product category
    $min_item_qty  = 4; // 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( 'You should at least pick', $min_item_qty ,'products  for'  ,$category,  'category' ), 'error' );
            break; // Stop the loop
        }
    }
}

It doesn't work as I would like as the a minimum quantity is set for the first cart item from the specific product category, but not globally for all items from that specific product category. Any help is appreciated.

解决方案

You need first to count the number of items from the targeted product category… then you can display the error notice when the number of items are below the minimum defined:

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $category  = 'Games'; // The targeted product category
    $min_qty   = 4; // Minimum Qty required (for each item)
    $qty_count = 0; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $item ) {
        // Count items from the targeted product category
        if( has_term( $category, 'product_cat', $item['product_id'] ) ) {
            $qty_count += $item['quantity'];
        }
    }

    // Display error notice avoiding checkout
    if( $qty_count != 0 && $qty_count < $min_qty ) {
        wc_clear_notices(); // Clear all other notices

        // Add an error notice (and avoid checkout).
        wc_add_notice( sprintf(
            __("You should pick at least %s items from %s category.", "woocommerce"),
            '<strong>' . $min_qty . '</strong>',
            '<strong>' . $category . '</strong>'
        ), 'error' );
    }
}

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

这篇关于为特定WooCommerce产品类别的购物车项目设置最小数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆