Table of Contents
In WooCommerce, the manual creation and deletion of coupons are straightforward, but the process is easy only when dealing with a limited number of coupons. Also, for those engaged in selling digital products such as ebooks and software through the Easy Digital Downloads (EDD) plugin, the scenario changes. Such plugins leverage extensions that enable site administrators to generate discount codes in bulk and offer promotions often. As a result, a substantial number of coupons may accumulate over time. In such instances, a useful code snippet may be required to efficiently delete unused coupon codes or coupons created before a specific date.
In this post, we will understand how to delete a coupon code programmatically for easy digital downloads based on the conditions listed below:-
- Delete all coupon codes created before a certain date in Easy Digital Downloads.
- Delete a single coupon programmatically in Easy Digital Downloads.
- Delete multiple coupons programmatically in Easy Digital Downloads.
Where to Add Custom Code in Easy Digital Downloads
It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
3 Ways to Delete Coupon Code Programmatically in Easy Digital Downloads
1. Delete all coupon codes created before a certain date in Easy Digital Downloads
Consider an online store that provides seasonal discounts to its customers through coupon codes, which are generated automatically. However, after some time, there can be too many coupon codes, leading to confusion. To address this issue, a solution is to delete the outdated codes. This can be done by implementing a code to remove all coupons created before Jan 2023. This way, only the latest and relevant discounts will be available to the customers, ensuring a clean and up-to-date system.
$target_date = strtotime('2023-01-01'); // Get all discounts created before the target date $args = array( 'posts_per_page' => -1, 'post_type' => 'edd_discount', 'post_status' => 'publish', 'date_query' => array( 'before' => date('Y-m-d', $target_date), ), ); $discounts = get_posts($args); if (!empty($discounts)) { foreach ($discounts as $discount) { // Delete the discount edd_delete_discount($discount->ID); // Optionally, log or display a message error_log('Discount ID ' . $discount->ID . ' deleted because it was created before ' . date('Y-m-d', $target_date)); } }
Output
Here, the coupon codes(Summer Sale and Black Friday) have been added to the discount listing page with their dates below.
After applying the code snippets, the coupon code (Summer Sale) gets deleted from the discount listing page as shown below.
This code script removes outdated discounts created before a specified date. It begins by defining the target date (January 1st, 2023) and then constructs a query to retrieve all published discounts created before that date. If any discounts are found, the script loops through each one and removes it using edd_delete_discount.
2. Delete a single coupon programmatically in Easy Digital Downloads
The following code snippets demonstrate how to delete a single coupon. To delete a specific coupon, replace the word ‘cybermonday10’ with the coupon code in the code given below.
if ( class_exists( 'Easy_Digital_Downloads' ) ) { // Specify the coupon code you want to delete $coupon_code = 'cybermonday10'; // Check if the coupon exists before trying to delete it $coupon_id = edd_get_discount_id_by_code( $coupon_code ); if ( $coupon_id ) { // Delete the coupon edd_delete_discount( $coupon_id ); // Optionally, log or display a message error_log( 'Coupon deleted successfully.' ); } }
Output
The below output shows that the coupon code ‘cybermonday10’ has been deleted from the backend side.
The code snippets mentioned above are responsible for setting the value of $coupon_code to a specific code to be deleted, which in this case is ‘cybermonday10’. The edd_get_discount_id_by_code function is then used to retrieve the internal ID of the given code. The code also verifies that $coupon_id is not empty, which ensures that the code exists. If the coupon code exists, then edd_delete_discount is used to remove it.
3. Delete multiple coupons programmatically in Easy Digital Downloads
If you need to delete multiple coupons, you can use an array to store the coupon codes and then loop through the array to delete each coupon. Here is the code to delete multiple coupons:
if ( class_exists( 'Easy_Digital_Downloads' ) ) { // Define an array of coupon codes to delete $coupon_codes = array( 'cybermonday10', 'summerdiscount20', 'freeshppingjuly', ); foreach ( $coupon_codes as $coupon_code ) { // Get the coupon ID $coupon_id = edd_get_discount_id_by_code( $coupon_code ); if ( $coupon_id ) { // Delete the coupon edd_delete_discount( $coupon_id ); // Optionally, log the deleted coupon code error_log( "Coupon '{$coupon_code}' deleted successfully." ); } else { error_log( "Coupon '{$coupon_code}' not found." ); } } }
Code Explanation
- It defines an array named $coupon_codes containing the coupon codes you want to delete (e.g., cybermonday10, summerdiscount20).
- It iterates through each code in the $coupon_codes array using a foreach loop.
- Inside the loop, it uses the edd_get_discount_id_by_code function to retrieve the internal ID for each coupon code.
- It checks if the retrieved ID is not empty (meaning the coupon exists).
- If the coupon exists, it calls the edd_delete_discount function to delete it from your store with the coupon ID.
- After deleting the coupon, it optionally logs a message using error_log, indicating whether the deletion was successful or not.
Read Related: How to delete a coupon Programmatically in WooCommerce? |
Conclusion
This post helps you to delete coupon codes programmatically that are generated in EDD plugins. Alternatively, you can also delete a coupon programmatically in WooCommerce based on different conditions.
Let us know the usefulness of the code snippets in the comment section below.