基于Product Tag为WooCommerce商品设置最小购买量及步长的问题
WooCommerce: Set Minimum Quantity (12 or Multiples) for Products with Specific Product Tag
I've got you covered! Let's fix both issues you're facing: getting the product tag check working properly, and adding front-end quantity restrictions so users can't even add invalid quantities to the cart in the first place (instead of just showing an error at checkout).
Why Your Original Code Failed with Product Tags
While swapping product_cat to product_tag in has_term() is technically correct, your original code had a few critical issues:
- The
$total_quantityvariable wasn't initialized before use - It used redundant loops that made the logic messy
- Most importantly, it only checked at checkout instead of preventing invalid quantities upfront
Complete Solution: Front-End + Back-End Validation
Here's a robust, two-part solution that handles both product page restrictions and cart/checkout fallback validation:
// Restrict quantity input on product pages for specific product tag add_filter( 'woocommerce_quantity_input_args', 'restrict_quantity_for_tagged_products', 10, 2 ); function restrict_quantity_for_tagged_products( $args, $product ) { // Replace 'your-target-tag' with your actual product tag slug (find this in WP Admin > Products > Tags) $target_tag = 'your-target-tag'; $min_qty = 12; // Check if the current product has the target tag if ( has_term( $target_tag, 'product_tag', $product->get_id() ) ) { $args['min_value'] = $min_qty; // Minimum quantity allowed $args['step'] = $min_qty; // Force quantity to increment by 12 each time $args['input_value'] = $min_qty; // Set default quantity to 12 } return $args; } // Validate quantity when adding to cart (blocks manual URL hacks or console edits) add_filter( 'woocommerce_add_to_cart_validation', 'validate_tagged_product_quantity', 10, 3 ); function validate_tagged_product_quantity( $passed, $product_id, $quantity ) { $target_tag = 'your-target-tag'; $min_qty = 12; if ( has_term( $target_tag, 'product_tag', $product_id ) ) { // Check if quantity is at least the minimum AND a multiple of it if ( $quantity < $min_qty || $quantity % $min_qty !== 0 ) { wc_add_notice( sprintf( '<strong>Products with this tag require a minimum purchase of %1$s items, or multiples of %1$s.</strong>', $min_qty ), 'error' ); $passed = false; } } return $passed; } // Fallback validation for cart/checkout (in case quantity was modified after adding) add_action( 'woocommerce_check_cart_items', 'validate_cart_for_tagged_products' ); function validate_cart_for_tagged_products() { if ( is_cart() || is_checkout() ) { $target_tag = 'your-target-tag'; $min_qty = 12; $cart_is_valid = true; foreach ( WC()->cart->get_cart() as $cart_item ) { $product_id = $cart_item['product_id']; $quantity = $cart_item['quantity']; if ( has_term( $target_tag, 'product_tag', $product_id ) ) { if ( $quantity < $min_qty || $quantity % $min_qty !== 0 ) { wc_add_notice( sprintf( '<strong>"%s" requires a minimum purchase of %1$s items, or multiples of %1$s.</strong>', $cart_item['data']->get_name(), $min_qty ), 'error' ); $cart_is_valid = false; } } } // Disable checkout button if invalid quantities are present if ( !$cart_is_valid ) { remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 ); } } }
How This Works:
Product Page Restriction:
- Modifies the quantity input field to only allow values starting at 12, incrementing by 12 (so users can pick 12, 24, 36, etc.)
- Sets the default quantity to 12 to guide users to the required minimum
Add to Cart Validation:
- Blocks any attempt to add invalid quantities (even if a user edits the input via browser console or modifies the add-to-cart URL)
- Shows a clear error message immediately when an invalid quantity is entered
Cart/Checkout Fallback:
- Double-checks all items in the cart to ensure no invalid quantities slipped through
- Disables the checkout button if any tagged product has an invalid quantity, preventing users from proceeding
Quick Setup Notes:
- Replace
'your-target-tag'with the slug of your product tag (you can find this in WordPress Admin > Products > Tags, edit your tag and look at the URL) - Adjust
$min_qty = 12to your desired minimum quantity if needed - Add this code to your child theme's
functions.phpfile or a custom plugin (never edit the parent theme directly, as changes will be lost on updates)
内容的提问来源于stack exchange,提问作者Florina Adriana
相关产品推荐
相关产品推荐

