Jump to content

Conditional email content for certain payment methods


Recommended Posts

We want to show some text in several invoice emails to the customers whose payment methods are Paypal or debit/credit cards.

I guess the code for that should be something like:

{if $invoice_payment_method eq "Paypal" or "Credit/Debit Card"}SAMPLE-TEXT{/if} 

But I'm not sure if the payment method for cards is really represented by "Credit/Debit Card" (I've seen it in one post of this community, but haven't found official documentation about this).

I also don't know if the "or" operator exists here.

And I'm also not sure if the conditional code will work if I enter it like normal text (using the Tinymce editor, with text format and all that) or if I have to open it in source code mode and remove all the formating that Tinymce could be adding around the conditional code. 

Thanks for your help!

Link to comment
Share on other sites

On 10/10/2018 at 09:41, marketingpro said:

I guess the code for that should be something like:

{if $invoice_payment_method eq "Paypal" or "Credit/Debit Card"}SAMPLE-TEXT{/if} 

alternatively, you should be able to use either of these..

{if in_array($invoice_payment_method, array('PayPal', 'Credit/Debit Card'))}Sample Text{/if}
{if $invoice_payment_method|in_array:['PayPal','Credit/Debit Card']}Sample Text{/if}
On 10/10/2018 at 09:41, marketingpro said:

But I'm not sure if the payment method for cards is really represented by "Credit/Debit Card" (I've seen it in one post of this community, but haven't found official documentation about this).

there are lots of things are missing from the official documentation - so often the only way is to test it and see - but I think it's correct... worst case, you can always throw a {debug} into the email, send yourself a test message and when you view the sent message in WHMCS, it should generate a popup window with all the available variables and their values... just remember to remove it from the template when you're finished.

On 10/10/2018 at 09:41, marketingpro said:

I also don't know if the "or" operator exists here.

it does.

On 10/10/2018 at 09:41, marketingpro said:

And I'm also not sure if the conditional code will work if I enter it like normal text (using the Tinymce editor, with text format and all that) or if I have to open it in source code mode and remove all the formating that Tinymce could be adding around the conditional code. 

for this, you should open it in source mode and enter your code directly in there... copy&pasting code normally can add all sorts of hidden rubbish to the template code and prevent successful sending.

generally when entering Smarty into an email template (as it bypasses WHMCS changing code upon submission), I would edit the database table directly - but i'd only recommend doing this if you were getting nowhere editing the template via the Source Code method... and in truth, i'm not seeing anything in the above code that WHMCS would try to change.

 

Link to comment
Share on other sites

Thanks a lot brian!

On 10/12/2018 at 4:14 PM, brian! said:

so often the only way is to test it and see - but I think it's correct... worst case, you can always throw a {debug} into the email, send yourself a test message and when you view the sent message in WHMCS, it should generate a popup window with all the available variables and their values... just remember to remove it from the template when you're finished.

How do I send myself a test message for a template? I don't see that option when I'm configuring an email template.  Or do you mean using de Mass Email tool or something else? I've searched a bit and found a possible solution in https://whmcs.community/topic/127549-test-send-email-templates/, but it seems like an overkill just for testing one email.

I also don't know what you mean by "view the sent message in WHMCS". Where we can look at the sent emails? 

Sorry, I'm not an advanced user of WHMCS.

Link to comment
Share on other sites

Hi,

5 hours ago, marketingpro said:

How do I send myself a test message for a template? I don't see that option when I'm configuring an email template.

depending on what type of email template it is, then sending test messages are available in different places...

  • to send a test "General" type, you could use the dropdown link on the client summary page in the admin area..

VHGTRD5.png

if you have a test client, then choosing the template from the dropdown and pressing "Go" will send it to that client... if you don't have a test client, make one! then choosing "New Message" will take you to the editor and you can edit your custom message there (including copy/paste into the source code viewer) and send it to any email address for testing.

  • to send a test "Products" or "Domains" email template, you can use the similar dropdown at the bottom of the page in the "Products" or "Domains" tabs in client summary.
  • to send a test "Invoices" email template, you can do that from the Invoices page in the admin area.

