Believe_ Posted March 6, 2021 Share Posted March 6, 2021 Hello, We have a few hooks on our system to run some additional tasks while accepting order, and which is working fine on our system. We are planning to create a few more products for which we don't have to run these hooks (rather a different hook functions). I don't see any way to execute hooks against a specific product(s) as it seems globally executing on all products. I've seen a thread on this community from back 2013 but no response on it - Please let me know if this is even possible? If yes, how can we apply it or a if condition to check out the product might get it done but not sure how we could integrate it with so sample code would be helpful. @DennisHermannsen / @brian! - you both have been helpful so far with such things, so any advise on this would be appreciable 🙂 Please shed some light on this as soon as possible, thanks 🙂 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 6, 2021 Share Posted March 6, 2021 In the hook, you would use an if, or switch case, and check for the pid of the product in the passed order array . Using an array to hold what pids you want would be easier to maintain also. 0 Quote Link to comment Share on other sites More sharing options...
Believe_ Posted March 8, 2021 Author Share Posted March 8, 2021 Hello @steven99 Can you provide a sample code for this with if statement for one product alone and an array of products, that would be much helpful. Thanks 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 8, 2021 Share Posted March 8, 2021 Which hook are you using? I ask as one will have different info and so getting the required pid would be different. 0 Quote Link to comment Share on other sites More sharing options...
Believe_ Posted March 9, 2021 Author Share Posted March 9, 2021 Hello @steven99, Thanks for your reply, and I've hooks for following actions: AddonActivation AcceptOrder AfterModuleCreate AfterProductUpgrade AddonTerminated AfterModuleTerminate Please shed some light on how to implement this with if/switch case with sample codes Thanks 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 10, 2021 Share Posted March 10, 2021 I'll focus on orderaccept as it is a pain and you have to go through hoops, more so then I thought, to get to the product id. <?php add_hook('AcceptOrder', 1, function ($vars) { $ProductsCaredAbout = array(14,11,15); //Ids of the products you want to do stuff with $results = localAPI('GetOrders', ['id'=>$vars['orderid'] ] ); if ($results['result']==='success') { foreach($results['orders']['order'] as $order) { foreach($order['lineitems']['lineitem'] as $item) { //AToW line item does not have product id, that helps a lot whmcs. $Service = WHMCS\Service\Service::find($item['relid']); if ($Service) { if (in_array( $Service->packageId, $ProductsCaredAbout) ) { // Do some magic. logModuleCall("hook", "AcceptOrder", "Order contains products: ".$Service->packageId, "", "", $vars); } } } } } }); The other hooks should provide the Service ID in the $vars array and so you just need to use the bits for that to find the service and then get the packageid and check in the productscaredabout array . Using the logModuleCall function to output the various arrays will help to determine what is provided when you look at system logs -> module logs . Just ensure that is enabled beforehand. 0 Quote Link to comment Share on other sites More sharing options...
Believe_ Posted May 3, 2021 Author Share Posted May 3, 2021 Hi @steven99, @brian! , @DennisHermannsen Sorry for the delay as I was on holidays and affected Covid-19. I implemented this check in one of our AcceptOrder hook, and it works perfectly however, noticed an error in Activity Log when we place a order for the product that is not in list as below. Any idea why this is occuring? Email Sending Failed - Invalid user id provided (Subject: Reset your password) This is coming just before the 'Order Accepted - Order ID: 105' line in Log and tested several times and same happening. Any idea from where this email is trying to generate and fail? I think the if statement [if (in_array( $Serviceid->packageId, $ProductsCaredAbout) )] is failing and trying to generate this message. Any way to get rid of this one? Otherwise everything okay with this one. Thanks, Fasal 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted May 4, 2021 Share Posted May 4, 2021 That sounds like another hook is sending the reset password email . Nothing in the above would cause that error and specifically about the password reset . 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted May 4, 2021 Share Posted May 4, 2021 fwiw, I agree with Steven on this. 0 Quote Link to comment Share on other sites More sharing options...
Believe_ Posted May 5, 2021 Author Share Posted May 5, 2021 Hi @steven99, @brian! But there is no other AcceptOrder hook present, so how do another hook process when we perform an order accept? If you wish, I could provide the full hook content to review it further. Please advise 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted May 5, 2021 Share Posted May 5, 2021 3 minutes ago, Believe_ said: But there is no other AcceptOrder hook present, so how do another hook process when we perform an order accept? If you wish, I could provide the full hook content to review it further. I think the point Steven is making, and that I agreed with, is that the above AcceptOrder hook itself wouldn't trigger a password reset email to be sent - so it won't necessarily be an acceptorder hook doing it, it could be another hook or even an addon module going wrong. it might even be happening by default and not caused by any hook - which WHMCS version do you have installed ? On 03/05/2021 at 17:30, Believe_ said: This is coming just before the 'Order Accepted - Order ID: 105' line in Log and tested several times and same happening. then it sounds like its occurring before the hook even logs the acceptance. what happens if you disable the above hook, make a relevant order for a product, and accept it manually - does that trigger the error? 0 Quote Link to comment Share on other sites More sharing options...
Magicklug Posted May 11, 2021 Share Posted May 11, 2021 This is interesting, I'm following. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.