Delete Expired Coupons Automatically

Update 03 February 2021: Script tested and confirmed working on WooCommerce 4.9.2.

Coupons are amazing, who does not like a discount? A very common way to give coupons to customers is to create one automatically after their first order, or something like that.

The issue with that though is that usually these coupons expire after a specific period, and by default, WooCommerce will not delete them. They have no reason to stay in your database though, so how do we delete expired coupons automatically?

With a snippet of course.

Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

This snippet will automatically move to the Trash all the expired coupons in your store so you can go and delete them permanently with one click, regardless if they have been used or not.

If you want to permanently delete the coupons directly instead of trashing them, you can change this code:

wp_trash_post( $coupon->ID );

to this:

wp_delete_post( $coupon->ID, true );

but beware that once you permanently delete a coupon it cannot be recovered.

Note that it works on WordPress cron jobs. If you disabled them or for any reason, they are not working, this snippet will not work as well.


More Posts That You Might Like…


7 responses to “Delete Expired Coupons Automatically”

  1. Will this delete expired coupons that have been used?

  2. Hi there. I have the same issue here, the snippet should not delete expired by date coupons but already used coupons. Any ideas in that?
    Thank you so much in advance!

  3. This snipped haven’t worked for me. I have added it as Snipppet (executed once) but without effect. Still having over 40’000 coupons in the system (the most of them are expired).

  4. Thanks 4 sharing

  5. will this work?

    1. Yes, I tested it now on WooCommerce 4.9.2 and I updated the article with the most updated version of this script.

  6. Hi Nicola,

    your script is not working for us. WordPress 5.7.2 + Woocommerce 5.4.1. I really hope it can be fixed, we desperately need this functionality.

    Kind regards,
    Edwin

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: