-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create subscription delay one time credit card required trial #209
Open
ipokkel
wants to merge
1
commit into
strangerstudios:dev
Choose a base branch
from
ipokkel:subscription-delay-one-time-credit-card-required-trial
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
add-ons/pmpro-subscription-delays/subscription-delay-one-time-credit-card-required-trial.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* This code stores data when a user checks out for a level. | ||
* If that user tries to checkout for the same level, the Subscription Delay is removed. | ||
* The user is instead charged for their first subscription payment at checkout. | ||
* | ||
* title: One time credit card required trial | ||
* layout: snippet | ||
* collection: add-ons, pmpro-subscription-delays | ||
* category: subscriptions, trials | ||
* link: https://www.paidmembershipspro.com/subscription-delay-one-time-credit-card-required-trial/ | ||
* | ||
* You can add this recipe to your site by creating a custom plugin | ||
* or using the Code Snippets plugin available for free in the WordPress repository. | ||
* Read this companion article for step-by-step directions on either method. | ||
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | ||
*/ | ||
|
||
// Record when users gain the trial level. | ||
function one_time_trial_save_trial_level_used( $level_id, $user_id ) { | ||
// Set this to the ID of your trial level. | ||
$trial_level_id = 1; // Membership Level ID | ||
|
||
if ( $level_id == $trial_level_id ) { | ||
// Add user meta to record that the user has received their one-time trial. | ||
update_user_meta( $user_id, 'pmpro_trial_level_used', $trial_level_id ); | ||
} | ||
} | ||
add_action( 'pmpro_after_change_membership_level', 'one_time_trial_save_trial_level_used', 10, 2 ); | ||
|
||
// Show the user's trial meta setting for admins on the Edit Profile page. | ||
function one_time_trial_show_trial_level_used( $user ) { | ||
if ( current_user_can( 'edit_users' ) ) { ?> | ||
<h3>One-Time Trial</h3> | ||
<table class="form-table"> | ||
<tbody> | ||
<tr> | ||
<th scope="row"></th> | ||
<td> | ||
<?php | ||
$already = get_user_meta( $user->ID, 'pmpro_trial_level_used', true ); | ||
if ( ! empty( $already ) && $already == '1' ) { | ||
echo 'Trial period has been claimed.'; | ||
} else { | ||
echo 'Trial period not claimed.'; | ||
} | ||
?> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<?php | ||
} | ||
} | ||
add_action( 'show_user_profile', 'one_time_trial_show_trial_level_used' ); | ||
add_action( 'edit_user_profile', 'one_time_trial_show_trial_level_used' ); | ||
|
||
// Check if the user has received their one-time trial at checkout. | ||
function one_time_trial_delay_pmpro_registration_checks() { | ||
global $current_user; | ||
|
||
//set this to the id of your trial level | ||
$trial_level_id = 1; // Membership Level ID | ||
if ( ! empty( $_REQUEST['level'] ) ) { | ||
$checkout_level_id = intval( $_REQUEST['level'] ); | ||
} | ||
|
||
if ( ! empty( $current_user->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( __( '<strong>%1$s per %2$s</strong>.', '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 ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check needs to change, and any other instance of
$_REQUEST['level']
we can use thepmpro_getLevelAtCheckout()
or from the $level (object) directly.