Get In Touch
Goslings Barn, Sheepcotes Lane,
Braintree, Essex.
CM77 8ER
Tel: +44 (0) 1376 584912

Woocommerce URL Coupons Applying the Wrong Code

I recently ran in to a strange problem when applying coupons via URLs in Woocommerce. I had it set up using the Woocommerce add-on for URL coupons. this is where a coupon can be applied to the shopping basket by passing it in the URL. So if you had a discount code ‘december19’, you could simply give your customers the URL and your chosen product and discount would be added to oyur shopping cart.

Now, the problem arose when similar coupon codes were being used for different discounts, particularly when the first part of the code was the same, so like ‘jan’, janr1′ and so on. So inputting the code ‘janr1’ would result in the discount set up for the code ‘jan’ being applied.

I then realised that this is actually how the URL coupons add-on is intended to work, it’s to allow you to personalise the coupon codes you give to your customers. This feature can be disabled by adding a trailing slash, but it’s not always convienient, and in my case there were a number of codes that had already been published, so a solution was needed, and fast!.

Digging through the code of the URL coupons add-on it became clear the Woocommerce developers had added a filter that helpfully allows the coupon look up process to be hooked into.

To disable the feature you simply add the code below into your theme’s or child theme’s functions.php file, and any coupons added will require an exact match to be processed.


//Woocommerce URL Coupons exact match function cq_coupon_exact_match($url_matches, $url, $coupon_url) { if ($url == $coupon_url) { return true; } else { return false; } } add_filter( 'wc_url_coupons_url_matches_coupon', 'cq_coupon_exact_match', 10, 3);

This simply looks to see if the full URL is an exact match to the full coupon url, and will only return true when that is the case.

Author avatar
Chris Quee