Restrict the usage of WooCommerce coupons unless required products are in the cart.
This plugin allows you to restrict the usage of WooCommerce coupons unless specific products or variations are in the cart.
You can specify which products are required for a coupon to be valid, providing more control over your discount strategies.
- Restrict coupon usage based on all required products (including variations) being in the cart.
- Customize the error message via a filter.
- Upload the plugin files to the
/wp-content/plugins/runthings-wc-coupons-required-productsdirectory, or install the plugin through the WordPress plugins screen directly. - Activate the plugin through the 'Plugins' screen in WordPress.
- Go to WooCommerce area of the admin panel, and look under Marketing > Coupons and edit or create a coupon.
- In the "Usage restriction" tab, you will see the option to select required products for the coupon.
This filter allows third-party plugins to alter the missing required products before the coupon is rejected.
| Parameter | Type | Description |
|---|---|---|
$missing_products |
array |
The missing products in the cart, in the format [product_id => quantity]. |
$coupon |
WC_Coupon |
The coupon object being validated. |
$required_products |
array |
The required products for the coupon, in the format [product_id => quantity]. |
$cart_products |
array |
Products found in the cart, in the format [product_id => quantity]. |
$cart |
WC_Cart |
The cart instance. |
Allow a custom packed product to satisfy a required product ID:
add_filter('runthings_wc_coupons_required_products_missing_products', function ($missing_products, $coupon, $required_products, $cart_products, $cart) {
foreach ($cart->get_cart() as $cart_item) {
if (!empty($cart_item['custom_pack_product_id'])) {
$pack_product_id = (int) $cart_item['custom_pack_product_id'];
unset($missing_products[$pack_product_id]);
}
}
return $missing_products;
}, 10, 5);Only satisfy a required product if the packed quantity meets the requirement:
add_filter('runthings_wc_coupons_required_products_missing_products', function ($missing_products, $coupon, $required_products, $cart_products, $cart) {
foreach ($cart->get_cart() as $cart_item) {
if (empty($cart_item['custom_pack_product_id']) || empty($cart_item['custom_pack_product_qty'])) {
continue;
}
$pack_product_id = (int) $cart_item['custom_pack_product_id'];
$pack_quantity = (int) $cart_item['custom_pack_product_qty'];
if (isset($missing_products[$pack_product_id]) && $pack_quantity >= $missing_products[$pack_product_id]) {
unset($missing_products[$pack_product_id]);
}
}
return $missing_products;
}, 10, 5);This filter allows customization of the error message shown when a coupon is not valid due to missing required products.
| Parameter | Type | Description |
|---|---|---|
$message |
string |
The default error message, e.g., "This coupon requires specific products in the cart.". |
$coupon |
WC_Coupon |
The coupon object being validated. |
$required_products |
array |
The required products for the coupon, in the format [product_id => quantity]. |
$missing_products |
array |
The missing products in the cart, in the format [product_id => quantity]. |
A simple static message replacement:
add_filter('runthings_wc_coupons_required_products_error_message', 'custom_coupon_required_products_error_message');
function custom_coupon_required_products_error_message($message) {
return __('Custom error message for invalid coupon.', 'your-theme');
}add_filter('runthings_wc_coupons_required_products_error_message', function ($error_message, $coupon, $required_products, $missing_products) {
$missing_product_titles = [];
foreach ($missing_products as $product_id => $quantity) {
$product = wc_get_product($product_id);
if ($product) {
$missing_product_titles[] = $product->get_name();
}
}
if (!empty($missing_product_titles)) {
$error_message = sprintf(
__('This coupon requires specific products in the cart. You still need to add the following products: %s', 'your-theme'),
implode(', ', $missing_product_titles)
);
}
return $error_message;
}, 10, 4);The built-in WooCommerce "Products" field allows you to specify products that the coupon can be applied to.
This means that the discount will be applied if any of the specified products are in the cart.
In contrast, this plugin restricts the usage of the coupon unless all of the specified products are in the cart.
This means that the coupon will not be valid unless every required product is present in the cart.
Edit the coupon and go to the "Usage restriction" tab.
In the "Required products" section, select the products that must be in the cart for the coupon to be valid.
Yes, this plugin works alongside other WooCommerce coupon restrictions such as minimum spend, maximum spend, and role restrictions.
Specific variations are supported in the required products selector.
The plugin has been kept deliberately simple for now, because this met the client's specific needs.
There are lots of other combinations of requirements that I can think of like excluding products, involving quantities, requiring x products in a category, etc.
If you have a specific requirement, and time allows, I'll try to implement it.
Please open an issue on the repo, over at GitHub.
-
Example denied coupon usage due to missing required products.

-
Example denied coupon usage due to missing required products, with custom message.

- Add
runthings_wc_coupons_required_products_missing_productsfilter for custom required-product validation. - Document variation support and the new filter.
- Fix required product checks to support product variations.
- Fixed missing "And" separator in the coupon usage restriction panel to match WooCommerce core styling.
- Move plugin directory assets to .wordpress-org/ folder.
- Bump WordPress tested up to field to support 6.9 branch.
- Bump WordPress tested up to field to support 6.8 branch.
- Fixed a bug which caused a valid voucher to fail to verify.
- Incorporate amends requested for WordPress plugin directory submission.
- Filter
runthings_wc_coupons_required_products_error_messageto customize error message. - Filter
runthings_wc_coupon_required_products_error_messagedeprecated.
- Initial release.
- Restrict coupons by required products.
- Filter
runthings_wc_coupon_required_products_error_messageto customize error message.
If you have a specific requirement or find a bug, please open an issue on the GitHub repository.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/gpl-3.0.html.
Icon - Discount by Gregor Cresnar, from Noun Project, https://thenounproject.com/browse/icons/term/discount/ (CC BY 3.0)
Icon - restriction by Puspito, from Noun Project, https://thenounproject.com/browse/icons/term/restriction/ (CC BY 3.0)
