Jump to content

Disable £0 Invoice Email Notifications


Bertie

Recommended Posts

Hi,

 

Can't post in the original thread: 

 

Basically emails are still being sent out when £0 invoices are being raised and causing a bit of confusion with clients so would like to get it disabled. It seems the HOOK I have is not working. I don't mind £0 invoices being raised as I don't think that can be disabled but would love to stop them from being sent out to clients when they raise.  This is the hook I have so far:

 

<?php
function disable_00_invoices($vars) {

   $email_template_name = $vars['messagename']; # Email template name being sent
   $relid = $vars['relid']; # Related ID it's being sent for - client ID, invoice ID, etc...

   //Checking for certain template name, if so - this is our case
   if ($email_template_name == "Invoice Created" || $email_template_name == "Invoice Payment Confirmation")
   {
       //getting total of the invoice
       $result = select_query('tblinvoices', 'total', array("id" => $relid));
       $data = mysql_fetch_assoc($result);
       //if it is equal to '0.00' we disable email sending
       if (isset($data['total']) && $data['total'] == '0.00')
       $merge_fields['abortsend'] = true;
   }

   return $merge_fields;
}

add_hook("EmailPreSend",1,"disable_00_invoices");

 

WHMCS Version:7.7.1

 

Any help will be appreciated. 

 

Cheers,

Link to comment
Share on other sites

Hi.

You can use this hook:

<?php
use WHMCS\Database\Capsule;

add_hook('EmailPreSend', 1, function($vars) {
	$email_template_name = $vars['messagename'];
	$merge_fields = [];
    if($email_template_name == 'Invoice Created' || $email_template_name == 'Invoice Payment Confirmation'){
		$total = Capsule::table('tblinvoices')
			->where('id', $vars['relid'])
			->value('total');
		if($total == '0.00'){
			$merge_fields['abortsend'] = true;
		}
	}
    return $merge_fields;
});

 

Link to comment
Share on other sites

3 hours ago, Bertie said:

This is the hook I have so far

that's an old hook! 👴

I think to rewrite it for Capsule, it should be...

<?php

# Disable 0.00 Invoices Hook v2
# Written by brian!

use WHMCS\Database\Capsule;

function disable_00_invoices_v2($vars) {

	$validtemplates = array("Invoice Created","Invoice Payment Confirmation");
	if (in_array($vars["messagename"], $validtemplates)) {
		$merge_fields = [];
		$invoicetotal = Capsule::table('tblinvoices')->where('id',$vars['relid'])->value('total');
		if ($invoicetotal == "0.00") {
			$merge_fields['abortsend'] = true;
			return $merge_fields;
		}		
	}
}
add_hook("EmailPreSend", 1, "disable_00_invoices_v2");

lol - Dennis beat me to it.

Link to comment
Share on other sites

5 minutes ago, DennisMidjord said:

And I can't imagine how it has ever worked since the if sentence doesn't have any brackets 😛 Would that have been working at some point?

