How to... > Shopping cart
This is the version for WebsiteX5 version 7
Check this page for the version for WebsiteX5 Evolution 8.
With this script you can show information about the contents of the shopping cart on, for example the home page of your website. It shows the number of products in the cart, the price, the percentage and the amount of VAT and the total amount of the items in the cart. It does not show the shipping costs because that can only be calculated when a customer places the order and chooses a shipping method.
It could look something like:
To use the script add a HTML-object anywhere on the page. Edit this HTML-object and copy/paste the following script in it.
<script type="text/javascript" src="res/x5cart.js"></script>
<script type="text/javascript" src="res/x5products.js"></script>
<script type="text/javascript">
//
// Show shoppingcart info on you home page
// Version for WebsiteX5 Evolution 8
// 2010 - Rbbrt
//
// Load the WX5 shoppingcartsystem
imCLoad("");
// Constant values. Change to suit your language
// or pull appropriate values from Language.ini
var strProducts="products";
var strSubtotal="Subtotal";
var strVAT = "VAT";
var strTotal = imLocale["TotalVAT"];
var strEmpty = imLocale["Err_EmptyCart"];
var strGoToCart = "Go to your Shoppingcart";
var iNumberOfProducts=0; // for counting the number of products
var fTotal = 0; // for calculating the total of the order
var fVAT = 0;
var fTotalPlusVat = 0;
var sBuf ="<div id='myDiv'>";
// Load the shopingcartfunction.
var sCart = imGetCookie("imOrder");
var aCart = "";
// set the results in a table...
sBuf +="<table id='myTable'>";
// split the contents of the shoppingcart into an array.
if(sCart != null && sCart != "" ) {
aCart = sCart.split("|");
}
else {
aCart = new Array();
}
// If the cart is empty show 'EmptyCart'.
// This is defined in the languagedefinition.
// Otherwise count the number of ordered articles
// and calculate the total orderprice (ex shipment and VAT.
if(aCart.length == 0) {
sBuf += strEmpty;
}
else {
for ( var i = 0; i < aCart.length; i++) {
aCartProd = aCart[i].split(":");
aProd = imCGetProduct(aCartProd[0],aCartProd[1]);
iNumberOfProducts += Number(aCartProd[3]);
fTotal += aProd[4]*aCartProd[3];
}
// Caluclate the amount of VAT
fVAT = fTotal * Number( imVAT/100 );
// calculate the total including VAT
fTotalPlusVat = fTotal + fVAT;
// Create output:
// remove the lines you don't need in your output by
// placing two forward slashed before them!
// First report the number of products and the amount ex VAT.
sBuf +="<tr><td style='text-align: left;'>" + iNumberOfProducts + " " +
strProducts + "</td>";
sBuf +="<td align='right'>" + imCFormat(fTotal,false) + "</td></tr>";
// Next report the amount of VAT
sBuf += "<tr><td style='text-align:left;'>" + strVAT + " (" + imVAT + "%)" + "</td>";
sBuf += "<td style='text-align:right;'>" + imCFormat(fVAT,false) + "</td></tr>";
// Calculate the total amount and show this.
sBuf +="<tr><td style='text-align: left;'>" + strTotal + "</td>";
sBuf +="<td align='right'><b>" + imCFormat(fTotalPlusVat,false) + "</b></td></tr>";
}
sBuf += "</table>";
sBuf +=" <p> </p>";
// add a link to go to the shoppingcart...
sBuf +="<a title='' href='imcart.html'>" + strGoToCart + "</a>";
// Close the div...
sBuf+="</div>";
// and output the buffer...
document.write(sBuf);
</script>
Change the language labels (the ones in green) in the texts which are appropriate for your language and click on OK (ignore the error. The script works fine!)
You can then format the block using the WX5 format painter or add extra markup-codes in the text.
There's also a link to quickly go to the shopping cart...