Jump to content

Merge Fields - Total number of oustanding invoices


HR_UK

Recommended Posts

Hi All,

 

Is there a merge field for the total number of outstanding invoices the client has?

 

I'm aware of {$client_due_invoices_balance} but looking for number of items, not the balance.

 

On a side note - The online documentation http://docs.whmcs.com/Email_Templates#Merge_Fields is particuarly useless. Surely this would be the perfect page to list all of the merge files that WHMCS posseses along with a list of what type of email template it will work with rather than having to guess / ask on here / look at snippets at bottom of each default template.

 

 

Thanks, Mark

Link to comment
Share on other sites

Mark,

 

MergeFields is the fields you can specify by ActionHook:PreEmailSend, the variables available at the bottom of template can be differ based on what type of email template you are editing (General, Admin, Products/Services, etc), also any 3rd party can provide specific mergefields there to specific type or email template.

Link to comment
Share on other sites

Hi,

 

I use the API but cannot understand how to get started with hooks.

 

I have used the WHMCS example from http://docs.whmcs.com/Hooks:EmailPreSend

and copied it into a file called "email_custom_variables.php" inside includes/hooks.

 

Then I have added {$my_custom_var} to the bottom of my "invoice created" template. But it shows nothing.

 

From the limited documentation, I am assuming that WHMCS just runs all the php files in the include directory so should be immediately 'active'?

 

Thanks, Mark

Link to comment
Share on other sites

Mark,

 

take a look at the thread below...

 

https://forum.whmcs.com/showthread.php?98066-Hooks-and-or-email-merge-fields-for-custom-urls

 

looking at the documentation code, it's not specifying the hook (don't know if you remembered to add that bit in to your code?) - but try some of the examples in the thread above as a starting point.

Link to comment
Share on other sites

Brilliant - Got it working now thank you.

 

How do I read the value of something from the template/email in the hook? I need the hook to know what client ID I am sending an invoice for so that I can goto the API to request the relevant information from the API.

 

Thanks, Mark

Link to comment
Share on other sites

How do I read the value of something from the template/email in the hook? I need the hook to know what client ID I am sending an invoice for so that I can goto the API to request the relevant information from the API.

based on what type of email template you are sending, you will need to query database to get clientid

Invoices => tblinvoices.userid

products => tblhosting.userid,

etc

Link to comment
Share on other sites

But I have nothing to query it with?

 

I understand how to add to the template with the hook, but how can I get /anything/ from the email template?

 

I need to get something, so I have a basis to query for the rest. i.e. I need it to have the invoice number or client ID. Then obviously with either of these I can get everything from the DB.

Link to comment
Share on other sites

Thanks guys.

 

For anyone wanting the complete result to show the number of outstanding invoices for a client, here it is (also useful example code of a basic hook + sql look up)

 

 

 

<?php
if (!defined("WHMCS"))	{
   die("This file cannot be accessed directly");	}

function hook_emailvars($vars) {

$root_path = substr ($_SERVER["DOCUMENT_ROOT"], 0, strpos ($_SERVER["DOCUMENT_ROOT"], "/public_html") + 12);

require ($root_path."/client/configuration.php");

$dblink = mysqli_connect($db_host, $db_username, $db_password);
mysqli_select_db ($dblink, $db_name);

$temp = mysqli_fetch_row (mysqli_query ($dblink, "SELECT userid FROM `tblinvoices` WHERE id=".$vars['relid']." LIMIT 1;") );

$invoice_qty = mysqli_num_rows (mysqli_query ($dblink, "SELECT id FROM `tblinvoices` WHERE userid='$temp[0]' AND status LIKE 'Unpaid';") );
if (!is_numeric ($invoice_qty) )	{	$invoice_qty = 0;	}
mysqli_close ($dblink);


$merge_fields = array();
$merge_fields['client_due_invoices_qty'] = $invoice_qty;


return $merge_fields;
}


add_hook("EmailPreSend",1,"hook_emailvars");				// Make the Custom Variables		
add_hook("EmailTplMergeFields",1,"hook_emailvars");			// Merge them onto the tpl so shown on edit template page

?>

Link to comment
Share on other sites

you could also use Capsule to access the database, which would remove the need to use the configuration file in your code. :idea:

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function hook_emailvars($vars) {

   $userid = Capsule::table('tblinvoices')
               ->where('id',$vars['relid'])
               ->pluck('userid');

   $invoice_qty = Capsule::table('tblinvoices')
               ->where('userid',$userid)
               ->where('status','Unpaid')
               ->count();            

   $merge_fields = array();
   $merge_fields['client_due_invoices_qty'] = $invoice_qty;

   return $merge_fields;
}
add_hook("EmailPreSend",1,"hook_emailvars");                // Make the Custom Variables        
add_hook("EmailTplMergeFields",1,"hook_emailvars");            // Merge them onto the tpl so shown on edit template page
?>

note - when using on v7, you might need to change pluck to value.

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