Jump to content

Hide Custom Fields from Email Templates


SeanP

Recommended Posts

I need to hide certain product custom fields from all the email templates where they would be displayed.  Basically any of the email templates where {$invoice_html_contents} is used.  Is there an easy was to do this with a hook for all templates, or would I have to do it for each template?

Link to comment
Share on other sites

It looks like I need to hide certain product custom fields in the following email templates:

Invoice Payment Confirmation
Credit Card Payment Confirmation
Order Confirmation
Invoice Created
Invoice Modified

The custom fields are being displayed from the following 2 tags:
{$invoice_html_contents}
{$order_details}

These custom fields have to be visible (cannot be set to Admin Only) for functionality in custom modules.  We have them hidden everywhere else, we just noticed they are displaying in some email templates.

Edited by SeanP
Link to comment
Share on other sites

3 hours ago, SeanP said:

Is there an easy was to do this with a hook for all templates, or would I have to do it for each template?

I would expect that it should only need to be one EmailPreSend hook, with 2 conditions within it - if order confirmation, modify the $order_details variable; if one of the listed invoice templates, modify $invoice_html_contents.

I think in both cases, those variables are HTML strings - so if you know the values/content of those customfield lines, then you might get away with just using a string replace on them; if not, you might need to explode them into an array and remove these fields that way.

Link to comment
Share on other sites

6 hours ago, brian! said:

I would expect that it should only need to be one EmailPreSend hook, with 2 conditions within it - if order confirmation, modify the $order_details variable; if one of the listed invoice templates, modify $invoice_html_contents.

I think in both cases, those variables are HTML strings - so if you know the values/content of those customfield lines, then you might get away with just using a string replace on them; if not, you might need to explode them into an array and remove these fields that way.

Thanks.  I guess to start I need to figure out what format those two variables are in.

Link to comment
Share on other sites

9 hours ago, brian! said:

I would expect that it should only need to be one EmailPreSend hook, with 2 conditions within it - if order confirmation, modify the $order_details variable; if one of the listed invoice templates, modify $invoice_html_contents.

I think in both cases, those variables are HTML strings - so if you know the values/content of those customfield lines, then you might get away with just using a string replace on them; if not, you might need to explode them into an array and remove these fields that way.

Sorry for my ignorance, but I haven't done a whole lot with the emailpresend hook.  How to you pull in the $invoice_html_contents and/or $order_details variables into the hook?

Link to comment
Share on other sites

So here is my final result:

function email_presend_filter($vars) {

  if (isset($vars['mergefields']['order_details'])){
    $ordercontents = $vars['mergefields']['order_details'];
    $ordercontents = preg_replace('/^ITEMTOREMOVE.*?<br>$/m', '', $ordercontents);

    $merge_fields['order_details'] = $ordercontents;
    return $merge_fields;
  }

  if (isset($vars['mergefields']['invoice_html_contents'])){
    $invoicecontents = $vars['mergefields']['invoice_html_contents'];
    $invoicecontents = preg_replace('/^ITEMTOREMOVE.*?<br>$/m', '', $invoicecontents);

    $merge_fields['invoice_html_contents'] = $invoicecontents;
    return $merge_fields;
  }

}

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

If you replace "ITEMTOREMOVE" with the custom field name you want removed from order confirmations and invoice emails, it will remove the line where that field resides.

Link to comment
Share on other sites

On 5/10/2021 at 2:56 AM, brian! said:

I would expect that it should only need to be one EmailPreSend hook, with 2 conditions within it - if order confirmation, modify the $order_details variable; if one of the listed invoice templates, modify $invoice_html_contents.

I think in both cases, those variables are HTML strings - so if you know the values/content of those customfield lines, then you might get away with just using a string replace on them; if not, you might need to explode them into an array and remove these fields that way.

Hey Brian!,

Do you ever do any WHMCS dev work for hire (unrelated to this thread)?  I tried to PM you...

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