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

huangapple go评论57阅读模式
英文:

Redirection to a page for new customers without an active product WHMCS

问题

早上好,

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

  1. add_hook('ClientAreaPage', 1, function($vars) {
  2. if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
  3. return;
  4. }
  5. if (isset($_SESSION['uid']) && $_SESSION['uid']) {
  6. $client_id = $_SESSION['uid'];
  7. $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
  8. if ($result['result'] == 'success') {
  9. $products = $result['products'];
  10. $has_active_product = false;
  11. foreach ($products as $product) {
  12. if ($product['gid'] == 1 && $product['status'] == 'Active') {
  13. $has_active_product = true;
  14. break;
  15. }
  16. }
  17. if (!$has_active_product) {
  18. header('Location: /store/adhesion');
  19. exit;
  20. }
  21. }
  22. }
  23. });

使用这个挂钩,即使客户拥有活跃产品,他们也会始终被重定向到页面/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 :

  1. add_hook('ClientAreaPage', 1, function($vars) {
  2. if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
  3. return;
  4. }
  5. if (isset($_SESSION['uid']) && $_SESSION['uid']) {
  6. $client_id = $_SESSION['uid'];
  7. $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
  8. if ($result['result'] == 'success') {
  9. $products = $result['products'];
  10. $has_active_product = false;
  11. foreach ($products as $product) {
  12. if ($product['gid'] == 1 && $product['status'] == 'Active') {
  13. $has_active_product = true;
  14. break;
  15. }
  16. }
  17. if (!$has_active_product) {
  18. header('Location: /store/adhesion');
  19. exit;
  20. }
  21. }
  22. }
  23. });

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

  1. $currentUser = new \WHMCS\Authentication\CurrentUser;
  2. add_hook('ClientAreaPage', 1, function($vars) {
  3. $currentUser = new \WHMCS\Authentication\CurrentUser;
  4. $user = $currentUser->user();
  5. $client = $currentUser->client();
  6. if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
  7. return;
  8. }
  9. if ($user) {
  10. $client_id = $client->id;
  11. $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
  12. if ($result['result'] == 'success') {
  13. $products = $result['products'];
  14. $has_active_product = false;
  15. if($products != NULL || !empty($products)){
  16. foreach ($products as $product) {
  17. if ($product['pid'] != 0 && $product['status'] == 'Active') {
  18. $has_active_product = true;
  19. break;
  20. }
  21. }
  22. }
  23. if ($has_active_product) {
  24. header('Location: /store/adhesion');
  25. exit;
  26. }
  27. }
  28. }});

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,感谢你抽出时间回答我的问题。
我刚刚对你的修改进行了一些测试并修改了一些细节:

  1. add_hook('ClientAreaPage', 1, function($vars) {
  2. $currentUser = new \WHMCS\Authentication\CurrentUser;
  3. $user = $currentUser->user();
  4. $client = $currentUser->client();
  5. if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
  6. return;
  7. }
  8. if ($user) {
  9. $client_id = $client->id;
  10. $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
  11. if ($result['result'] == 'success') {
  12. $products = $result['products'];
  13. $has_active_product = false;
  14. if($products != NULL || !empty($products)){
  15. foreach ($products as $product) {
  16. 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
  17. $has_active_product = true;
  18. break;
  19. }
  20. }
  21. }
  22. if (!$has_active_product) { // redirect unser only don't have product active on account
  23. header('Location: /store/adhesion');
  24. exit;
  25. }
  26. }
  27. }});

总结一下,这个挂钩必须将已连接但帐户中没有激活的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:

  1. add_hook('ClientAreaPage', 1, function($vars) {
  2. $currentUser = new \WHMCS\Authentication\CurrentUser;
  3. $user = $currentUser->user();
  4. $client = $currentUser->client();
  5. if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
  6. return;
  7. }
  8. if ($user) {
  9. $client_id = $client->id;
  10. $result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
  11. if ($result['result'] == 'success') {
  12. $products = $result['products'];
  13. $has_active_product = false;
  14. if($products != NULL || !empty($products)){
  15. foreach ($products as $product) {
  16. 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
  17. $has_active_product = true;
  18. break;
  19. }
  20. }
  21. }
  22. if (!$has_active_product) { // redirect unser only don't have product active on account
  23. header('Location: /store/adhesion');
  24. exit;
  25. }
  26. }
  27. }});

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

huangapple
  • 本文由 发表于 2023年2月16日 16:45:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定