Jump to content

Using Action Hooks to do something


RPS

Recommended Posts

Someone else might make use out of what I just finished coding.

 

The following script (inside of actionhooks.php) will run once an invoice has been paid for.

 

A perfect example of why you would need this is if you were selling a product that contains a serial number.

 

So you have a product created that requires a serial number of type 'Other'. You will also have a custom field named 'Serial Number' that is hidden on the order form.

 

When client places an order, an event is triggered to run the code inside the actionhooks file. The code will verify the correct product is ordered, and that you will need to generate a serial number.

 

Once the serial number is generated, it will update the custom field which contains that value.

 

You can manually e-mail the client, or you can wait for WHMCS to update the API so that it will be able to e-mail using an e-mail template. (Matt says this may be done within a week or so).

 

function actionhook_InvoicePaid($vars) {
# This function runs when an invoice is fully paid and therefore the services renewed
# $vars["InvoiceID"]

/*****************************************************************************************************************
This script is perfect if you are selling a product of type 'Other' that has a serial number associated with it.
It is assumed that you connect to another script that generates the serial number, in this case, I have hard coded it.

You will need to modify the $packageid == "0000000038" line to use the correct package id
You will also need to modify the $serialNumber = "SERIALNUMBER567890"; to input the correct information into the custom field
*****************************************************************************************************************/

// this example will insert something into a custom field when a package id is given (this example uses 0000000038 for the packageid)
// this code will check to make sure the correct package is ordered
// once it is verified to be correct package, it will lookup the custom field associated with the package
// once the custom field name is available, it will insert something into the value of the custom field

// we need invoiceid in order to do a lookup
$invoiceID = $vars['InvoiceID'];

// lookup relid from the invoiceid
$result = mysql_query("SELECT relid FROM tblinvoiceitems WHERE type='Hosting' AND invoiceid='".$invoiceID."'");

$grab_relid = mysql_fetch_row($result);
$relid = $grab_relid[0];

// lookup the packageid from the relid
$result = mysql_query("SELECT packageid FROM tblhosting WHERE id='".$relid."'");

$grab_packageid = mysql_fetch_row($result);
$packageid = $grab_packageid[0];

// verify package id is of the correct product type - one that should have additional action taken
if($packageid == "0000000038")
{
	// temporary serial number just for testing
	$serialNumber = "SERIALNUMBER567890";

	// grab the fieldname from the product associated with the package id entered
	$result = mysql_query("SELECT fieldname FROM tblcustomfields WHERE relid='".$packageid."'");

	$grab_fieldname = mysql_fetch_row($result);
	$fieldName = $grab_fieldname[0];

	// now we need to find the custom field id number since we know the name of the custom field
	// this will cause an error if there are more than 1 custom fields with the same name
	// suggestions for additional error handling here are welcome

	// note: id on tblcustomfields = fieldid on tblcustomfieldsvalues
	$result = mysql_query("SELECT id FROM tblcustomfields WHERE fieldname='".$fieldName."'");

	$grab_customfieldid = mysql_fetch_row($result);
	$customfieldid = $grab_customfieldid[0];

	// now we have the custom field id, the relid (which was looked up earlier based on the invoice), and then the serial number, 
	// we can insert this into the table to associate the serial number with the product purchased
	mysql_query("INSERT INTO tblcustomfieldsvalues (fieldid, relid, value) VALUES('".$customfieldid."', '".$relid."', '".$serialNumber."') ");  

	// call WHMCS API to send client e-mail

	// send out e-mail to say the test worked, comment this out when you are done testing
	$mydate = date ( 'l, F d Y g:i A',time()+240 );
	// where to send e-mail to
	$to = 'enter-your-email-here@domain.com';

	// e-mail subject
	$subject = "The Test Worked!";

	// e-mail message
	$message = "Hello,\r\n"
	."Details are below:\r\n"
	."-----------------------------------------\r\n"
	."Invoice ID: $invoiceID\r\n"
	."Relid: $relid\r\n"
	."Package ID: $packageid\r\n"
	."Custom Field Name for Package ID: $fieldName\r\n"
	."Date: $mydate\r\n";

	$headers = "From: $SSL Certificate <$noreply@example.com>\n"
	."Reply-To: noreply@example.com\n"
	."X-Mailer: PHP/".phpversion();

	mail( $to, $subject, $message, $headers );
	}
}

Link to comment
Share on other sites

Just put the php code under the appropriate spot within the file....

 

 

thank you I am able to run actionhooks as per my requirment.but one more issue I have to clear I want to access custom fields as their is table for custom fields how I supposed to go in order to get custom fields of specific client. Can you guide me on that

 

Thank you

Link to comment
Share on other sites

thank you I am able to run actionhooks as per my requirment.but one more issue I have to clear I want to access custom fields as their is table for custom fields how I supposed to go in order to get custom fields of specific client. Can you guide me on that

 

Thank you

- Read my first post, my code does this...

Link to comment
Share on other sites

  • 3 years later...
  • 1 month later...
This thread is like 3 years old, who knows if it even still works anymore.

 

 

It seems pretty likely that it would work - if all the prerequisites are met. One good reason it would not work is mismatch for the package id.

 

if($packageid == "0000000038")

 

Another might be case sensitivity in the variable passed. Docs for this hook indicate that 'invoiceid' is passed, not 'InvoiceID'.

 

David

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