Use these helper methods when integrating custom code with Membership Pro. If your code runs outside Membership Pro, use the line below to load the Membership Pro API before calling its methods.
require_once JPATH_ADMINISTRATOR . '/components/com_osmembership/loader.php';
// The code below returns ids of active subscription plans of the current logged in user
$planIds = OSMembershipHelperSubscription::getActivePlanIdsForUser();
// The code below returns ids of active subscription plans of the user with ID = 100
$planIds = OSMembershipHelperSubscription::getActivePlanIdsForUser(100);
$data = OSMembershipHelperSubscription::getSubscriptionData(100);
echo $data['first_name'];
echo $data['last_name'];
echo $data['name_of_certain_field'];
$id = 100; // ID of a subscription record
$fieldId = 30; // ID of the none core field which you want to get value from
$fieldValue = OSMembershipHelperSubscription::getFieldValue($id, $fieldId);
$userId = 100;
$fieldName = 'first_name'; // Name of the core field to get value from
$planId = 0; // Optional, can set to ID of the plan you want
$fieldValue = OSMembershipHelperSubscription::getFirstCoreFieldValue($userId, $fieldName, $planId);
$userId = 100;
$fieldName = 'first_name'; // Name of the core field to get value from
$planId = 0; // Optional, can set to ID of the plan you want
$fieldValue = OSMembershipHelperSubscription::getLastCoreFieldValue($userId, $fieldName, $planId);
$userId = 100;
$fieldName = 'first_name'; // Name of the core field to get value from
$planId = 0; // Optional, can set to ID of the plan you want
$fieldValue = OSMembershipHelperSubscription::getFirstFieldValue($userId, $fieldName, $planId);
$userId = 100;
$fieldName = 'first_name'; // Name of the core field to get value from
$planId = 0; // Optional, can set to ID of the plan you want
$fieldValue = OSMembershipHelperSubscription::getLastCoreFieldValue($userId, $fieldName, $planId);
$data = [
'plan_id' => $planId,
'user_id' => $userId,
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $user['email'],
// You can add any other custom fields and other data here
];
$model = new OSMembershipModelApi();
try
{
$model->store($data);
}
catch (Exception $e)
{
// Ignore error for now
}
The system will renew subscription if there is an existing subscription for the user for the plan.