X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Credit card surcharge (https://forum.x-cart.com/showthread.php?t=7609)

richard_o 05-17-2004 04:12 AM

Credit card surcharge
 
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

jeremye 05-17-2004 04:50 AM

You actually need to use the math function within Smarty to calculate this value:

Code:

{math equation="(sub_total+discount+shipping_cost)*0.02"
      sub_total=$cart.sub_total
      discount=$cart.discount
      shipping_cost=$cart.shipping_cost
      assign="surcharge"
      format="%.2f"}


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.

richard_o 05-17-2004 05:23 AM

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...

jeremye 05-17-2004 05:29 AM

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.

TelaFirma 05-17-2004 05:41 AM

Re: Credit card surcharge
 
Quote:

Originally Posted by richard_o
I'm attempting to add a credit card surcharge to my shopping cart.


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.

richard_o 05-17-2004 05:51 AM

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.

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 :-)[/quote]

Mod King 05-17-2004 05:57 AM

Code:

{math equation="(sub_total+discount+shipping_cost)*0.02"
      sub_total=$cart.sub_total
      discount=$cart.discount
      shipping_cost=$cart.shipping_cost
      assign="surcharge"
      format="%.2f"}

You need it to be of that form which was stated above.
You have to assign the variables for the math equation for smarty to use them.
[/code]

richard_o 05-17-2004 06:17 AM

Quote:

You need it to be of that form which was stated above.
You have to assign the variables for the math equation for smarty to use them.


Unfortunately, using the snippet below (which I agree, looks like it should work fine) doesn't.

Quote:

{math equation="(sub_total+discount+shipping_cost)*0.02"
sub_total=$cart.sub_total
discount=$cart.discount
shipping_cost=$cart.shipping_cost
assign="surcharge"
format="%.2f"}


And it gives me this error message

Quote:

Warning: Smarty error: math: parameter sub not passed as argument

A real head-scratcher... :?

Mod King 05-17-2004 06:34 AM

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

richard_o 05-17-2004 06:41 AM

All sorted now! smarty wasn't liking the underscores in the equation. Working code is:

Quote:

{math equation="(subtotal+discount+shippingcost)*0.02"
subtotal=$cart.sub_total
discount=$cart.discount
shippingcost=$cart.shipping_cost
assign="surcharge"
format="%.2f"}

Thanks for all the help, everyone! :D :D :D


All times are GMT -8. The time now is 10:49 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.