Problem
I recently got asked if it was possible to have two discounts built into one coupon code in WooCommerce. The store owner had an add-on service for products that costs $5. So in a mailer, they wanted to send out a flat $5 discount coupon code to encourage sales of this add-on service. But they also wanted to give a 10% discount as well to further encourage customers to visit the site again.
Solution
In WooCommerce, you can create flat discounts using the “Cart Discount” coupon type and you can create percent discounts using the “Cart % Discount” type. But the two can’t be used at the same time in a single coupon, and requiring a customer to enter two coupon codes isn’t the best option. So the solution to this problem was to chain the coupons together using the following snippet:
add_action( 'woocommerce_applied_coupon', 'wc_ninja_chain_a_coupon' );
function wc_ninja_chain_a_coupon( $coupon_code ) {
$first_coupon = 'code-in-mailer';
$chained_coupon = 'code-2';
if ( $first_coupon == $coupon_code ) {
WC()->cart->add_discount( $chained_coupon );
}
}
With the above code, if a customer uses the coupon “code-in-mailer” then coupon “code-2” is automatically added as well. If you want to also prepare for the second coupon code to be added first, you can change the conditional statement to this to chain the second coupon to the first:
if ( $first_coupon == $coupon_code ) {
WC()->cart->add_discount( $chained_coupon );
} elseif ( $chained_coupon == $coupon_code ) {
WC()->cart->add_discount( $first_coupon );
}
Not sure where to place this code? Here is a guide on correctly adding custom code to WooCommerce.
[…] can chain two coupon codes together with WooCommerce (to apply two discounts at once) using this tutorial from Caleb […]
Thanks Caleb, I was searching for something like this.