| ||||||||||
![]() |
Shopping cart software Solutions for online shops and malls | |||||||||
![]() |
![]() |
|
X-Cart Home | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Credit card surcharge | ||||
![]() |
|
|
Thread Tools | Search this Thread |
#1
|
|||||||
|
|||||||
![]() Hi,
I'm attempting to add a credit card surcharge to my shopping cart. Starting off with editing cart_totals.tpl to have another row at the bottom showing the surcharge (in the same list as the sub total, discount, shipping etc). I've run into problems immediately, though. Starting simple, I just want to display the value of the surcharge on screen. I want to total the values of $cart.sub_total, $cart.discount and $cart.shipping_cost, and multiply it by 0.02 (to get the 2% surcharge value). I can't get smarty to perform the addition/multiplication by following this approach: {assign var="surcharge" value=($cart.sub_total+$cart.discount+$cart.shippi ng_cost)*0.02 } and then using {$surcharge} to display the value on screen. So I've tried it with php: {php}echo(($cart.sub_total+$cart.discount+$cart.sh ipping_cost)*0.02);{/php} And also {php}echo(({$cart.sub_total}+{$cart.discount}+{$ca rt.shipping_cost})*0.02);{/php} It's all a little bit confusing as I'm quite new to these smarty templates. If anyone has any ideas, I'd be very grateful. Many thanks, Richard |
|||||||
#2
|
|||||||
|
|||||||
![]() You actually need to use the math function within Smarty to calculate this value:
Code:
Note how you have to reference smarty variables within the equation (sub_total=$cart.sub_total). Notice the "assign" variable is 'surcharge'. So you can then display the surcharge in the template as {$surcharge}. The format=%.2f will output the surcharge as "1.22" or "1.00". NOTE: Be very careful with using the MATH function. It is very inefficient and will cause your pages to load slowly if used in multiple loops. Try to use PHP for that if this is intended for looping. Let me know if this works or not, since I haven't actually tested this.
__________________
Jeremy X-Cart Gold v3.4.14 [Linux/Apache] Heavily Modded |
|||||||
#3
|
|||||||
|
|||||||
![]() Thanks for the reply. It *almost* works - i.e. I can use the math function with known values, but cannot use smarty variables.
For example, {math equation="4+5" assign="surcharge" format="%.2f"} {$surcharge} produces '9.00' (correctly) but {math equation="sub_total+4" sub_total=$cart.sub_total assign="surcharge" format="%.2f"} {$surcharge} yields the following error: Warning: Smarty error: math: parameter Array not passed as argument I might go and have another look at the smarty web site to see if any similar problems have arisen... |
|||||||
#4
|
|||||||
|
|||||||
![]() If you can print out $cart.sub_total then you should be able to use it in your math equation.
If you absolutely cannot get the math function to work, the PHP code should work just as well.
__________________
Jeremy X-Cart Gold v3.4.14 [Linux/Apache] Heavily Modded |
|||||||
#5
|
|||||||||
|
|||||||||
![]() Quote:
You better be very careful in deciding to do this. Please read your Agreements with Mastercard and Visa. There are only a few cases where charging a surcharge for Mastercard are acceptable. Please contact your Credit Card Processing Gateway and discuss this prior to doing this. There are stiff penalties associated if you are not in compliance. |
|||||||||
#6
|
|||||||
|
|||||||
![]() Quote:
Excellent point. Our site is UK based, and my boss assures me that it's OK over here, so long as you state clearly that there is a surcharge. Thanks for the warning ![]() |
|||||||
#7
|
|||||||
|
|||||||
![]() Code:
You have to assign the variables for the math equation for smarty to use them. [/code]
__________________
Mod King |
|||||||
#8
|
|||||||
|
|||||||
![]() Quote:
Unfortunately, using the snippet below (which I agree, looks like it should work fine) doesn't. Quote:
And it gives me this error message Quote:
A real head-scratcher... ![]() |
|||||||
#9
|
|||||||
|
|||||||
![]() hmm thats strange, you could make it calculate the surcharge in php, where it calculates the sub_total etc and just pass that to smarty
as like $cart.surcharge
__________________
Mod King |
|||||||
#10
|
|||||||
|
|||||||
![]() All sorted now! smarty wasn't liking the underscores in the equation. Working code is:
Quote:
Thanks for all the help, everyone! ![]() ![]() ![]() |
|||||||
|
|||
X-Cart forums © 2001-2020
|