Woocommerce中仅允许购物车中已定义产品类别的一项 [英] Allow only one item in cart from defined product categories in Woocommerce

查看:59
本文介绍了Woocommerce中仅允许购物车中已定义产品类别的一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Woocommerce, I found a bit of code that restricts a users purchase to one per category for category a or b. So currently the user could purchase 2 items 1 from cat a and 1 from cat b. I would like to limit the user to only one product from category a or b. The code I am working with is below, thanks in advance.

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文件中.经过测试和工作.

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

这篇关于Woocommerce中仅允许购物车中已定义产品类别的一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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