Jump to content

Which hook would work here the best?


yggdrasil

Recommended Posts

I want to add a hook in the pre ordering process, when someone adds a product to the cart but has not registered an account yet and the order was not entered into the system yet.

 

There are several hooks, but I want to minimize running the hook unless its necessary since it has a bit of overhead and I want it run before the account is created, and I don't want to make the experience slower by running it unless I really have. So when a user is viewing the cart is out of the question.

 

I'm not sure which one would be a better fit here between this 3:

 

ShoppingCartValidateProductUpdate

As the product configuration is being completed. If no product configuration is required, this hook will not run.

 

ShoppingCartValidateCheckout

As the final checkout button is pressed, this hook is run and an error can be returned to stop the process.

 

PreShoppingCartCheckout

As the Checkout button is pressed, before the order is added to the database.

 

The first one seems a no go since if a product has no configuration it would not run and I want to run them for all ordering attempts. The fraud or registration hooks will not work because that means the order was already send and the user created an account which is what I want to stop here.

 

Between the following two, I'm not sure in which order they are executed. The customer should:

 

1. Not yet have an account created, the cart usually asks them to register one before finalizing the checkout

2. The order should not be yet added to the WHMCS database

 

What ever runs first before the user actually registers his account and completes the order.

 

Any ideas?

Link to comment
Share on other sites

I think choosing between ActionHooks point(s) actually depends on what you need to do, for example (PreCalculateCartTotals, PreShoppingCartCheckout and ShoppingCartValidateCheckout) all has the same values passed to it so you can do some validation or automate things using ActionHook function, but only "ShoppingCartValidateCheckout" give you the option to return error message to client.

 

any of these three ActionHook points might work for you, from there you can check if client is logged-in or not

 

if (isset($_SESSION['uid']) && intval($_SESSION['uid'])!==0){
   // User Logged In
   // Add your code here
}

Link to comment
Share on other sites

I don't actually need to pass any data to or from the cart. Its more simple than that.

 

I just need to execute an action or code when someone tries to order something (anything). I could put this on the viewcart page, but then it would be executed on every page hit, even when Google or any other bot loads the page. That is not a desirable effect. I would prefer this to be executed only when someone adds something to the cart or even better, when someone tries to buy something, before they try to checkout. At least 1 step before they are on the final page where they are required to register their new account to finalize the checkout procedure.

 

What hook could apply here? When someone adds a product to the shopping cart?

 

The hooks don't really explain to much in the documentation and some look very similar.

Link to comment
Share on other sites

This one is then http://docs.whmcs.com/Hooks:ShoppingCartValidateCheckout

 

<?php

function hook_ShoppingCartValidateCheckout($vars){

   // Existing Client Can Bypass This Check
   if (filter_var($vars['loginemail'], FILTER_VALIDATE_EMAIL)!==false || intval($_SESSION['uid'])!==0){
       return false;
   }

   // Cart Items
   $cart = $_SESSION['cart'];

   // No Items In Cart
   if (count($cart['products'])==0 && count($cart['domains'])==0){
       return false;
   }

   // Here you can validate Order or Client information and return error message if you need to

}
add_hook("ShoppingCartValidateCheckout", 1, "hook_ShoppingCartValidateCheckout");

 

this function will work when no clients logged in, shopping cart has at least one item (product or domain), and the person who submit the order is registering for the first time in your system.

Link to comment
Share on other sites

That is great. Thank you, but you think I should use ShoppingCartValidateCheckout as the hook?

 

I actually want to stop the user from registering, if the hook is executed after he hits the register/checkout final button, then he will probably get registered at the same time the hook is run. I need to execute my code before he tries to create an account on WHMCS. At least on my experience with WHMCS hooks, its better to use the hook before the action you want to stop, otherwise both your code and the action on WHMCS are executed.

 

It seems to me that this hook is actually the last part on the checkout process which maybe would be too late already and the account would be created already. I think it needs to be before this step.

Link to comment
Share on other sites

I'm aware about your issue already, using this function you can return error message to prevent order from being submitted and client will not be registered neither

 

From your last reply this hook should be then executed before actually then adding any customer details or order details to the database.

 

Thank you. I will test that out.

Link to comment
Share on other sites

From your last reply this hook should be then executed before actually then adding any customer details or order details to the database.

yes, you need to return error message (example below) to prevent order from being submitted

 

<?php

function hook_ShoppingCartValidateCheckout($vars){

   // Existing Client Can Bypass This Check
   if (filter_var($vars['loginemail'], FILTER_VALIDATE_EMAIL)!==false || intval($_SESSION['uid'])!==0){
       return false;
   }

   // Cart Items
   $cart = $_SESSION['cart'];

   // No Items In Cart
   if (count($cart['products'])==0 && count($cart['domains'])==0){
       return false;
   }

   // Here you can validate Order or Client information and return error message if you need to
   if ($a!=$b){
       return array("Error message #1", "Error message #2");
   }

}
add_hook("ShoppingCartValidateCheckout", 1, "hook_ShoppingCartValidateCheckout");

Link to comment
Share on other sites

I have tested this extensively today by basically just adding a basic code that sends me an email when the hook is executed. (to check it its running). It does not work. Neither my code or yours. I suspect the hooks are not working.

 

If I change the code or yours to use another hook like ShoppingCartValidateProductUpdate or PreCalculateTotals they work. I receive a test email which means the code was executed.

 

For some reason neither ShoppingCartValidateCheckout works and neither does PreShoppingCartCheckout works.

 

The first one is supposed to run when you click the final checkout button which is what you used. The second one is supposed to run when you hit the checkout button (not the final checkout button).

 

I actually need the first one before landing on the page where a user selects his payment option and enters his registration details, but regardless of the hook, none works. I tried other hooks, they work but not those two. I tried ordering multiple times, right after the end checkout page and the hooks are not executed.

 

Maybe this specific hooks are not working in WHMCS v6. No errors either in the debug logs. They are just not executed anywhere in the checkout process, using the standard official cart.

Link to comment
Share on other sites

if (!defined("WHMCS")) {

die("This file cannot be accessed directly");

}

 

add_hook('ShoppingCartValidateCheckout', 1, function ($vars)

 

{

 

// Send Test Email

$to = "info@example.com";

$subject = "Hook is Executed";

$txt = "Ok. Its running!";

$headers = "From: info@example.com";

 

mail($to,$subject,$txt,$headers);

 

});

 

- - - Updated - - -

 

That, as you can see I made it a bit cleaner and more simple. I mean the part that executes the hook.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated