Whilst working on the brand new Hildon website I encountered an issue with WooCommerce coupons.
You can’t create quantity-based coupons out of the box and whilst this functionality is available through plugins such as Dynamic Pricing, I’m a cheap-skate who doesn’t want to pay for plugins when I can do something for free – plus programmatically creating/amending coupons allows for greater control.
In case you’re unaware, Hildon produce some of the purest Natural Mineral Water in the UK and you can find bottles of Hildon in some of the finest establishments and restaurants around the World. They sell their bottles of Natural Mineral Water in cases and that is how quantities are measured online.
Hildon only have two products – Delightfully Still Natural Mineral Water and Gently Sparkling Natural Mineral Water but there are up to 19 variations of those products.
A generous offer
Hildon like to reward their customers and they have a fantastic offer whereby if you order six or more cases of Natural Mineral Water, you get a 5% discount applied to your basket. If you order 12 or more cases, this offer increases to 10% off.
As mentioned already, discount-based coupons are not available in WooCommerce out of the box. Fear not – we can fix this issue quite easily.
Firstly – Create a WooCommerce coupon:
- Log in to WordPress and create a new coupon;
- Add a title for the coupon – this is important as it will be referenced in code;
- You can now set up the rest of the coupon as you normally would – i.e. Discount type, Discount amount etc; and finally
- Don’t set any “Usage restrictions” or “Usage limits” for the coupon as they’ll likely be ignored by our code.
Secondly – Check for individual use coupons:
I have created a function that checks whether or not a coupon set to “Individual use” is currently applied to the customer cart. It simply returns
true if an individual use coupon is applied and a
false if not. We’ll use this function in upcoming code.
You can grab this code from my Gist. You’ll probably want to rename the function to something more meaningful to your theme or plugin.
Thirdly – Set our conditions and apply coupons:
Hook the function:
You’ll need to create a function that you can hook in to
wp_head. We only want the code to run on the cart and checkout pages though – I’d recommend using the
is_checkout() conditionals that ship with WooCommerce.
Check for individual use coupons:
I have then set up an additional conditional statement to check whether or not an “Individual use” coupon is currently applied to the cart – if it is, we don’t to apply the 5% or 10% discounts. A simple check using the
hildon_is_individual_coupon_applied() function is all that’s required here.
Custom conditions for coupon use:
After this, you’ll need to set up the conditions in which you wish the coupon(s) to be applied. In my code sample you can see that we have two coupons (5% discount and 10% discount) which are applied under certain circumstances.
My code example works as follows:
- If cart contains five or less units (cases of Natural Mineral Water for Hildon) –
remove both coupons;
- If cart contains between 6 and 11 cases –
apply the 5% discount and remove the 10% discount (only if applied); or
- If cart contains 12 or more cases –
remove the 5% discount (if applied) and add the 10% discount.
I have referenced the coupon names in variables so that if they change I only need update one location and not several.
The perfect solution?
I make no claim that this is the perfect solution for discount-based coupons in WooCommerce but it does have its advantages. It’s worked for our needs and has performed pretty well since the site has launched.
Have you got a better solution? Tweet us your ideas or comment on this post.