From e7c1241a1cde81e4580de1fea9a71e7f0760fa2c Mon Sep 17 00:00:00 2001 From: Theuns Coetzee Date: Tue, 12 Nov 2024 10:08:54 +0200 Subject: [PATCH] Create subscription-delay-one-time-credit-card-required-trial.php --- ...ay-one-time-credit-card-required-trial.php | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 add-ons/pmpro-subscription-delays/subscription-delay-one-time-credit-card-required-trial.php diff --git a/add-ons/pmpro-subscription-delays/subscription-delay-one-time-credit-card-required-trial.php b/add-ons/pmpro-subscription-delays/subscription-delay-one-time-credit-card-required-trial.php new file mode 100644 index 0000000..0bfeb01 --- /dev/null +++ b/add-ons/pmpro-subscription-delays/subscription-delay-one-time-credit-card-required-trial.php @@ -0,0 +1,129 @@ + +

One-Time Trial

+ + + + + + + +
+ ID, 'pmpro_trial_level_used', true ); + if ( ! empty( $already ) && $already == '1' ) { + echo 'Trial period has been claimed.'; + } else { + echo 'Trial period not claimed.'; + } + ?> +
+ ID ) && ! empty( $checkout_level_id ) && $checkout_level_id == $trial_level_id ) { + // Check the current user's meta. + $already = get_user_meta( $current_user->ID, 'pmpro_trial_level_used', true ); + + // Remove the subscription delay from checkout. Charge the subscripton immediately. + if ( $already ) { + remove_filter( 'pmpro_profile_start_date', 'pmprosd_pmpro_profile_start_date', 10, 2 ); + remove_action( 'pmpro_after_checkout', 'pmprosd_pmpro_after_checkout' ); + remove_filter( 'pmpro_next_payment', 'pmprosd_pmpro_next_payment', 10, 3 ); + remove_filter( 'pmpro_level_cost_text', 'pmprosd_level_cost_text', 10, 2 ); + remove_action( 'pmpro_save_discount_code_level', 'pmprosd_pmpro_save_discount_code_level', 10, 2 ); + } + } +} +add_filter( 'init', 'one_time_trial_delay_pmpro_registration_checks' ); + +// Filter the price on the levels page to remove one-time trial. +function one_time_trial_delay_pmpro_level_cost_text( $cost, $level ) { + global $current_user, $pmpro_pages; + + // Not logged in? + if ( empty( $current_user->ID ) ) { + return $cost; + } + + // Check the current user's meta. + $already = get_user_meta( $current_user->ID, 'pmpro_trial_level_used', true ); + + // If the user already had the trial for this level, make initial payment = billing amount. + if ( $level->id == $already && is_page( $pmpro_pages['levels'] ) ) { + $cost = sprintf( __( '%1$s per %2$s.', 'paid-memberships-pro' ), pmpro_formatPrice( $level->billing_amount ), pmpro_translate_billing_period( $level->cycle_period ) ); + } + + return $cost; +} +add_filter( 'pmpro_level_cost_text', 'one_time_trial_delay_pmpro_level_cost_text', 15, 2 ); + +// Filter the price at checkout to charge the billing ammount immediately. +function one_time_trial_delay_pmpro_checkout_level( $level ) { + global $current_user, $discount_code, $wpdb; + + // Not logged in? + if ( empty( $current_user->ID ) ) { + return $level; + } + + // Not if using a discount code. + if ( ! empty( $discount_code ) || ! empty( $_REQUEST['discount_code'] ) ) { + return $level; + } + + // Check the current user's meta. + $already = get_user_meta( $current_user->ID, 'pmpro_trial_level_used', true ); + + // If the user already had the trial for this level, make initial payment = billing amount. + if ( $level->id == $already ) { + $level->initial_payment = $level->billing_amount; + } + + return $level; +} +add_filter( 'pmpro_checkout_level', 'one_time_trial_delay_pmpro_checkout_level', 5 );