简体   繁体   English

我想限制它只添加一个类别的产品,不允许其他人使用 woocommerce

[英]I want to limit it to add only products from one category and not allow others with woocommerce

I'm trying to limit it, so I can only add products with the same category and not allow others, returning a message, but when I try to add another product from the same category, it returns the defined error message.我试图限制它,所以我只能添加具有相同类别的产品而不允许其他产品,返回一条消息,但是当我尝试添加来自同一类别的另一个产品时,它返回定义的错误消息。 My code:我的代码:

    // define your product category (can be term IDs, slugs or names)
    $category = 'courses';

    // close if cart is empty
    if( WC()->cart->is_empty() )
        return $passed;

    // check cart: search by course category items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
            // display a warning message
            wc_add_notice( __('Not possible to add courses together with products.', 'woocommerce' ), 'error' );
            // avoid adding to cart
            return false;
        }
    }
    return $passed;
}

I believe you only need to fix this line by adding "!"我相信你只需要通过添加“!”来修复这一行。 as you're looking to deny different categories:因为你想拒绝不同的类别:

if ( ! has_term( $category, 'product_cat', $cart_item['product_id'] ) )

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

相关问题 在WooCommerce中仅允许从相同父类别的产品添加到购物车 - Only allow add to cart from products of the same parent category in WooCommerce WooCommerce:只允许购物车中相同类别的产品 - WooCommerce: Only allow products from the same category in cart Woocommerce购物车中仅允许两种产品中的一种 - Allow only one from two products in Woocommerce cart 仅从一种自定义分类法中限制购物车中的 WooCommerce 产品 - Limit WooCommerce products in cart only from one custom taxonomy 如果一个类别中只有一个产品,则将其他类别中的随机 WooCommerce 产品显示为相关产品 - Show random WooCommerce products from other categories as related products if only one product in a category "允许将 Woocommerce 中特定产品类别的最多 3 个产品添加到购物车" - Allow add to cart 3 products max for a specific product category in Woocommerce 如何限制用户在 WooCommerce 的购物车中添加最多 6 个产品 - How to limit a user to add max 6 products of each category in cart in WooCommerce 限制Woocommerce产品类别档案中显示的产品 - Limit displayed products on Woocommerce product category archives 只允许为 Woocommerce 中的特定产品类别购买一种产品 - Allow only one product purchase for a specific product category in Woocommerce 在 Woocommerce 中一次只允许购物车中的一个产品类别 - Allow only one product category in cart at once in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM