Skip to content
Advertisement

Mandatory redemption coupon for gifted items in WooCommerce

I’m using the below function which currently accepts a single (or array of) static coupon codes. How can I change this to work with any valid (i.e. not already used the maximum times etc.) coupon code that exists in the site?

JavaScript

Edit:

Further to the comments and information from this link, I’ve tried the below code but this isn’t working:

JavaScript

Advertisement

Answer

As wooCommerce already check for coupons validity when they are applied, If they are not valid it displays different warning messages depending on the reason for invalidity, removing invalid applied coupons…

So you don’t need to get all valid coupons in your code… You just need the following:

JavaScript

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Now if the required coupons needs to be specific, you will have make a custom SQL query to check if any applied coupon is valid for redemption.

Note: on your Edit, the coupon query is missing, so that’s why it doesn’t work.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement