Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EDD - Allow discounts to be set to apply to all renewals
<?php
// Add new Discount setting the to discount add and edit forms
add_action( 'edd_add_discount_form_before_use_once', array( $this, 'edd_perpetual_discounts_setting_add_form' ) );
add_action( 'edd_edit_discount_form_before_use_once', array( $this, 'edd_perpetual_discounts_setting_edit_form' ) );
// Handle saving the settings on form submission
add_filter( 'edd_update_discount', 'edd_perpetual_discounts_add_meta' );
add_filter( 'edd_insert_discount', 'edd_perpetual_discounts_add_meta' );
// Process the discount at renewal
add_filter( 'edd_get_option_recurring_one_time_discounts', 'edd_perpetual_discounts_discounts' );
function edd_perpetual_discounts_add_meta( $meta ) {
$meta['use_renewals'] = 0;
if ( isset( $_POST['use_renewals'] ) ) {
$meta['use_renewals'] = intval( $_POST['use_renewals'] );
}
return $meta;
}
function edd_perpetual_discounts_setting_add_form() {
edd_perpetual_discounts_setting_edit_form( false );
}
function edd_perpetual_discounts_setting_edit_form( $discount_id ) {
$use_renewals = false;
if ( $discount_id ) {
$use_renewals = get_post_meta( $discount_id, '_edd_discount_use_renewals', true );
} ?>
<tr>
<th scope="row" valign="top">
<label
for="edd-use-renewals"><?php _e( 'Discount applies for renewals', 'easy-digital-downloads' ); ?></label>
</th>
<td>
<input type="checkbox" id="edd-use-renewals" name="use_renewals"
value="1"<?php checked( true, $use_renewals ); ?>/>
<span
class="description"><?php _e( 'By default discounts don\'t apply to renewals, but enabling it for this coupon will.', 'easy-digital-downloads' ); ?></span>
</td>
</tr>
<?php
}
function edd_perpetual_discounts_discounts( $value ) {
if ( ! $value ) {
return $value;
}
$discounts = edd_get_cart_discounts();
foreach ( $discounts as $code ) {
$discount_id = edd_get_discount_id_by_code( $code );
if ( 1 == get_post_meta( $discount_id, '_edd_discount_use_renewals', true ) ) {
// The discount should be applied for all renewals
return false;
}
}
return $value;
}
@polevaultweb
Copy link
Author

When using Easy Digital Downloads and the Recurring Payments addon, you can set discounts to only apply to the initial payment, and not to renewal payments:

image

However, this applies to all discounts.

But what happens if you want to create a discount for a charity to give them 20% off the download price for ever? Currently it's not possible with EDD and Recurring Payments. With this code, it's now possible.

The code gives you a new setting on the discount screen:

image

Discounts with that setting enabled will mean subscriptions purchased with that discount will have the discount applied to subsequent renewals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment