Apply an EDD Discount Code Automatically
Applies a discount code to every Easy Digital Downloads checkout automatically, ideal for a site-wide sale where buyers should not need to type a code.
Opens WPIDE on
Code
$wpide_edd_auto_code = 'SUMMER20'; // Existing discount code.
add_action( 'template_redirect', function () use ( $wpide_edd_auto_code ) {
if ( ! function_exists( 'edd_is_checkout' ) || ! edd_is_checkout() || ! edd_get_cart_contents() || edd_cart_has_discounts() ) {
return;
}
if ( edd_is_discount_valid( $wpide_edd_auto_code, '', false ) ) {
edd_set_cart_discount( $wpide_edd_auto_code );
}
} );
Notes
Create the code in Downloads > Discounts first (its dates and limits still apply) and remove the snippet when the sale ends.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.