Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JarrydLong/4933a5649e753772cf5a114356bebd3d to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/4933a5649e753772cf5a114356bebd3d to your computer and use it in GitHub Desktop.
<?php //do not copy
/* This recipe adds a new level to a member's account for every year of renewal.
*
* A base level is used to handle the billing. Then Renewal Levels for Year 1 - 4 are created in a separate level group
* that allows for multiple memberships.
*
* Once a member reaches Year 4, they become a lifetime member and billing is cancelled while maintaining existing levels
*
* 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_progress_member_access($order) {
if (empty($order) || empty($order->user_id)) {
return;
}
$user_id = $order->user_id;
/**
* Assign each level ID that corresponds to the year of access. Adjust these to match your actual level IDs in PMPro.
* The Billing Level ID is the level that the user initially purchases.
* The Year 1-4 and Lifetime level IDs are the levels that will be added to the user as they make successful payments.
*/
$billing_level_id = 1;
$level_year_1 = 2;
$level_year_2 = 3;
$level_year_3 = 4;
$level_year_4 = 5;
$level_lifetime = 6;
// Only run for billing level
if ((int)$order->membership_id !== $billing_level_id) {
return;
}
// Prevent duplicate runs
$lock_key = 'pmpro_progress_lock_' . $order->id;
if (get_user_meta($user_id, $lock_key, true)) {
return;
}
update_user_meta($user_id, $lock_key, 1);
// Starting year
$starting_year = (int)get_user_meta($user_id, 'pmpro_starting_year', true);
if ($starting_year < 1) {
$starting_year = 1;
}
// Count payments
$payment_count = my_pmpro_get_successful_payment_count($user_id, $billing_level_id);
if ($payment_count < 1) {
return;
}
$current_year = $starting_year + $payment_count - 1;
if ($current_year >= 1) my_pmpro_add_level_if_missing($user_id, $level_year_1);
if ($current_year >= 2) my_pmpro_add_level_if_missing($user_id, $level_year_2);
if ($current_year >= 3) my_pmpro_add_level_if_missing($user_id, $level_year_3);
if ($current_year >= 4) my_pmpro_add_level_if_missing($user_id, $level_year_4);
if ($current_year >= 4) {
my_pmpro_add_level_if_missing($user_id, $level_lifetime);
my_pmpro_cancel_user_subscription($order);
}
}
add_action('pmpro_subscription_payment_completed', 'my_pmpro_progress_member_access', 10, 2);
function my_pmpro_get_successful_payment_count($user_id, $membership_id) {
global $wpdb;
$table = $wpdb->pmpro_membership_orders;
$count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(id)
FROM $table
WHERE user_id = %d
AND membership_id = %d
AND status = 'success'
AND subscription_transaction_id IS NOT NULL
", $user_id, $membership_id));
return (int)$count;
}
function my_pmpro_add_level_if_missing($user_id, $level_id) {
if (!pmpro_hasMembershipLevel($level_id, $user_id)) {
pmpro_changeMembershipLevel($level_id, $user_id);
}
}
function my_pmpro_cancel_user_subscription($order) {
if (empty($order) || empty($order->subscription_transaction_id)) {
return;
}
// This calls the gateway (Stripe/PayPal) properly
$order->cancel();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment