Jump to content

Hook for a specific one or few products


Recommended Posts

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 🙂 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

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?

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