it must have - possibly it still would under PHP5.6 (haven't checked) - there are missing brackets in some of the reports IF statements and they still work... it doesn't look "right" though.

4 minutes ago, Bertie said:

I think the hook was located on another website a while back - Thanks guys, I will give it a go! 

I remember it from a whmcsjet post back in 2012... though whether they wrote it or they found it somewhere, I don't know.

Link to comment
Share on other sites

  • 7 months later...

Anything changed in the recent updates of WHMCS to stop this from working? Seems like the £0 email notification is being sent out. I have the following code:

<?php

# Disable 0.00 Invoices Hook v2
# Written by brian!

use WHMCS\Database\Capsule;

function disable_00_invoices_v2($vars) {

        $validtemplates = array("Invoice Created","Invoice Payment Confirmation");
        if (in_array($vars["messagename"], $validtemplates)) {
                $merge_fields = [];
                $invoicetotal = Capsule::table('tblinvoices')->where('id',$vars['relid'])->value('total');
                if ($invoicetotal == "0.00") {
                        $merge_fields['abortsend'] = true;
                        return $merge_fields;
                }
        }
}
add_hook("EmailPreSend", 1, "disable_00_invoices_v2");

 

Edited by Bertie
Link to comment
Share on other sites

16 minutes ago, Bertie said:

Anything changed in the recent updates of WHMCS to stop this from working?

not that i'm aware of. 😕

I just tried it on v7.10.1 with an existing 0.00 invoice in the admin area - with the hook disabled, the email was sent; with the hook enabled, it wasn't.

88sP5VI.png

it would be interesting to know under what conditions the hook doesn't appear to work.

Link to comment
Share on other sites

21 hours ago, brian! said:

not that i'm aware of. 😕

I just tried it on v7.10.1 with an existing 0.00 invoice in the admin area - with the hook disabled, the email was sent; with the hook enabled, it wasn't.

88sP5VI.png

it would be interesting to know under what conditions the hook doesn't appear to work.

We have a hosting package that includes a free renewal and states that on the invoice the client gets. WHMCS raises the invoice for the package as normal but then it also raises an invoice just for the domain name renewal with £0 value. This £0 invoice is the one that still gets sent out via email by the looks of it. Looking at the activity log for "Email Sending Aborted by Hook". There is a lot of "Email Sending Aborted by Hook (Subject: Invoice Payment Confirmation)" and only a couple of "Email Sending Aborted by Hook (Subject: Customer Invoice)" every so often.

This is using: 7.9.2 and not 7.10.1 but I can't see updating fixing it unless WHMCS did change something between those versions. 

 

Edited by Bertie
Link to comment
Share on other sites

24 minutes ago, Bertie said:

The product is a hosting package that includes a free 1 year domain renewal. WHMCS generates an invoice with an actual £ value in it and then the next date, it generates a £0 invoice for the domain renewal. It's that £0 domain renewal invoice that still gets sent out via email. Looking at the activity log for "Email Sending Aborted by Hook". There is a lot of "Email Sending Aborted by Hook (Subject: Invoice Payment Confirmation)" and only a couple of "Email Sending Aborted by Hook (Subject: Customer Invoice)" every so often.

quick thought - can you check that these 0.00 domain renewal invoices used the "Customer Invoice" email template and not the "Credit Card Customer Invoice" (which wouldn't trigger the hook) - hopefully the content of the two templates are different enough to be able to tell which was used.

27 minutes ago, Bertie said:

This is using: 7.9.2 and not 7.10.1 but I can't see updating fixing it unless WHMCS did change something between those versions. 

I don't think so - I can test it on v7.9.2 once you rule out the CCCI email template being used.

Link to comment
Share on other sites

1 hour ago, brian! said:

quick thought - can you check that these 0.00 domain renewal invoices used the "Customer Invoice" email template and not the "Credit Card Customer Invoice" (which wouldn't trigger the hook) - hopefully the content of the two templates are different enough to be able to tell which was used.

I don't think so - I can test it on v7.9.2 once you rule out the CCCI email template being used.

The two templates are the exact same content by the looks of it - Is there a way to find out which one was actually triggered? 

Link to comment
Share on other sites

9 minutes ago, Bertie said:

The two templates are the exact same content by the looks of it - Is there a way to find out which one was actually triggered? 

interesting question - I don't know to be honest... i've always kept their content different in the devs, so never had reason to check.

I could think of one possible way if you could resend them again, but if you could resend them again, you would already know which template you were using anyway. ☺️

I don't suppose you have a relevant 0.00 invoice on a test account that you can resend via the invoice page ?

Link to comment
Share on other sites

1 hour ago, brian! said:

interesting question - I don't know to be honest... i've always kept their content different in the devs, so never had reason to check.

I could think of one possible way if you could resend them again, but if you could resend them again, you would already know which template you were using anyway. ☺️

I don't suppose you have a relevant 0.00 invoice on a test account that you can resend via the invoice page ?

I'm going to assume it may use the credit card one as when you try and re-send it using "Invoice Created" - A message pop ups saying it's been aborted by the hook. But credit card one sends just fine. 

Link to comment
Share on other sites

Just now, Bertie said:

I'm going to assume it may use the credit card one as when you try and re-send it using "Invoice Created" - A message pop ups saying it's been aborted by the hook. But credit card one sends just fine. 

in that case...

$validtemplates = array("Invoice Created","Invoice Payment Confirmation","Credit Card Invoice Created","Credit Card Payment Confirmation");
Link to comment
Share on other sites

  • 2 years later...
On 9/25/2019 at 11:05 AM, brian! said:

that's an old hook! 👴

I think to rewrite it for Capsule, it should be...


<?php

# Disable 0.00 Invoices Hook v2
# Written by brian!

use WHMCS\Database\Capsule;

function disable_00_invoices_v2($vars) {

	$validtemplates = array("Invoice Created","Invoice Payment Confirmation");
	if (in_array($vars["messagename"], $validtemplates)) {
		$merge_fields = [];
		$invoicetotal = Capsule::table('tblinvoices')->where('id',$vars['relid'])->value('total');
		if ($invoicetotal == "0.00") {
			$merge_fields['abortsend'] = true;
			return $merge_fields;
		}		
	}
}
add_hook("EmailPreSend", 1, "disable_00_invoices_v2");

lol - Dennis beat me to it.

Hi, I'm a bit late to the conversation. I have a similar problem but my email template has name = "Order Confirmation" and is of type = "general", so $vars['ŕelid'] references the client ID, not the service ID. How can i make sure to select and check the invoice of the service (check that total=0.00) that triggered the Order Confirmation email, and doing it inside the EmailPreSend hook with only the client id?

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