PDA

View Full Version : Cart Contents


Pneumatus
15-Aug-2003, 04:00 PM
I am currently using the follwing bit of script for showing the card contents:


if (getCartItem(3) < 1)
{
document.write("Your cart is empty");
}

else

{
document.write ("NETQUOTEVAR:CARTCOOKIEITEMS&nbsp;" + getCartItem(3)+"&nbsp;<BR>");
document.write("NETQUOTEVAR:CARTCOOKIEVALUE&nbsp;" + getCartItem(1));
}


This works fine except that the value displayed from getCartItem(1) returns a price including VAT. Is there any way of accuratly getting an ex vat total displayed there?

Cheers

NormanRouxel
15-Aug-2003, 05:55 PM
If ALL your prices include VAT then you could always try replacing:-

getCartItem(1)

with:-

'£' + (getCartItem(1).substring(1) / 1.175)

This works if you only display a single price (not 2 currencies).

However you might need some additional code to round it to 2 decimal digits.

Norman.

Pneumatus
15-Aug-2003, 09:00 PM
I guessed that would be the case - I was going to do something similar if there was no other option anyway.

Cheers

NormanRouxel
16-Aug-2003, 06:38 AM
Also (to save you the time trying it out) I tried calling getcartitem(0) through getcartitem(10) just in case there were some additional values available with no luck.

Norman

Pneumatus
19-Aug-2003, 12:50 PM
Well, it transpires that the problem is a little more difficult the 1st thought.

getCartItem(1) does not return a standard string like "£123,45.67", it actually returns something along the lines of:

"&amp;#163;123&amp;#44;45&amp;#46;67"

Which of course we all knew would happen(!). So in order to do any sort of mathematical calculation we need to strip out the html characters and turn it into a float.

To strip the characters, perform the calculation then return a nice formatted string complete with £'s and 000's commas, I wrote the following javascript function in a .js file:

function formatCurrency(num) {
num = num.toString().replace(/\£|\,/g,'');
num = num.replace("&amp;#163;",""); // Strip £ sign
num = num.replace("&amp;#44;",""); // Strip commans
num = num.replace("&amp;#46;","."); // Replaces html perios with true periods
num = num / 1.175;
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = "0" + pence;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '£' + num + '.' + pence);
}

Then, to get a net price displayed in the quick cart, rather than using:

document.write("NETQUOTEVAR:CARTCOOKIEVALUE&amp;nbsp;" + getCartItem(1));

I can use

document.write("NETQUOTEVAR:CARTCOOKIEVALUE&amp;nbsp;" + formatCurrency(getCartItem(1)));

In any page that include the .js file with the fuction in!

Hopefully this might help someone and save them the hours it took me to work out.

cdicken
19-Aug-2003, 02:01 PM
Thanks Matt - a useful function.