Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
When a user with an enddate renews for a recurring subscription for the same level, do not charge the initial payment and set the first recurring payment date for their old enddate.
<?php
/**
* When a user with an enddate renews for a recurring subscription for the same level,
* do not charge the initial payment and set the first recurring payment date for their
* old enddate.
*
* 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_checkout_level_profile_start_date_from_expiration( $level ) {
if ( ! pmpro_isLevelRecurring( $level ) ) {
return $level;
}
$current_level = pmpro_getMembershipLevelForUser();
if ( $current_level->id == $level->id ) {
if ( ! empty( $current_level->enddate ) && $current_level->enddate > time() ) {
global $my_pmpro_profile_start_date_from_expiration_flag;
$my_pmpro_profile_start_date_from_expiration_flag = date( 'Y-m-d', $current_level->enddate );
$level->initial_payment = 0;
}
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level_profile_start_date_from_expiration', 15, 1 );
function my_pmpro_profile_start_date_from_expiration( $date, $order ) {
global $my_pmpro_profile_start_date_from_expiration_flag;
if ( ! empty( $my_pmpro_profile_start_date_from_expiration_flag ) ) {
$date = $my_pmpro_profile_start_date_from_expiration_flag;
}
return $date;
}
add_filter( 'pmpro_profile_start_date', 'my_pmpro_profile_start_date_from_expiration', 15, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment