Jump to content

Hide date range in invoice emails - on new domain name orders


cluster

Recommended Posts

Would it be possible to hide date range in invoice emails (only for new domain orders)?
By default "Expiry Date" should be empty if a NEW domain name order is received e.g.
00/00/0000

Following hook removes the date range for all invoices ... I just want to remove the date for "Domain name" items and only when the "Expiry Date" is 00/00/0000.
https://github.com/Katamaze/WHMCS-Action-Hook-Factory/blob/master/hooks/noDatesInInvoiceItemsDescription.php

It doesn't work with the Smarty email templates, so, I’d like to apply a similar condition into this hook ... any idea?

{if $domain_expiry_date eq "00/00/0000"}

 

<?php

/**
 * No Dates in Invoice Items Description
 *
 * @package     WHMCS
 * @copyright   Katamaze
 * @link        https://katamaze.com
 * @author      Davide Mantenuto <info@katamaze.com>
 */

use WHMCS\Database\Capsule;

add_hook('InvoiceCreationPreEmail', 1, function($vars) {

    $items = Capsule::table('tblinvoiceitems')->select('id', 'description')->where('invoiceid', '=', $vars['invoiceid'])->get();

    if (!$items) {

        return;
    }

    $dateFormat = Capsule::table('tblconfiguration')->select('value')->where('setting', '=', 'DateFormat')->first();

    if (in_array($dateFormat->value, [ 'DD/MM/YYYY', 'DD.MM.YYYY', 'DD-MM-YYYY' ])) {

        $regex = '/[" "][(](((0[1-9]|[12][0-9]|3[01])[- \/.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[- \/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[- \/.]02)[- \/.]\d{4}|29[- \/.]02[- \/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[\w -]*[-]*[\w -](((0[1-9]|[12][0-9]|3[01])[- \/.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[- \/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[- \/.]02)[- \/.]\d{4}|29[- \/.]02[- \/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[)]/';
    }
    elseif ($dateFormat->value == 'MM/DD/YYYY') {

        $regex = '/[" "][(](((0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-\/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[-\/.]02)[-\/.](0[1-9]|[12][0-9]|3[01])[-\/.]\d{4}|29[-\/.]02[-\/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[\w -]*[-]*[\w -](((0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-\/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[-\/.]02)[-\/.](0[1-9]|[12][0-9]|3[01])[-\/.]\d{4}|29[-\/.]02[-\/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[)]/';
    }
    elseif (in_array($dateFormat->value, [ 'YYYY/MM/DD', 'YYYY-MM-DD' ])) {

        $regex = '/[" "][(](\d{4}|29[-\/.]02[-\/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[-\/.]((0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-\/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[-\/.]02)[-\/.](0[1-9]|[12][0-9]|3[01])[\w -]*[-]*[\w -](\d{4}|29[-\/.]02[-\/.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))[-\/.]((0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-\/.](0[469]|11)|(0[1-9]|1\d|2[0-8])[-\/.]02)[-\/.](0[1-9]|[12][0-9]|3[01])[)]/';
    }

    foreach ($items AS $v) {

        $v->description = preg_replace($regex, '', $v->description);
        Capsule::table('tblinvoiceitems')->where('id', '=', $v->id)->update([ 'description' => $v->description ]);
    }
});

 

 

Link to comment
Share on other sites

If I extend the condition w/ "type" -> DomainRegister ...  that should work, but it doesn't ... (no preg_replace).

e.g.

$ptype = "DomainRegister";
$items = Capsule::table('tblinvoiceitems')->select('id', 'type', 'description')->where('type', $ptype)->where('invoiceid', '=', $vars['invoiceid'])->get();

or

$ptype = "DomainRegister";
$items = Capsule::table('tblinvoiceitems')->select('id', 'type', 'description')->where('type', '=', $ptype)->where('invoiceid', '=', $vars['invoiceid'])->get();

did I missed something here... what am I doing wrong?

Link to comment
Share on other sites

The perfect way to do this is, by editing you email templates itself.

Go to your admin account and from System settings list down all of the email templates by searching Email Templates.

It will take you to a page like 

domain.com/admin/configemailtemplates.php then locate the email template that was sent for the order. I f you can tell me the subject of the email, I can suggest you how to fix this.


 

Link to comment
Share on other sites

found a working solution ...

 

I modified the hook so that it only removes date for new domain name orders (type > DomainRegister).

<?php

use WHMCS\Billing\Invoice;

add_hook('InvoiceCreation', 1, function($vars) {
	// $vars allows us to use get the specific invoice id
	$invoiceID = $vars['invoiceid'];
        $ptype = 'DomainRegister';
		
	// Get data related to the invoice. See this for documentation on models: https://docs.whmcs.com/Using_Models
	// It would also be possible to use Capsule to query the database instead of using the Invoice model. See information on database queries here: https://developers.whmcs.com/advanced/db-interaction/
	$invoiceItems = Invoice::find($invoiceID)->items->where('type', $ptype);

	$pattern = '/\(([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9]) - ([0-3][0-9]|[0-1][0-9]|[\d]{4})(\/|\.|-)([0-1][0-9]|[0-3][0-9]|)(\/|\.|-)([\d]{4}|[0-1][0-9]|[0-3][0-9])\)/';

	foreach ($invoiceItems as $invoiceItem)
	{
		preg_match($pattern, $invoiceItem->description, $matches);
		$invoiceItem->description = str_replace($matches[0], '', $invoiceItem->description); // Update the description of the invoice's line item
		$invoiceItem->save(); // Save to the database
	}
    
});

 

Edited by cluster
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