Jump to content
Believe_

Execute a bash script while accepting an order

Recommended Posts

Hello Guys,

I've set up a bash script to automate few tasks that we did manually, but to complete this we would need to execute this script at the time of accepting an order. From reviewing some online threads, I guess it could be possible using the WHMCS AcceptOrder hook [ https://developers.whmcs.com/hooks-reference/shopping-cart/#acceptorder ] , but I'm not sure how this can be applied to get it work.

My requirement is:

- We have a custom product, once they have paid the fee and when the order accepted a bash script need to execute. 

- The client will give an IP address on a custom field at the time of order, and that need to pass as an argument to the script including the client name and email address.

- for eg: /root/automate_script.sh $ipaddress $name $email

If so, how would I go further doing it? Can someone shed some light on this with an example code, that would be much helpful. 

Thanks, 🙂

Share this post


Link to post
Share on other sites

Create a PHP file in your includes/hook directory with this action hook:

<?php

use WHMCS\Database\Capsule;

add_hook('AcceptOrder', 1, function($vars)
{
	// Replace with the ID of your Product Custom Field
	$CustomFieldID = '4';

	// Retreiving email and firstname and relid needed to get IPs later on
	$Data 		= Capsule::select(Capsule::raw('SELECT t2.relid, t3.email, t3.firstname FROM tblorders AS t1 LEFT JOIN tblinvoiceitems AS t2 ON t1.invoiceid = t2.invoiceid LEFT JOIN tblclients AS t3 ON t2.userid = t3.id WHERE t1.id = "' . $vars['orderid'] . '" AND t2.type = "Hosting"'));
	$Email 		= $Data[0]->email;
	$Firstname 	= $Data[0]->firstname;
	foreach ($Data as $item) $relids[] = $item->relid;

	// Retreiving IP
	$CustomFieldValue = Capsule::select(Capsule::raw('SELECT value FROM tblcustomfieldsvalues WHERE relid IN (\'' . implode('\',\'', $relids) . '\') AND fieldid = "' . $CustomFieldID . '"'));

	foreach ($CustomFieldValue as $ip)
	{
		// I'm just priting the command you'll need to run with exec or stuff like that. This is the output:
		// /root/automate_script.sh 127.0.0.1 Mike mike@example.com
		// /root/automate_script.sh 127.0.0.2 Mike mike@example.com
		echo '/root/automate_script.sh ' . $ip->value . ' ' . $Firstname . ' ' . $Email . '<hr>';
	}
});

 

Share this post


Link to post
Share on other sites

Hi Kian,

Thank you so much for this code. Can you change it a bit to include Last Name too, and the Name should include both First and Last name before passing to the script. Also, is it possible to use shell_exec function within this hook to execute the script there? 

I would also like to confirm if I have more than one products with the same custom field name, then the customfield ID would varies so how I would set if ordered product is 'Hosting' then CustomFieldID would be 4, and if it is 'Email Hosting' then CustomFieldID would be 5. So a if check needed before that statement. Can you assist me to implement this.

Once again thanks for this code 😊

Share this post


Link to post
Share on other sites
<?php

use WHMCS\Database\Capsule;

add_hook('AcceptOrder', 1, function($vars)
{
	// Replace with the ID of your Product Custom Field
        $CustomFieldID[] = '4';
        $CustomFieldID[] = '5';

	// Retreiving email and firstname and relid needed to get IPs later on
	$Data 		= Capsule::select(Capsule::raw('SELECT t2.relid, t3.email, t3.firstname, t3.lastname, CONCAT(t3.firstname, " ", t3.lastname) AS clientname FROM tblorders AS t1 LEFT JOIN tblinvoiceitems AS t2 ON t1.invoiceid = t2.invoiceid LEFT JOIN tblclients AS t3 ON t2.userid = t3.id WHERE t1.id = "' . $vars['orderid'] . '" AND t2.type = "Hosting"'));
	$Email 		= $Data[0]->email;
	$Firstname 	= $Data[0]->firstname;
	$Lastname 	= $Data[0]->lastname;
	$Clientname     = $Data[0]->clientname;
	foreach ($Data as $item) $relids[] = $item->relid;

	// Retreiving IP
	$CustomFieldValue = Capsule::select(Capsule::raw('SELECT value FROM tblcustomfieldsvalues WHERE relid IN (\'' . implode('\',\'', $relids) . '\') AND fieldid IN (\'' . implode('\',\'', $CustomFieldID) . '\')'));

	foreach ($CustomFieldValue as $ip)
	{
		// I'm just priting the command you'll need to run with exec or stuff like that. This is the output:
		// /root/automate_script.sh 127.0.0.1 Mike mike@example.com
		// /root/automate_script.sh 127.0.0.2 Mike mike@example.com
		echo '/root/automate_script.sh ' . $ip->value . ' ' . $Firstname . ' ' . $Email . '<hr>';
	}
});

Where...

  • $Firstname -> Jack
  • $Lastname -> Black
  • $Clientname -> Jack Black
  • $Email -> whatever@example.com
19 hours ago, Believe_ said:

Also, is it possible to use shell_exec function within this hook to execute the script there?

This is what you are supposed to do.

19 hours ago, Believe_ said:

I would also like to confirm if I have more than one products with the same custom field name, then the customfield ID would varies so how I would set if ordered product is 'Hosting' then CustomFieldID would be 4, and if it is 'Email Hosting' then CustomFieldID would be 5. So a if check needed before that statement. Can you assist me to implement this.

The updated script already support this thing. You need to add an element for each of your Product.

$CustomFieldID[] = '4';
$CustomFieldID[] = '5';
$CustomFieldID[] = '6';
$CustomFieldID[] = '7';

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

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