# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Easy Digital Downloads](https://wpide.com/library/category/edd/)
- Requires: Easy Digital Downloads
- Tags: edd, discounts, checkout, sale
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/edd-auto-apply-discount/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Apply an EDD Discount Code Automatically" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
