Kevin K Posted September 19, 2020 Share Posted September 19, 2020 I have some custom coding working along with the ModulesGarden Discount Center. Basically this custom math code will show the discount price, percent saved, and Pay Now price in the order form page. Although I am having an issue when the currency suffix is enabled in WHMCS admin and I am hoping someone can offer some help so I can use the different currency suffixs with this code. The custom math code: {if $pricing.quarterly|strstr:"(" && !$pricing.quarterly|strstr:$LANG.orderfreedomainonly} {assign var="temp" value=$currency.prefix|explode:$pricing.quarterly} {assign var="discounted_price" value='('|explode:$temp['1']|replace:" ":""} {math assign="original_price" equation="y/x" x=3 y=$pricing.rawpricing.quarterly format="%.2f"} {math assign="percent_save" equation="(x/y)*100" x=$discounted_price['0'] y=$original_price} {math assign="pay_now_price" equation="(x*y)/100" x=$pricing.rawpricing.quarterly y=$percent_save format="%.2f"} <div class="check-price"> <b>{$currency.prefix}{$discounted_price['0']|string_format:"%.2f"}{$currency.suffix|replace:" ":""}</b>/{$LANG.pricingCycleShort.monthly} <b>(Save {$percent_save|string_format:"%d"}%</b>, Regular Price: {$currency.prefix}{$original_price}{$currency.suffix|replace:" ":""}/{$LANG.pricingCycleShort.monthly}) </div> <div class="check-price">{$LANG.invoicespaynow}: <b>{$currency.prefix}{$pay_now_price}{$currency.suffix}</b></div> Everything seems to work fine if I disable the Currency Suffix (USD) from the whmcs and I will get the following: Although if I enable the currency suffix (USD) from the WHMCS admin, I get the following: Hopefully someone has some ideas of how I can resolve this as I really would like to be able to show the currency suffix in the order form and still work with this custom coding. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 20, 2020 Share Posted September 20, 2020 18 hours ago, Kevin K said: Hopefully someone has some ideas of how I can resolve this as I really would like to be able to show the currency suffix in the order form and still work with this custom coding. if you use the code below, it should return the quarterly price with no prefix or suffix (assuming suffix contains a space)... {assign var="discounted_price" value=$temp['1']|strstr:' ':true} though with a combination of MGDC coding and Lagom's own Smarty code involved, it's difficult to follow the logic. 1 Quote Link to comment Share on other sites More sharing options...
Kevin K Posted September 20, 2020 Author Share Posted September 20, 2020 (edited) Thanks for taking a swing at this. Unfortunately it produced the wrong value for everything in the quarterly and the wrong save percentage for all of the billing terms, although I only changed the code on the quarterly entry. If I change all billing terms to include the new code, I get the following: Edited September 20, 2020 by Kevin K 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 21, 2020 Share Posted September 21, 2020 20 hours ago, Kevin K said: Thanks for taking a swing at this. Unfortunately it produced the wrong value for everything in the quarterly and the wrong save percentage for all of the billing terms, although I only changed the code on the quarterly entry. I wasn't confident it would work - I wouldn't mind seeing a debug of the pricing array because it's using modified variables, and specifically the values temp and discounted price are using... if it were me and I knew the variables, i'd do this in a hook rather than the template. have you asked ModulesGarden about this, or would they charge you an arm, leg and first born for the answer ? 👶 1 Quote Link to comment Share on other sites More sharing options...
Kevin K Posted September 21, 2020 Author Share Posted September 21, 2020 15 minutes ago, brian! said: I wasn't confident it would work - I wouldn't mind seeing a debug of the pricing array because it's using modified variables, and specifically the values temp and discounted price are using... if it were me and I knew the variables, i'd do this in a hook rather than the template. have you asked ModulesGarden about this, or would they charge you an arm, leg and first born for the answer ? 👶 Ya I did talk to ModulesGarden about this and they want a minimum $250 to even look at it, not figuring "additional hours". Hopefully this is the pricing array you were looking for: $pricing Origin: "Smarty object" Value Array (9) type => "recurring" quarterly => "3 Month Price $4.48USD (Save 50%, Regular Price: $8.95USD) " semiannually => "6 Month Price $4.48USD (Save 50%, Regular Price: $8.95USD) " annually => "12 Month Price $3.98USD (Save 50%, Regular Price: $7.95USD) " biennially => "24 Month Price $3.48USD (Save 50%, Regular Price: $6.95USD) " hasconfigoptions => true cycles => Array (4) quarterly => "3 Month Price - $8.95USD" semiannually => "6 Month Price - $8.95USD" annually => "12 Month Price - $7.95USD" biennially => "24 Month Price - $6.95USD" rawpricing => Array (12) msetupfee => "0.00" qsetupfee => "0.00" ssetupfee => "0.00" asetupfee => "0.00" bsetupfee => "0.00" tsetupfee => "0.00" monthly => "-1.00" quarterly => "26.85" semiannually => "53.70" annually => "95.40" biennially => "166.80" triennially => "-1.00" minprice => Array (6) price => WHMCS\View\Formatter\Price Object (0) setupFee => 0 cycle => "monthly" simple => "$8.95" cycleText => "<span>$8.95</span>/3 mo" cycleTextWithCurrency => "<span>$8.95USD</span>/3 mo" What odd is the code seems to working fine in the array: quarterly => "3 Month Price $4.48USD (Save 50%, Regular Price: $8.95USD) " semiannually => "6 Month Price $4.48USD (Save 50%, Regular Price: $8.95USD) " annually => "12 Month Price $3.98USD (Save 50%, Regular Price: $7.95USD) " biennially => "24 Month Price $3.48USD (Save 50%, Regular Price: $6.95USD) " Let me know if you need any other part of the debug. 🙂 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 21, 2020 Share Posted September 21, 2020 Hi Kevin, 6 minutes ago, Kevin K said: Ya I did talk to ModulesGarden about this and they want a minimum $250 to even look at it, not figuring "additional hours". *gulps* - they've kidnapped the cat too and are holding him for ransom. 🤑 7 minutes ago, Kevin K said: Hopefully this is the pricing array you were looking for: it is. 8 minutes ago, Kevin K said: What odd is the code seems to working fine in the array Discount Center is modifying those values because that content wouldn't be in that form by default. 9 minutes ago, Kevin K said: Let me know if you need any other part of the debug. ideally, it would be far simpler if there is a value in the array that shows the discounted price on its own - e.g $4.48USD (with or without currency). if there isn't, then you'll need to explode that value - so try changing... {assign var="temp" value=$currency.prefix|explode:$pricing.quarterly} {assign var="discounted_price" value='('|explode:$temp['1']|replace:" ":""} to... {assign var="temp" value=" "|explode:$pricing.quarterly} {assign var="discounted_price" value=$temp['3']|replace:$currency.prefix:""|replace:$currency.suffix:""} that *should* make $discounted_price a number that you can manipulate in your math equations... {math assign="original_price" equation="y/x" x=3 y=$pricing.rawpricing.quarterly format="%.2f"} {math assign="percent_save" equation="(x/y)*100" x=$discounted_price y=$original_price} {math assign="pay_now_price" equation="(x*y)/100" x=$pricing.rawpricing.quarterly y=$percent_save format="%.2f"} if your currency suffix includes a space before it, e.g " USD" and not "USD", then you might need to use $currency.code instead of $currency.suffix you can change $discounted_price[0] to $discounted_price in the html code too - though either should work I think. it's worth knowing that these days, you often don't need to use math equations in Smarty code as you can either do the calculations when needed or assigning them to variables directly... e.g the shorthand way of assigning those variables would be... {$discounted_price=$temp['3']|replace:$currency.prefix:""|replace:$currency.suffix:""} {$original_price=$pricing.rawpricing.quarterly / 3} {$percent_save=($discounted_price / $original_price) * 100} {$pay_now_price=(($pricing.rawpricing.quarterly * $percent_save) / 100)|string_format:"%.2f"} in any event, ideally you'd be performing these calculations in a hook, but as the above shows, they can be achieved using Smarty in the template if necessary. 1 Quote Link to comment Share on other sites More sharing options...
Kevin K Posted September 21, 2020 Author Share Posted September 21, 2020 (edited) @brian! OMG you are a life saver!!! This new code worked perfect. I am interested in how this could be done with a hook and would be willing to pay for it accordingly. If you are interested, feel free to send me a pm and we can discuss it further. 🙂 Opps, I think I might have spoke too soon, but we are very close. The only issue I am finding now is that the Pay Now price is off by a few cents starting from Quarterly to Triennially. For Example: The order summary has the correct pricing, but the Pay Now price seems to be a few cents too high. Any ideas on that? Edited September 21, 2020 by Kevin K 0 Quote Link to comment Share on other sites More sharing options...
Kevin K Posted September 23, 2020 Author Share Posted September 23, 2020 @brian! Did you see the edits I made to my last post? I appreciate any help you can offer to fix this one last issue with the Pay Now price being a few cents off and as the term increases, it amount it is off get a few cents higher each time. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 23, 2020 Share Posted September 23, 2020 3 hours ago, Kevin K said: Did you see the edits I made to my last post? only when I read this thread - I don't get email notifications of post edits. 3 hours ago, Kevin K said: I appreciate any help you can offer to fix this one last issue with the Pay Now price being a few cents off and as the term increases, it amount it is off get a few cents higher each time. that's a rounding error - if I work out the maths... original price = 101.70 / 6 = 16.95 discount price = 8.48 percent save = (8.48 / 101.70) * 100 = 50.029498525074 pay now = (101.70 * 50.029498525074) / 100 = 50.879999 = 50.88 so i'm assuming the module is rounding the percentage to the nearest whole number - so use either (but not both) of the lines below and that should return the percentage as 50 and make the calculations correct (or at least consistent with MG). {math assign="percent_save" equation="(x/y)*100" x=$discounted_price y=$original_price format="%d"} {$percent_save=(($discounted_price / $original_price) * 100)|string_format:"%d"} 0 Quote Link to comment Share on other sites More sharing options...
Kevin K Posted September 23, 2020 Author Share Posted September 23, 2020 Omg @brian! I could kiss you. 🤨 Ok maybe I wouldn't go quite that far..... Looks like everything is worked out now. I cannot thank you enough for all the help on this problem and the support you provide to the community. 😁 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.