h.nejjar Posted September 8, 2020 Share Posted September 8, 2020 Hello, I'm using the following hook codes to display amount of services with tax: > <?php add_hook('EmailPreSend', 1, function($vars) { $merge_fields = []; if (!array_key_exists('my_prix_ht', $vars['mergefields'])) { $price_ht=$vars['service_recurring_amount']; $merge_fields['my_price_ht'] = $price_ht * 1.12; } return $merge_fields; }); > And on my email template i have the following lines: > Price: {$service_recurring_amount}. my_price_ht: {$my_custom_var} > But on the email sent i get the following result: > Price: 600.00 DD. my_price_ht: 0 > Is there any error on my hook code? Is it correct to use the following variable on my hook code: $price_ht=$vars['service_recurring_amount']; Can some one help please! Thank you, 0 Quote Link to comment Share on other sites More sharing options...
h.nejjar Posted September 8, 2020 Author Share Posted September 8, 2020 Hello, Just to correct: My hook code > <?php add_hook('EmailPreSend', 1, function($vars) { $merge_fields = []; if (!array_key_exists('my_price_ht', $vars['mergefields'])) { $price_ht=$vars['service_recurring_amount']; $merge_fields['my_price_ht'] = $price_ht * 1.12; } return $merge_fields; }); > And on my email template i have the following lines: > Price: {$service_recurring_amount}. my_price_ht: {$my_price_ht} > Thanks 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 9, 2020 Share Posted September 9, 2020 (edited) 23 hours ago, h.nejjar said: Is it correct to use the following variable on my hook code: $price_ht=$vars['service_recurring_amount']; your problem will be that $vars['service_recurring_amount'] is a string and not a number, and you can't multiply a string. you will have to convert it into a number before multiplying it, so the code below should work... $merge_fields['my_price_ht'] = $price_ht->toNumeric() * 1.12; the result won't be currency formatted, e.g it won't have "DD" after the price - if your site only has one currency, then you could easily add it in the hook or in the template; if it uses multiple currencies, then you would need to get the client's currency from the database and format the price accordingly. it's worth mentioning that if you were able to edit the email templates within the database directly, and not via the email templates pages in WHMCS, then you could do this in the template itself without the need for a hook... {$service_recurring_amount->toNumeric() * 1.12} would give the same result as the hook - again, it wouldn't be currency formatted, so if you have multiple currencies, then you might be better off with a hook - it's doable to format it in the template, but long-winded. you couldn't use the above code whilst editing the email template in WHMCS as it would change the > to > and potentially cause issues. Edited September 9, 2020 by brian! 0 Quote Link to comment Share on other sites More sharing options...
h.nejjar Posted September 10, 2020 Author Share Posted September 10, 2020 Hello, Thank you for your reply, I used the code you give me: $merge_fields['my_price_ht'] = $price_ht->toNumeric() * 1.12; But WHMCS gives me error, so i try to see if i'm getting the correct value of the "service_recurring_amount" and i try the following code: <?php add_hook('EmailPreSend', 1, function($vars) { $merge_fields = []; if (!array_key_exists('my_price_ht', $vars['mergefields'])) { $price_ht=$vars['service_recurring_amount']; $merge_fields['my_price_ht'] = $price_ht; } return $merge_fields; }); And i'm getting the following result, >> Price: 600.00 DD. my_price_ht: >> So i think i'm not getting the correct value of my service amount!! About the second solution, to change this directly on the database, i'm afraid it will be overwritten if i update WHMCS! Can you give the table where i can do this change. Thank you. 0 Quote Link to comment Share on other sites More sharing options...
h.nejjar Posted September 11, 2020 Author Share Posted September 11, 2020 Hello, Just for update, i changed the email template directly on the database using the value you sent me, and it works fine for me :) But still the hook do nt give me the correct value. Thank you Brian. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 11, 2020 Share Posted September 11, 2020 23 hours ago, h.nejjar said: So i think i'm not getting the correct value of my service amount!! oh I didn't check the rest of your code - the hook below should work and should format the result in your clients currency... <?php # Create Taxed Price Merge Field Hook # Written by brian! function tax_price_mergefield_hook($vars) { $merge_fields = $vars['mergefields']; $validtemplates = array("Hosting Account Welcome Email"); if (!array_key_exists('my_price_ht', $vars['mergefields']) && in_array($vars['messagename'],$validtemplates)) { $merge_fields['my_price_ht'] = formatCurrency(($merge_fields['service_recurring_amount']->toNumeric() * 1.12),getCurrency($merge_fields['client_id'])); } return $merge_fields; } add_hook("EmailPreSend",1,"tax_price_mergefield_hook"); also, you'll have to modify the $validtemplates array with the list of email templates that you want this hook to apply to - it's only going to be product/service type email templates that should have the specified recurring amount variable you're using. 5 minutes ago, h.nejjar said: Just for update, i changed the email template directly on the database using the value you sent me, and it works fine for me 🙂 hurrah! 😎 just remember never to edit that email template in WHMCS, otherwise when you save it, it will change the Smarty code used in the email template - though you can edit it in WHMCS if you plan to remove that code and just rely on the hook. 0 Quote Link to comment Share on other sites More sharing options...
h.nejjar Posted September 11, 2020 Author Share Posted September 11, 2020 Hello, I try to use the hook code you sent me, and changed the $validtemplates array with my email templates name, but whmcs gives error!! For information the tepmlate email i want to use is a custom email (but under the product/service emails). > just remember never to edit that email template in WHMCS, Yes i know, it clear Thanks again Brian 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 12, 2020 Share Posted September 12, 2020 20 hours ago, h.nejjar said: I try to use the hook code you sent me, and changed the $validtemplates array with my email templates name, but whmcs gives error!! what's the exact error ? 20 hours ago, h.nejjar said: For information the tepmlate email i want to use is a custom email (but under the product/service emails). I just tried the hook with a custom product/service email template and it worked fine when it was added to the array. what's the value of the $validtemplates array in your hook? 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.