Add this code to your page to display total number and price of items in the shopping cart:
Code:
<script language="javascript">
if ( Cart_is_empty()) {
document.write('Your cart is empty.');
} else {
document.write('In your cart:<p>');
Print_total_products(true);
document.write(', ');
Print_total(true);
document.write('<p>Applicable shipping and taxes extra.<p>');
document.write('<a href="managecart.html">View Cart</a>');
}
</script>
Add to very bottom of nopcart.js:
Code:
//---------------------------------------------------------------------||
// FUNCTION: Print_total ||
// PARAMETERS: true/false if you want MonetarySymbol added to string ||
// RETURNS: Total cost currently racked up by shopper ||
// PURPOSE: Aesthetics ||
//---------------------------------------------------------------------||
function Print_total(bSymbol) {
var strOutput = ""; //String to be written to page
var strTotal = ""; //Total cost formatted as money
var fTotal = 0;
var iNumberOrdered = 0; //Number of products ordered
iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;
for ( i = 1; i <= iNumberOrdered; i++ ) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);
Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);
fields = new Array;
fields[0] = database.substring( 0, Token0 ); // Product ID
fields[1] = database.substring( Token0+1, Token1 ); // Quantity
fields[2] = database.substring( Token1+1, Token2 ); // Price
fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description
fields[4] = database.substring( Token3+1, Token4 ); // Weight
fields[5] = database.substring( Token4+1, database.length ); //Additional Information
fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) );
}
strTotal = moneyFormat(fTotal);
strOutput+=strTotal;
if ( bSymbol )
strOutput = MonetarySymbol + strOutput
document.write(strOutput);
}
//---------------------------------------------------------------------||
// FUNCTION: Print_total_products ||
// PARAMETERS: true/false if you want "item(s)" added to string ||
// RETURNS: Total cost currently racked up by shopper ||
// PURPOSE: Aesthetics ||
//---------------------------------------------------------------------||
function Print_total_products(bVerbose) {
var strOutput = ""; //String to be written to page
var fTotal = 0;
var iNumberOrdered = 0; //Number of products ordered
iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;
for ( i = 1; i <= iNumberOrdered; i++ ) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);
Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);
fields = new Array;
fields[0] = database.substring( 0, Token0 ); // Product ID
fields[1] = database.substring( Token0+1, Token1 ); // Quantity
fields[2] = database.substring( Token1+1, Token2 ); // Price
fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description
fields[4] = database.substring( Token3+1, Token4 ); // Weight
fields[5] = database.substring( Token4+1, database.length ); //Additional Information
fTotal += (parseInt(fields[1]));
}
strOutput+=fTotal;
if ( bVerbose ) {
if (fTotal == 1) {
strOutput+=" item"
}
else {
strOutput+=" items"
}
}
document.write(strOutput);
}
//---------------------------------------------------------------------||
// FUNCTION: Cart_is_empty ||
// PARAMETERS: none ||
// RETURNS: Total true if cart is empty, false otherwise ||
// PURPOSE: Aesthetics ||
//---------------------------------------------------------------------||
function Cart_is_empty( ) {
iNumInCart = GetCookie("NumberOrdered");
if ( iNumInCart == null ) iNumInCart = 0;
if ( iNumInCart == 0 ) return true;
return false;
}
Note that the text in the "code" box above may appear to wrap at the end of long lines. If you copy and paste the code from the box above into your nopcart.js file the lines should be just fine.
I've used this script and it works fine....there are a few questions....
I modified a copy of the nopcart.js to reflect these changes and renamed the js file as nopcart2.js I have a seperate directory on my web site called /cart and in that directory have added the nopcart.js file and the two files managecart.html and checkout.html on each page that I sell a product I have added the two scripts in the head file nopcart.js and language_en.js. In my viewcart form I simply point the getfrom cart this.form to /cart/nopcart.js file and that seems to work fine. Now after the modifications to the .js script I added nopcart2.js file to the /cart directory and changed the script on each page in the header to
<script="nopcart2.js>. this seems ok and works fine......BUT when I add this to the managecart.html file in /cart directory I get "script errors on this page" when I revert back to nopcart.js it works fine and displays the cart. Now my question is......do I need to have nopcart2.js AND nopcart.js in the /cart directory for this mod to work???? If not what am I doing wrong?
Thanks for all the help... I find this forum very helpful.
Charlie aks (kc giftware)
