英文:
Redirection to a page for new customers without an active product WHMCS
问题
早上好,
我创建了一个挂钩来尝试重定向新客户和那些在产品组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
。
我检查了文档,这是我的第一个挂钩,我无法确定问题出在哪里。
你能帮我吗?
非常感谢。
英文:
Good morning,
I made a hook to try to redirect new customers and those who no longer have an active product among product group 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;
}
}
}
});
With this, the customer is always redirected to the page /store/adhesion
even if he has an active product,
I check documentation, this is my first hook and I can't figure out where the problem is.
Can you help me ?
Thank you so much
答案1
得分: 1
我从当前经过身份验证的用户获取了客户端ID,检查了用户是否不为null,然后使用localAPI获取客户端的产品。
我看到您检查了$product['gid'] == 3
;我不知道那是什么,但我已将其更改为检查产品ID是否存在。
英文:
I don't know if this answer is still relevant, but here is a modification to your code.
Instead of getting the client ID from the session, which will return NULL, you should get it from the currently authenticated user instead.
WHMCS\Authentication\CurrentUser
$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;
}
}
}});
I got the client ID from the current authenticated user, checked if the user was not null, and then used the localAPI to get the clients' products.
I see that you checked against $product['gid'] == 3
; I don't know what that is but I changed it to check if product Id exists.
答案2
得分: 1
你好,@Steve Brain,感谢你抽出时间回答我的问题。
我刚刚对你的修改进行了一些测试并修改了一些细节:
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['gid'] == 3 && $product['status'] == 'Active') { // User can have other pid from same gid, i need to check if product is on right group id
$has_active_product = true;
break;
}
}
}
if (!$has_active_product) { // redirect unser only don't have product active on account
header('Location: /store/adhesion');
exit;
}
}
}});
总结一下,这个挂钩必须将已连接但帐户中没有激活的3号产品的用户重定向(这解释了你提到的GID 3)。
然而,使用旧代码和你的代码,不管客户是否有来自3号组或其他组的激活产品,他们都会被系统性地重定向到/store/adhesion页面。
我仍然不明白为什么,因为一切都在那里,但没有什么有帮助。
如果你有什么想法,我很感兴趣,再次感谢。
英文:
Hello @Steve Brain and thank you for taking the time to answer me.
I have just carried out some tests with your modifications and modified some details:
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['gid'] == 3 && $product['status'] == 'Active') { // User can have other pid from same gid, i need to check if product is on right group id
$has_active_product = true;
break;
}
}
}
if (!$has_active_product) { // redirect unser only don't have product active on account
header('Location: /store/adhesion');
exit;
}
}
}});
I summarize, the hook must redirect a user connected without active group 3 product on his account (which explains the GID 3 that you mentioned).
Except that with the old code as well as with yours, whether or not the customer has an active product from group 3 or another group, he is systematically redirected to the store/membership page.
And I still don't understand why because everything is there but nothing helps.
If you have an idea, I'm interested,
thanks again
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论