While MemberPress provides built-in Group configurations to handle product upgrades and downgrades via its standard frontend checkout pages, advanced projects often require programmatic control. Whether you are building a custom client dashboard where users can switch plans with a single click, migrating legacy subscriptions, or implementing custom proration and credit logic, you need to handle membership transitions directly via PHP.
Instead of hacking database tables or leaving orphaned active subscriptions, MemberPress provides robust data classes (MeprUser, MeprSubscription, and MeprTransaction) to manage lifecycle shifts cleanly.
In this tutorial, we will walk through how to programmatically upgrade or downgrade a user’s membership tier using PHP.
1. Checking Current Membership and Validating Upgrade Eligibility
Before shifting a user to a new membership tier, you must verify what active product they currently hold. This prevents duplicate subscriptions or invalid transitions.
Add the following helper function to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
function wg_mepr_get_user_active_product_ids($user_id) {
if (!class_exists('MeprUser')) {
return [];
}
$mepr_user = new MeprUser($user_id);
$subscriptions = $mepr_user->active_subscriptions();
$product_ids = [];
foreach ($subscriptions as $sub) {
$product_ids[] = (int) $sub->product_id;
}
return array_unique($product_ids);
}
2. Programmatically Upgrading a User (Switching Plans)
When upgrading a user from a lower-tier plan (e.g., Product ID 100) to a higher-tier plan (e.g., Product ID 200), you typically need to:
Cancel or expire their current active subscription.
Create a new transaction/subscription for the upgraded product.
- Grant access privileges immediately.
Here is how you can handle the programmatic transition:
function wg_mepr_programmatically_upgrade_user($user_id, $old_product_id, $new_product_id) {
if (!class_exists('MeprUser') || !class_exists('MeprTransaction')) {
return false;
}
$mepr_user = new MeprUser($user_id);
// 1. Find and cancel/stop the existing active subscription for the old product
$subscriptions = $mepr_user->active_subscriptions();
foreach ($subscriptions as $sub) {
if ((int) $sub->product_id === (int) $old_product_id) {
// Update subscription status to cancelled/stopped
$sub->status = MeprSubscription::$cancelled_str;
$sub->store();
}
}
// 2. Create a new complete transaction for the upgraded product
$txn = new MeprTransaction();
$txn->user_id = $user_id;
$txn->product_id = $new_product_id;
$txn->amount = 150.00; // Set appropriate upgrade price
$txn->total = 150.00;
$txn->status = MeprTransaction::$complete_str;
$txn->gateway = 'manual'; // Or your active gateway slug
$txn->created_at = current_time('mysql', true);
// Set expiration (e.g., 1 year from now, or leave '0000-00-00 00:00:00' for lifetime)
$txn->expires_at = date('Y-m-d H:i:s', strtotime('+1 year'));
if ($txn->store()) {
// Trigger MemberPress actions to ensure user rules/meta update properly
do_action('mepr-txn-status-complete', $txn);
return true;
}
return false;
}
3. Handling Custom Proration Logic (Calculating Credits)
If your business model requires calculating unused time from the old subscription and applying it as a discount (proration) toward the new plan, you can calculate the credit before generating the new transaction:
function wg_mepr_calculate_proration_credit($user_id, $old_product_id) {
$mepr_user = new MeprUser($user_id);
$credit = 0.00;
// Find latest active transaction for the old product
$transactions = $mepr_user->get_latest_transactions_by_product($old_product_id);
if (!empty($transactions)) {
$txn = $transactions[0];
if (!empty($txn->expires_at) && $txn->expires_at !== '0000-00-00 00:00:00') {
$expiration_timestamp = strtotime($txn->expires_at);
$current_timestamp = current_time('timestamp');
// If expiration is in the future, calculate remaining ratio
if ($expiration_timestamp > $current_timestamp) {
$total_duration = $expiration_timestamp - strtotime($txn->created_at);
$remaining_duration = $expiration_timestamp - $current_timestamp;
if ($total_duration > 0) {
$ratio = $remaining_duration / $total_duration;
$credit = round($txn->total * $ratio, 2);
}
}
}
}
return $credit;
}
Handling membership upgrades and downgrades programmatically gives you total control over custom account dashboards, automated billing adjustments, and complex migration workflows without relying on default frontend form flows.
Be sure to check out our previous tutorials on WordPress development:
How to Add Custom Dynamic Fees in WooCommerce Checkout
How to Restrict WordPress REST API via MemberPress
How to Programmatically Create WooCommerce Orders
Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.