You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce批发用户购物车数量规则异常问题求助

Fix: Restrict Cart/Checkout Quantity Rules to Wholesale Users Only

Got it, let's sort out this issue for you! The problem in your original code lies in the conditional logic for cart/checkout pages—right now, the is_cart() || is_checkout() check is being evaluated independently of the user role and product tag conditions, which is why the quantity rules are applying to all users. Let's fix that with adjusted condition checks and ensure we correctly verify product tags even on the cart page.

Here's the corrected code:

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
    // Check if we're on cart/checkout, product has 'mug' tag, AND user is wholesale
    if ( ( is_cart() || is_checkout() ) && has_term( 'mug', 'product_tag', $product->get_id() ) && current_user_can('wholesale') ) {
        $args['input_value'] = 36; // Start quantity at 36
        $args['min_value'] = 36; // Minimum allowed quantity
        $args['step'] = 36; // Increment/decrement by 36
    } elseif ( is_product() && has_term( 'mug', 'product_tag', $product->get_id() ) && current_user_can('wholesale') ) {
        // Apply same rules to single product pages for wholesale users
        $args['input_value'] = 36;
        $args['min_value'] = 36;
        $args['step'] = 36;
    } else {
        // Default quantity rules for all other scenarios
        $args['input_value'] = 1;
        $args['min_value'] = 1;
        $args['step'] = 1;
    }
    return $args;
}

// Handle quantity rules for product variations
add_filter( 'woocommerce_available_variation', 'jk_woocommerce_available_variation' );
function jk_woocommerce_available_variation( $args ) {
    // Get the parent product of the variation (tags are usually applied to parent)
    $parent_product_id = wc_get_product( $args['variation_id'] )->get_parent_id();
    
    if ( has_term( 'mug', 'product_tag', $parent_product_id ) && current_user_can('wholesale') ) {
        $args['input_value'] = 36;
        $args['min_value'] = 36;
        $args['step'] = 36;
    } else {
        $args['min_value'] = 1;
        $args['step'] = 1;
        $args['input_value'] = 1;
    }
    return $args;
}

Key Fixes Breakdown:

  • Conditional Logic Restructuring: Wrapped is_cart() || is_checkout() in parentheses so it's treated as a single condition, then combined with the product tag and user role checks using &&. This ensures all three conditions must be true for the wholesale rules to kick in.
  • Cart Page Product Tag Check: Added $product->get_id() to the has_term call—this ensures we're checking the tag of the specific product in the cart, instead of relying on the current page's product (which doesn't work when the cart has multiple items).
  • Clearer Single Product Page Handling: Added a dedicated condition for single product pages to make the logic easier to read, while ensuring wholesale users see the correct quantity rules there too.
  • Variation Parent Tag Check: For product variations, we now check the parent product's tag (since tags are almost always applied to the parent, not individual variations).

How to Verify:

  1. Log in as a user with the wholesale role, add a product tagged mug to your cart, and check cart/checkout pages—you'll only be able to adjust quantities in steps of 36, starting at 36.
  2. Log in as a regular user (without the wholesale role) and repeat the above—you'll see the default quantity rules (start at 1, step by 1).

内容的提问来源于stack exchange,提问作者user2059376

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:08:29