Woocommerce customize coupon application order

This might be a simple one, but essentially i’m trying to customize the ORDER of the application of more than one coupon at checkout time.

I’m programmatically creating a coupon code (call it ‘coupon1’) using the ‘woocommerce_get_shop_coupon_data’ filter which at runtime creates a coupon whose value is determined by how many items in the cart. If they order 2 or 3 items, the cart price is set to a certain value and if they order 4 or more of these items, the cart price is set to a total regardless of how many additional items are ordered (anything over 4 items is ‘free’). That works great, however my client has made me aware of a situation where if they’ve previously purchased several items, they want to apply the store quantity credit based on the past order. In short, if they’ve bought 4 of these items previously but now want to add two more, they want to give the customer the ability to essentially add those two items free of charge. Here’s a sketch of my code of the custom coupon creation (before learning about this additional requirement):

add_filter('woocommerce_get_shop_coupon_data', function($false, $data, $coupon) {
global $woocommerce;
$number_of_applicable_cart_items = 0;
$applicable_items_subtotal = 0;
$two_tier_subtotal = 400;
$three_tier_subtotal = 500;
$max_subtotal = 600;
$my_slug = 'my-product';

$term = get_term_by( 'slug', $my_slug, 'product_cat' );
$cat_id = $term->term_id;
$cat_ids = array($term->term_id);

foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cat_id, $cart_item['data']->get_category_ids()) ) {
$applicable_items_subtotal += $cart_item['line_subtotal'];

switch($data) {
case 'multi-credit':
if ($number_of_applicable_cart_items == 2) {
} elseif ($number_of_applicable_cart_items == 3) {
} elseif ($number_of_applicable_cart_items >= 4) {
$coupon->set_amount($applicable_items_subtotal - $max_law_fair_subtotal);
} else {
// Do nothing.
return $coupon;
}, 10, 3);

My initial solution was to create ordinary coupons and give my client a coupon code (‘coupon2’) that they would in turn give to the customer to enter at order time to get that volume discount on a second order. The problem that I’m having is that the coupon that i’ve applied programmatically via the filter above is overriding the coupon entered by the customer. So, if a customer has two items in the cart, the programmatic discount (coupon1) that i’ve created of $200 is applied, however the 100% off coupon entered by the customer (coupon2) is not. The result is a coupon for $200, rather than the whole order being ‘free’ essentially. It appears (though i’m not certain) that coupon2 (entered by the customer) is being applied first, and then coupon1 is applied and resets the total. What i am attempting to get to is: (1) apply my programmatic discount on the cart items and THEN (2) apply the 100% discount code that the customer has entered on the screen.

Is there a way to customize the ORDER in which coupons are applied? If anyone has an idea, i’m open to suggestion. Cheers!


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer