将重力形式连接到会员专业插件

问题描述 投票:0回答:1

我在 WordPress 中使用重力表单创建了一个表单。 例如,我希望,当用户在表单中 ID 为 3 的字段中选择数字 33 时,提交表单后,ID 为 1 的 MemberPress Pro 中的会员订阅将延长与选择的天数相同的天数。用户,即该人的订阅将添加 33 天。

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.");
    }
}

但不工作

重力形式ID:3 表单字段 ID:6 会员专业套餐 ID:1

wordpress gravity-forms-plugin membership
1个回答
0
投票

你可以试试这个 - add_action('gform_after_submission_1', 'extend_memberpress_subscription', 10, 2); 函数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();
            }
        }
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.