I created a form using Gravity Forms in WordPress.
I want, for example, when the user selects the number 33 in the field with ID 3 in the form, after the form is submitted, the membership subscription in MemberPress Pro with ID 1 will be extended by the same number of days selected by the user, i.e., 33 days will be added to the person’s subscription.
Code By GPT
add_action('gform_post_submission_3', 'add_days_to_membership', 10, 2);
function add_days_to_membership($entry, $form) {
error_log("add_days_to_membership action executed"); // To check if the code runs
$selected_days = rgar($entry, '6'); // The field ID where the user selects the number of days
$user_id = get_current_user_id(); // Get the current user ID
// Check if the selected number of days is valid
if (!empty($selected_days) && is_numeric($selected_days)) {
error_log("Selected days: " . $selected_days); // Log the selected number of days
$membership_id = 1; // The ID of the membership
$current_expiration = get_user_meta($user_id, 'membership_expiration_' . $membership_id, true);
// Set the current expiration to now if it does not exist
if (empty($current_expiration)) {
$current_expiration = time();
}
$new_expiration = strtotime("+$selected_days days", $current_expiration);
update_user_meta($user_id, 'membership_expiration_' . $membership_id, $new_expiration);
error_log("New expiration date: " . date('Y-m-d H:i:s', $new_expiration)); // Log the new expiration date
} else {
error_log("Invalid days input or missing data.");
}
}
But NotWorking
Gravity form ID : 3
Form Field ID : 6
MemberShip Pro Pakage ID : 1
IMAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
You can try this –
add_action('gform_after_submission_1', 'extend_memberpress_subscription', 10, 2);
function extend_memberpress_subscription($entry, $form) {
$days_to_add = rgar($entry, '3'); // Field ID 3
$user_email = rgar($entry, '2');
$user = get_user_by('email', $user_email);
if ($user) {
$membership_id = 1;
if (class_exists('MeprUser')) {
$mepr_user = new MeprUser($user->ID);
$subscriptions = $mepr_user->active_subscriptions();
foreach ($subscriptions as $subscription) {
if ($subscription->product_id == $membership_id) {
$new_end_date = strtotime("+{$days_to_add} days", strtotime($subscription->ends_at));
$subscription->set_ends_at(date('Y-m-d H:i:s', $new_end_date));
$subscription->save();
}
}
}
}
}
Shradha Panchal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1