IYgSpn0.png

now we hit a problem with Admin templates because they are sent by WHMCS when triggered by specific events and not manually - so usually what you would do would be to repeat the procedure that triggers that particular email being sent and then repeat until you get the correct output.

5 hours ago, marketingpro said:

I also don't know what you mean by "view the sent message in WHMCS". Where we can look at the sent emails? 

in the client summary page of the client via the "Emails" tab.

qPdHrsR.png

when you click on any email in there, it will open it in a popup preview window... additionally, if that email template contained a {debug} at the time, it would open another popup window with a list of variables available to the email template.

5 hours ago, marketingpro said:

Sorry, I'm not an advanced user of WHMCS.

no worries - we were all newbies here once. thanks.png

Link to comment
Share on other sites

  • 2 weeks later...

Thanks a lot Gearhead!

I've been doing a lot of experiments with "Invoices" template emails, but I'm not getting the expected results. Even with a very simple conditional, like {if $invoice_payment_method eq "Paypal"}TEXT{/if} or {if $invoice_payment_method eq "Paypal"}TEXT{else}NOTEXT{/if}), I'm getting bad results (TEXT is showing when it shouldn't, when the payment method for the invoice/customer I'm using for the tests is not Paypal). 

I'm not sure if I'm doing something wrong with the conditionals code (I'm putting it in the source code and it seems OK, but who knows...), with the customer account I'm using for the tests (it's not a real account, it's services are cancelled actually) or something else.

I'm using {debug} and when I change the payment method in the customer account it reflects OK in the variable (I see the change in the debug popup), but in the email itself I don't see what I expect.

Any clue of what could be the problem? Should I send an email to WHMCS support for this?

 

Link to comment
Share on other sites

Hi @marketingpro,

Are you searching for the term that you have used as the "Display Name" in the Gateway setup? e.g. Have you really called it Paypal and not PayPal - note the case sensitivity.

Try printing the payment method in the emails to ensure that you are actually testing for the right condition i.e.  

Payment Method: {$invoice_payment_method}

 

Link to comment
Share on other sites

On 26/10/2018 at 08:57, marketingpro said:

Any clue of what could be the problem?

@Vox is right when he mentions case-sensitivity - if you look in the debug window you will see that paypal is called "PayPal" and so this is what you'll have to search for *exactly* in your conditional statements...

ltDL4q6.png

{if $invoice_payment_method eq "PayPal"}PayPal text{else}NOTEXT{/if}

if you don't want to worry about case-sensitivity, then you could use the following in the email template...

{if $invoice_payment_method|strtolower eq "paypal"}PayPal text{else}NOTEXT{/if}

just be aware that doing this would require you to modify your Smarty Security Policy to allow the PHP modifier 'strtolower' to be used in email templates (otherwise you'll get an error message and the email won't send)... but in this instance, just using the if x eq y format should be sufficient - as long as you get 'y' exactly correct. thanks.png

Link to comment
Share on other sites

Thanks, the case in PayPal was one of the problems, but not the only one.

Now I get what I expect when I put:

{if $invoice_payment_method eq "PayPal"}PayPal text{else}NOTEXT{/if}

But when I include the "or" (my original question) it doesn't work as expected, it shows "Paypal text" for everyone (it doesn't matter which payment method is activated). I've even tried with "Paypal or Paypal" and it still shows "Paypal text" for everyone. 

{if $invoice_payment_method eq "PayPal" or "PayPal"}PayPal text{ else}NOTEXT{/if}

I've tried the alternatives to the "or" that Brian included in his first message:

{if in_array($invoice_payment_method, array('PayPal', 'Credit/Debit Card'))}Sample Text{/if}
{if $invoice_payment_method|in_array:['PayPal','Credit/Debit Card']}Sample Text{/if}

They both give me the following error when trying to send the email:

Email Sending Failed - Email message rendered empty - please check the email message Smarty markup syntax 

 

Link to comment
Share on other sites

Hi @marketingpro,

I'm no coder but the error message is telling you that there is an error in your code.

23 hours ago, marketingpro said:

please check the email message Smarty markup syntax

