为没有活动产品的新客户重定向到页面 WHMCS

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

早安,

我做了一个钩子来尝试重定向新客户和那些在产品组 1 中不再拥有活跃产品的客户:

add_hook('ClientAreaPage', 1, function($vars) {
    if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
        return;
    }
    if (isset($_SESSION['uid']) && $_SESSION['uid']) {
    $client_id = $_SESSION['uid'];
    $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
    if ($result['result'] == 'success') {
        $products = $result['products'];
        $has_active_product = false;
        foreach ($products as $product) {
            if ($product['gid'] == 1 && $product['status'] == 'Active') {
                $has_active_product = true;
                break;
            }
        }
        if (!$has_active_product) {
            header('Location: /store/adhesion');
            exit;
        }
    }
}
});

有了这个,客户总是被重定向到页面

/store/adhesion
,即使他有一个活跃的产品,

我检查了文档,这是我的第一个钩子,我不知道问题出在哪里。

你能帮帮我吗? 非常感谢

hook whmcs
1个回答
0
投票

我不知道这个答案是否仍然相关,但这是对您的代码的修改。

不是从将返回 NULL 的会话中获取客户端 ID,而是应该从当前经过身份验证的用户中获取它。 WHMCS\身份验证\当前用户

$currentUser = new \WHMCS\Authentication\CurrentUser;
add_hook('ClientAreaPage', 1, function($vars) {

$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
$client = $currentUser->client();

if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
    return;
}

if ($user) {
    $client_id = $client->id;
    
    $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
    if ($result['result'] == 'success') {
        $products = $result['products'];
        
        $has_active_product = false;
        if($products != NULL || !empty($products)){
            foreach ($products as $product) {
                if ($product['pid'] != 0 && $product['status'] == 'Active') {
                    $has_active_product = true;
                    break;
                }
            }
        }
        if ($has_active_product) {
            header('Location: /store/adhesion');
            exit;
        }
    }
}});

我从当前经过身份验证的用户那里获得了客户端 ID,检查用户是否不为空,然后使用 localAPI 获取客户的产品。

我看到你检查了 $product['gid'] == 3;我不知道那是什么,但我更改了它以检查产品 ID 是否存在。

我希望这能回答你的问题。

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