php - Woocommerce 中定义的产品类别中仅允许购物车中的一件商品

标签 php wordpress woocommerce product custom-taxonomy

在 Woocommerce 中,我发现了一些代码,可以将用户购买的类别限制为每个类别 a 或 b。因此,当前用户可以从 cat a 购买 2 件商品 1,从 cat b 购买 1 件。我想限制用户只能使用类别 a 或类别 b 中的一种产品。我正在使用的代码如下,在此先感谢。

add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {

$max_num_products = 1;// change the maximum allowed in the cart
$running_qty = 0;

$restricted_product_cats = array();

//Restrict particular category/categories by category slug
$restricted_product_cats[] = 'cat-a, cat-b';


// Getting the current product category slugs in an array
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;


// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){

    // Restrict $max_num_products from each category
    if( has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {

    // Restrict $max_num_products from restricted product categories
    //if( array_intersect($restricted_product_cats, $current_product_cats) && has_term( $restricted_product_cats, 'product_cat', $cart_item['product_id'] )) {

        // count(selected category) quantity
        $running_qty += (int) $cart_item['quantity'];

        // More than allowed products in the cart is not allowed
        if( $running_qty >= $max_num_products ) {
            wc_add_notice( sprintf( 'Only %s '.($max_num_products>1?'products from this category are':'product from this category is').' allowed in the cart.',  $max_num_products ), 'error' );
            $passed = false; // don't add the new product to the cart
            // We stop the loop
            break;
        }

    }
}
return $passed;}

最佳答案

试试这个(但它不处理数量,因为不清楚而且复杂得多,因为在购物车页面中它们可以更改):

add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity )
{
    // Accept when cart is empty
    if( WC()->cart->is_empty() ) return $passed;

    // HERE your product categories in this array (can be names, slugs or Ids)
    $categories = array('T-shirts', 'Hoodies');
    $found = $current = false;

    // Check the current product
    if( has_term( $categories, 'product_cat', $product_id ) ){
        $current = true;
    }

    // Loop through cart items checking for product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true;
            break; // stop the loop.
        }
    }

    // Current product and a cart item match with defined product categories
    if( $found && $current ){
        $passed = false;
        $cats_str = implode('" and "', $categories );
        wc_add_notice( sprintf( __('Only one item is allowed from "%s" categories…', 'woocommerce' ), $cats_str ), 'error' );
    }
    return $passed;
}

代码进入事件子主题(或事件主题)的 function.php 文件。测试和工作。

关于php - Woocommerce 中定义的产品类别中仅允许购物车中的一件商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459387/

相关文章:

php - Woocommerce 分组产品带有单独的 “Add To Cart” 按钮

css - 将谷歌验证码框集中在联系表 7 上

php - 将变量格式化为 map 上信息窗口中的链接

php - 检查两个数组是否共享任何公共(public)键

php - 我可以使用组连接的 2 种组合吗?或者我应该使用子查询

php - 在类别页面中显示最低的简单产品价格

php - 根据 WooCommerce 购物车中的运输类别显示自定义文本

php - 如何在 fputcsv 中使用 foreach?

javascript - 使用 jQuery 在 WooCommerce 上增加/减少数量

php - 循环遍历数据库表数据并以正确的格式显示它