Last active
November 7, 2024 07:59
-
-
Save ipokkel/cda2b97674a0d155f6a3105a8eba5f58 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Remove the initial payment for users who already had a membership level, including free levels, | |
* and the checkout level is a recurring billing level. | |
* Optionally, only remove the initial payment when the user has paid an initial payment for a previous level. | |
* | |
* 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/ | |
*/ | |
function my_pmpro_remove_initial_payment_for_existing_members( $level ) { | |
/* set to true to only remove initial payment when initial payment has been paid for a previous level. Set to false to remove initial payment if user had any previous level, including false. */ | |
$require_previous_payment = false; | |
if ( ! is_user_logged_in() || 0 === $level->initial_payment || $level->billing_amount <= 0 ) { | |
return $level; // bail if user is not logged in or if level doesn't have an initial payment. | |
} | |
$user_id = get_current_user_id(); // get user ID. | |
$membership_history = pmpro_getMembershipLevelsForUser( $user_id, true ); // get user's membership level history. | |
if ( empty( $membership_history ) ) { | |
return $level; // bail if user has no membership level history. | |
} | |
if ( $require_previous_payment ) { | |
$initial_payments = wp_list_pluck( $membership_history, 'initial_payment' ); // get all initial payments. | |
if ( ! empty( $initial_payments ) ) { | |
foreach ( $initial_payments as $initial_payment ) { | |
if ( $initial_payment > 0 ) { | |
$level->initial_payment = 0; // set initial payment to 0 if user has paid an initial payment before. | |
break; | |
} | |
} | |
} | |
} else { | |
$level->initial_payment = 0; // set initial payment to 0 if user has had any previous level. | |
} | |
return $level; | |
} | |
add_filter( 'pmpro_checkout_level', 'my_pmpro_remove_initial_payment_for_existing_members', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment