Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save w3guy/95f61d39b5cab857535a622ceff50d8f to your computer and use it in GitHub Desktop.
Save w3guy/95f61d39b5cab857535a622ceff50d8f to your computer and use it in GitHub Desktop.
EDD - Allow discounts to be set to apply to all renewals
<?php
add_filter( 'edd_update_discount', 'edd_perpetual_discounts_add_meta' );
add_filter( 'edd_insert_discount', 'edd_perpetual_discounts_add_meta' );
add_action( 'edd_edit_discount_form_before_use_once', 'edd_perpetual_discounts_setting', 10, 2 );
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( $discount_id, $discount ) {
$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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment