Forked from polevaultweb/edd_perpetual_discounts.php
Created
February 18, 2022 09:55
-
-
Save danieliser/c4d6f8658b6b87de8bd65375e24b4d75 to your computer and use it in GitHub Desktop.
EDD - Allow discounts to be set to apply to all renewals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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