如何在WooCommerce中创建仅适用于促销商品的优惠券?
Absolutely, this is a super common customization for WooCommerce stores—since the core plugin doesn’t have this option out of the box, but it’s totally doable with a bit of custom code. I’ve implemented this for multiple clients, so here’s a step-by-step solution:
Step 1: Add a Custom Checkbox to the Coupon Editor
First, we’ll add a simple checkbox on the coupon edit page so you can toggle whether the coupon should only apply to sale items. This makes it easy to enable this behavior for specific coupons without editing code every time.
Add this code to your theme’s functions.php file, or a custom plugin:
// Add custom checkbox to coupon edit page add_action( 'woocommerce_coupon_options', 'add_sale_only_coupon_checkbox', 10 ); function add_sale_only_coupon_checkbox() { woocommerce_wp_checkbox( array( 'id' => '_sale_only_coupon', 'label' => __( 'Only apply to on-sale products', 'woocommerce' ), 'description' => __( 'Check this box to restrict the coupon to products that are currently on sale.', 'woocommerce' ), 'desc_tip' => true, ) ); } // Save the custom checkbox value add_action( 'woocommerce_coupon_options_save', 'save_sale_only_coupon_checkbox', 10 ); function save_sale_only_coupon_checkbox( $post_id ) { $checkbox = isset( $_POST['_sale_only_coupon'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_sale_only_coupon', $checkbox ); }
Step 2: Add Validation Logic for the Coupon
Next, we’ll hook into WooCommerce’s coupon validation system to check if the coupon is marked as "sale only", and if the product is actually on sale. If the coupon should only apply to sale items but the product isn’t discounted, we’ll invalidate the coupon for that product.
Add this code right after the previous snippet:
// Validate coupon against on-sale products add_filter( 'woocommerce_coupon_is_valid_for_product', 'validate_sale_only_coupon', 10, 4 ); function validate_sale_only_coupon( $is_valid, $product, $coupon, $values ) { // Check if our custom checkbox is checked $sale_only = get_post_meta( $coupon->get_id(), '_sale_only_coupon', true ); if ( $sale_only === 'yes' ) { // For variable products, check if any variation is on sale if ( $product->is_type( 'variable' ) ) { $is_on_sale = $product->is_on_sale(); } else { // For simple products, check directly $is_on_sale = $product->is_on_sale(); } // If product isn't on sale, invalidate the coupon for this item if ( ! $is_on_sale ) { $is_valid = false; // Optional: Add a notice to explain why the coupon didn't apply wc_add_notice( __( 'This coupon only applies to on-sale products.', 'woocommerce' ), 'error' ); } } return $is_valid; }
Step 3: Test the Setup
- Create or edit a coupon in WooCommerce
- Check the new "Only apply to on-sale products" checkbox
- Add a mix of sale and regular products to your cart
- Apply the coupon—you’ll see it only discounts the sale items, and shows an error notice if you try to apply it to non-sale products
Notes for Edge Cases
- Variable Products: The code already handles variable products by checking if the parent product has any on-sale variations. If you want to restrict it only to the specific variation that’s on sale (not the entire variable product), you can modify the logic to check the variation’s
is_on_sale()instead of the parent. - Bundle Products: If you use product bundles, you’ll need to add extra logic to check each item in the bundle individually.
- Custom Discounts: If you have other custom coupon logic, make sure this filter runs after those (adjust the priority number if needed).
Let me know if you need help tweaking this for your specific store setup!
内容的提问来源于stack exchange,提问作者Mathias Beck

