There is no option to auto-apply coupons when a certain product is in the WooCommerce cart by default.
However it is possible to do this with some custom code.
Here’s an example that will add an option to your coupons settings to select a product to link to that coupon. When the product is added to cart, the coupon will also be applied.
/**
* Add "Auto-Apply When Product In Cart" option to WooCommerce coupon settings.
*/
function wcusage1_add_coupon_auto_apply_option($coupon_id, $coupon) {
woocommerce_wp_select(array(
'id' => 'auto_apply_product',
'label' => __('Auto-Apply When Product In Cart', 'woocommerce'),
'description' => __('Select a product to trigger automatic coupon application.', 'woocommerce'),
'desc_tip' => true,
'options' => wcusage1_get_product_dropdown_options(),
'value' => get_post_meta($coupon_id, 'auto_apply_product', true),
));
?>
<script>
jQuery(function ($) {
$('#auto_apply_product').select2({
placeholder: '<?php _e('Select a product', 'woocommerce'); ?>',
allowClear: true,
});
});
</script>
<?php
}
add_action('woocommerce_coupon_options', 'wcusage1_add_coupon_auto_apply_option', 10, 2);
/**
* Save the custom field for the auto-apply product.
*/
function wcusage1_save_coupon_auto_apply_option($coupon_id, $coupon) {
if (isset($_POST['auto_apply_product'])) {
update_post_meta($coupon_id, 'auto_apply_product', sanitize_text_field($_POST['auto_apply_product']));
}
}
add_action('woocommerce_coupon_options_save', 'wcusage1_save_coupon_auto_apply_option', 10, 2);
/**
* Get WooCommerce products as dropdown options.
*/
function wcusage1_get_product_dropdown_options() {
$products = wc_get_products(array(
'status' => 'publish',
'limit' => -1,
'return' => 'objects',
));
$options = array('' => __('None', 'woocommerce'));
foreach ($products as $product) {
$options[$product->get_id()] = $product->get_name();
}
return $options;
}
/**
* Auto-apply coupons if a matching product is in the cart.
*/
function wcusage1_auto_apply_coupon() {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$cart = WC()->cart;
$cart_product_ids = array();
// Get all product IDs in cart
foreach ($cart->get_cart() as $cart_item) {
$cart_product_ids[] = $cart_item['product_id'];
}
// Get all coupons with the auto-apply product set
$args = array(
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$coupons = get_posts($args);
foreach ($coupons as $coupon) {
$coupon_code = $coupon->post_title;
$auto_apply_product = get_post_meta($coupon->ID, 'auto_apply_product', true);
// Check if the product is in the cart
if ($auto_apply_product && in_array($auto_apply_product, $cart_product_ids)) {
if (!$cart->has_discount($coupon_code)) {
$cart->apply_coupon($coupon_code);
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'wcusage1_auto_apply_coupon');
/**
* Remove auto-applied coupon if the corresponding product is removed from the cart.
*/
function wcusage1_remove_auto_applied_coupon($cart_item_key) {
$cart = WC()->cart;
$cart_product_ids = array();
// Get all product IDs in cart
foreach ($cart->get_cart() as $cart_item) {
$cart_product_ids[] = $cart_item['product_id'];
}
// Get all coupons with the auto-apply product set
$args = array(
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$coupons = get_posts($args);
foreach ($coupons as $coupon) {
$coupon_code = $coupon->post_title;
$auto_apply_product = get_post_meta($coupon->ID, 'auto_apply_product', true);
// Check if the product is in the cart
if ($auto_apply_product && !in_array($auto_apply_product, $cart_product_ids)) {
if ($cart->has_discount($coupon_code)) {
$cart->remove_coupon($coupon_code);
}
}
}
}
add_action('woocommerce_cart_item_removed', 'wcusage1_remove_auto_applied_coupon');
The code may not be 100% complete or tested properly. Please ensure to do your own testing and create website backups before using custom code.