That would indicate that you've added something or left something out of brian!s code. Please copy it exactly as posted.

As per the PayPal case sensitivity issue, have you checked that your other payment type is exactly the same as

Credit/Debit Card

I couldn't even start to explain what an array is, so I have to build these up from scratch when I need them and would suggest something like

{if $invoice_payment_method eq "PayPal"}

Your PayPal text here

{else if $invoice_payment_method eq “Your other payment type"}

Your other payment type text here

{else}

Your default text here

{/if}

Just check that you are trying to match the exact other payment type.

Edited by Vox
Link to comment
Share on other sites

also, the activity log will be your friend when you get an error sending an email template - it should tell you what and where the issue is... it can occasionally be a little off with where, but it will tell you the cause of the send failure... and that will give you a great clue as to what to check. thanks.png

Link to comment
Share on other sites

The code provided by Brian! it's the cause of the error. I tried with the 2 options he provided (copied exactly) again and it gives the error. I don't have access to the log, but anyway, I prefer to use the "OR" operator and not lose more time with that code with errors.

The code provided by Vox (if, else if and else) works, but it's not exactly what I was looking for and it less efficient than the "OR" for my needs.

The"OR" code doesn't give an error, but it doesn't behave as expected. Are you 100% sure that the "OR" operator exists and works OK in WHMCS? 

I guess I will use the code by Vox or maybe contact directly to the WHMCS support team. 

Thanks anyway. 

 

Link to comment
Share on other sites

18 hours ago, marketingpro said:

The code provided by Brian! it's the cause of the error.

both lines of code work fine - they were tested before posting, and i've just double-checked them again.

18 hours ago, marketingpro said:

I tried with the 2 options he provided (copied exactly) again and it gives the error.

what you probably failed to do is follow what I said about needing to modify your Smarty Security Policy to allow this to work - absolutely, if you don't do that, then you'll see the error message that you describe.

18 hours ago, marketingpro said:

I don't have access to the log, but anyway, I prefer to use the "OR" operator and not lose more time with that code with errors.

good luck debugging Smarty errors in the email templates without access to the logs - you've wasted your own time by not having access to them! headshake.gif

if you could have seen the logs, then you would have seen something like this...

Tk6YhJ6.png

the first red arrow would have told you that it was an issue with the Smarty Security Policy (as I previously warned) and the fact that you hadn't modified it to allow 'in_array' as a modifier - if you had just followed that one simple step, then the email template would have sent without any errors - as shown above next to the green arrow when I modified the policy and resent the email. thanks.png

iGMId1H.png

18 hours ago, marketingpro said:

The"OR" code doesn't give an error, but it doesn't behave as expected. Are you 100% sure that the "OR" operator exists and works OK in WHMCS? 

i'm 126% sure that it exists and works in WHMCS - example taken from the Smarty docs page...

{* an example with "or" logic *}
{if $name eq 'Fred' or $name eq 'Wilma'}
   ...
{/if}

{* same as above *}
{if $name == 'Fred' || $name == 'Wilma'}
   ...
{/if}

do you see where you went wrong with your code? you won't need to make any security policy modifications using the above code.

also if it doesn't work, then why have WHMCS used it themselves in at least two email templates - Support Ticket Change Notification and the SiteLock Welcome Email ? 🙄

18 hours ago, marketingpro said:

I guess I will use the code by Vox or maybe contact directly to the WHMCS support team. 

WHMCS support might send you back here to the Community if they think it's a customisation question. 🙂

Link to comment
Share on other sites

You didn't mention that I needed to change the Security Policy to use your "array" code, you only mentioned it with the "strtolower" code. 

Anyway, as I've said I prefer to use "or" and not change a Security Policy, so I'm fine with the "or" code you mentioned now from the docs. It works 🙂

So the solution to my question was just 2 small corrections to the code I put in my first message. It's not {if $invoice_payment_method eq "Paypal" or "Credit/Debit Card"}SAMPLE-TEXT{/if}, but {if $invoice_payment_method eq "PayPal" or $invoice_payment_method eq "Credit/Debit Card"}SAMPLE-TEXT{/if}

Thanks for your help

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