Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active October 27, 2020 03:11
Show Gist options
  • Save kimcoleman/089acd84d152d6ed0bd5d8ab99381992 to your computer and use it in GitHub Desktop.
Save kimcoleman/089acd84d152d6ed0bd5d8ab99381992 to your computer and use it in GitHub Desktop.
Allow members to renew at a discounted rate if they have expired within 3 months.
<?php
/*
* Allow members to renew at a discounted rate if they have expired within 3 months.
*
*/
/*
* Allow members to renew at a discounted rate if they have expired within 3 months.
*
*/
function grace_period_discount_pmpro_checkout_level( $level ) {
global $current_user, $discount_code, $wpdb;
// Discount does not apply new users.
if ( empty( $current_user->ID ) ) {
return $level;
}
// Discount does not apply if using a discount code.
if ( ! empty( $discount_code ) || ! empty( $_REQUEST['discount_code'] ) ) {
return $level;
}
// Calculate appropriate price if user is logged in and checking out for level ID 1.
if ( $level->id == 1 && is_user_logged_in()) {
// Set cutoff date for last 3 months.
$grace_period_cutoff = date( 'Y-m-d H:i:s', strtotime( '-3 months', current_time( 'timestamp' ) ) );
// Check if they had a discount in the past.
$grace_period_discount = $wpdb->get_var( "SELECT id
FROM $wpdb->pmpro_memberships_users
WHERE user_id = {$current_user->ID}
AND status NOT IN('active')
AND enddate > '{$grace_period_cutoff}'
ORDER BY id DESC
LIMIT 1");
// Discount their renewal if conditions are met.
if ( ! empty( $grace_period_discount ) ) {
$level->initial_payment = '50.00';
$level->billing_amount = '50.00';
return $level;
}
} else {
return $level;
}
}
add_filter( 'pmpro_checkout_level', 'grace_period_discount_pmpro_checkout_level', 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment