Foros del Web » Programando para Internet » Javascript »

shopping cart

Estas en el tema de shopping cart en el foro de Javascript en Foros del Web. Carrito de compras.- Espero que a alguien le sea util este script, lo posteo en varias partes porque no entra.- Si alguien quiere el zip ...
  #1 (permalink)  
Antiguo 03/12/2002, 18:21
Avatar de sordo77  
Fecha de Ingreso: noviembre-2002
Ubicación: Rosario
Mensajes: 70
Antigüedad: 21 años, 5 meses
Puntos: 0
Mensaje shopping cart

Carrito de compras.-
Espero que a alguien le sea util este script, lo posteo en varias partes porque no entra.-
Si alguien quiere el zip con los archivos mandeme un mail y listo =)
a [email protected]

This script includes 3 parts (thescript, addtocartform and managecartform). The script should be placed in all pages as this contains the cookie functions etc, the addtocartform should be on your item page where your visitors can add that item to their cart, the managecartform should be placed on managecart page.

For support and more details visit http://www.nopdesign.com/freecart/index.html

******
Este script incluye 3 partes (thescript, addtocartfrom y managecartfrom). Thescript debe estar puesto en todas las páginas que contengan las funciones de cookies etc; el addtocartfrom debe estar sobre la página de item donde tus visitantes puedan agregar estos items al carrito; y el managecartfrom debe estar puesto en la página de administración del carrito.

Para soporte y mas detalles visitar
http://www.nopdesign.com/freecart/index.html
***** Traducido por sordo77 (perdón por los errores)

Última edición por sordo77; 03/12/2002 a las 18:29
  #2 (permalink)  
Antiguo 03/12/2002, 18:24
Avatar de sordo77  
Fecha de Ingreso: noviembre-2002
Ubicación: Rosario
Mensajes: 70
Antigüedad: 21 años, 5 meses
Puntos: 0
----- thescript -----
<SCRIPT LANGUAGE="JavaScript">

//================================================== ===================||
// NOP Design JavaScript Shopping Cart ||
// ||
// For more information on SmartSystems, or how NOPDesign can help you ||
// Please visit us on the WWW at http://www.nopdesign.com ||
// ||
// Javascript portions of this shopping cart software are available as ||
// freeware from NOP Design under the GPL. You must keep this comment ||
// unchanged in your code. For more information contact ||
// [email protected] ||
// ||
// JavaScript Shop Module, V.3.0.2 ||
//================================================== ===================||


//---------------------------------------------------------------------||
// FUNCTION: checkForm ||
// PARAMETERS: Form object, and true/false if we should check for ||
// a credit card number. You shouldn't accept credit cards||
// via email. Only use that when you are connected to a ||
// secure server. ||
// RETURNS: boolean (True form is correct, False form is in error) ||
// PURPOSE: To check form elements ||
//---------------------------------------------------------------------||
function checkForm(thisForm, checkForCreditCard) {
bFormError = false; //Boolean variable to store form state
bIsValidCard = false; //Boolean variable to store CC state
strErrorList = ""; //String list of missing/errorsum fields

if( thisForm.FIRST.value=='' ) {bFormError = true; strErrorList += "First Name, ";}
if( thisForm.LAST.value=='' ) {bFormError = true; strErrorList += "Last Name, ";}
if( thisForm.ADDRESS.value=='') {bFormError = true; strErrorList += "Address, ";}
if( thisForm.CITY.value=='' ) {bFormError = true; strErrorList += "City, ";}
if( thisForm.STATE.value=='' ) {bFormError = true; strErrorList += "State, ";}
if( thisForm.ZIP.value=='' ) {bFormError = true; strErrorList += "Zip, ";}
if( thisForm.PHONE.value=='' ) {bFormError = true; strErrorList += "Phone, ";}
if( thisForm.ACCOUNT.value=='') {bFormError = true; strErrorList += "Credit Card Number, ";}
if( thisForm.MONTH.value=='' ) {bFormError = true; strErrorList += "Month, ";}
if( thisForm.YEAR.value=='' ) {bFormError = true; strErrorList += "Year ";}
if( bFormError == true ) {
alert("I'm sorry, but you had one or more missing or invalid entries.\n"
+"Please check the following fields: \n\n"
+strErrorList
+"\n\n");
return false;
}

//Check for valid Visa
if (((thisForm.ACCOUNT.value.length == 16) || (thisForm.ACCOUNT.value.length == 13)) &&
(thisForm.ACCOUNT.value.substring(0,1) == 4))
bIsValidCard = true;

//Check for valid MasterCard
firstdig = thisForm.ACCOUNT.value.substring(0,1);
seconddig = thisForm.ACCOUNT.value.substring(1,2);
if ((thisForm.ACCOUNT.value.length == 16) &&
(firstdig == 5) && ((seconddig >= 1) &&
(seconddig <= 5))
)
bIsValidCard = true;

if (bIsValidCard == false){
alert("I'm sorry, but you need to enter a valid credit card number.\n");
return false;
}

return needComments();
} //END function checkForm


//---------------------------------------------------------------------||
// FUNCTION: CKquantity ||
// PARAMETERS: Quantity to ||
// RETURNS: Quantity as a number, and possible alert ||
// PURPOSE: Make sure quantity is represented as a number ||
//---------------------------------------------------------------------||
function CKquantity(checkString) {

strNewQuantity = ""; // String Adjusted Item Quantity
count = 0; // Generic Loop Counter

for (i = 0; i < checkString.length; i++) {
ch = checkString.substring(i, i+1);

if ((ch >= "0" && ch <= "9") || (ch == '.')) {
strNewQuantity += ch;
}
}

if (strNewQuantity.length < 1)
strNewQuantity = "1";

return strNewQuantity;
}


//---------------------------------------------------------------------||
// FUNCTION: AddToCart ||
// PARAMETERS: Form Object ||
// RETURNS: Cookie to user's browser, with prompt ||
// PURPOSE: Adds a product to the user's shopping cart ||
//---------------------------------------------------------------------||
function AddToCart(thisForm) {

iNumberOrdered = 0; //Integer number of products already ordered

iNumberOrdered = GetCookie("NumberOrdered");
iNumberOrdered++;

if ( iNumberOrdered > 12 )
alert("I'm Sorry, your cart is full, please proceed to checkout.");
else {
dbUpdatedOrder = thisForm.QUANTITY.value + "|"
+ thisForm.PRICE.value + "|"
+ thisForm.ID_NUM.value + "|"
+ thisForm.NAME.value;

NewOrder = "Order." + iNumberOrdered;
SetCookie (NewOrder, dbUpdatedOrder, null, "/");
SetCookie ("NumberOrdered", iNumberOrdered, null, "/");

notice = thisForm.QUANTITY.value + " "
+ thisForm.NAME.value
+ " added to your shopping cart.";

alert(notice);
}
}


//---------------------------------------------------------------------||
// FUNCTION: getCookieVal ||
// PARAMETERS: offset ||
// RETURNS: URL unescaped Cookie Value ||
// PURPOSE: Get a specific value from a cookie ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}


//---------------------------------------------------------------------||
// FUNCTION: FixCookieDate ||
// PARAMETERS: date ||
// RETURNS: date ||
// PURPOSE: Fixes cookie date, stores back in date ||
//---------------------------------------------------------------------||
function FixCookieDate (date) {
var base = new Date(0);
var skew = base.getTime();
date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------||
// FUNCTION: GetCookie ||
// PARAMETERS: Name ||
// RETURNS: Value in Cookie ||
// PURPOSE: Retrieves cookie from users browser ||
//---------------------------------------------------------------------||
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}

return null;
}


//---------------------------------------------------------------------||
// FUNCTION: SetCookie ||
// PARAMETERS: name, value, expiration date, path, domain, security ||
// RETURNS: Null ||
// PURPOSE: Stores a cookie in the users browser ||
//---------------------------------------------------------------------||
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

//----- CONTINUA....
  #3 (permalink)  
Antiguo 03/12/2002, 18:25
Avatar de sordo77  
Fecha de Ingreso: noviembre-2002
Ubicación: Rosario
Mensajes: 70
Antigüedad: 21 años, 5 meses
Puntos: 0
//----- CONTINUACION....

//---------------------------------------------------------------------||
// FUNCTION: DeleteCookie ||
// PARAMETERS: Cookie name, path, domain ||
// RETURNS: null ||
// PURPOSE: Removes a cookie from users browser. ||
//---------------------------------------------------------------------||
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}


//---------------------------------------------------------------------||
// FUNCTION: MoneyFormat ||
// PARAMETERS: Number to be formatted ||
// RETURNS: Formatted Number ||
// PURPOSE: Reformats Dollar Amount to #.## format ||
//---------------------------------------------------------------------||
function moneyFormat(input) {
var dollars = Math.floor(input)
var tmp = new String(input)
for (var decimalAt = 0; decimalAt < tmp.length; decimalAt++) {
if (tmp.charAt(decimalAt)==".")
break;
}

var cents = "" + Math.round(input * 100)
cents = cents.substring(cents.length-2, cents.length)
dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

return dollars + "." + cents
}


//---------------------------------------------------------------------||
// FUNCTION: RemoveFromCart ||
// PARAMETERS: Order Number to Remove ||
// RETURNS: Null ||
// PURPOSE: Removes an item from a users shopping cart ||
//---------------------------------------------------------------------||
function RemoveFromCart(RemOrder) {
if (confirm("Click 'Ok' to remove this product from your shopping cart.")) {
NumberOrdered = GetCookie("NumberOrdered");
for(i=RemOrder; i < NumberOrdered; i++) {
NewOrder1 = "Order." + (i+1);
NewOrder2 = "Order." + (i);
database = GetCookie(NewOrder1);
SetCookie (NewOrder2, database, null, "/");
}
NewOrder = "Order." + NumberOrdered;
SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");
DeleteCookie(NewOrder, "/");
location.href=location.href;
}
}


//---------------------------------------------------------------------||
// FUNCTION: GetFromCart ||
// PARAMETERS: Null ||
// RETURNS: Product Table Written to Document ||
// PURPOSE: Draws current cart product table on HTML page ||
//---------------------------------------------------------------------||
function GetFromCart() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 );
fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

whattowrite += "<tr><td>" + fields[2] + "</td><td><font size=-1>"
+ fields[3] + "</font></td><td>$" + fields[1]
+ "</td><td><input type=text size=2 name=\"QUANTITY_"+ i +"\" value=\""
+ fields[0] + "\"></td>"
+ "<td><input type=button value=\" Remove \" onClick=\"RemoveFromCart("+i+")\"></td>"
+ "<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">"
+ "<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">"
+ "<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">";
}

document.write(whattowrite);
document.write("</td></tr><tr><td colspan=2><b>SUBTOTAL</b></td><td>$");
document.write(TOTotal);
document.write("</td><td></td>");
}


//---------------------------------------------------------------------||
// FUNCTION: WriteToForm ||
// PARAMETERS: Null ||
// RETURNS: Product hidden fields Written to Document ||
// PURPOSE: Draws current cart product hidden fields on HTML form ||
//---------------------------------------------------------------------||
function WriteToForm() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 );
fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

document.write("<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">");
document.write("<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">");
document.write("<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">");
document.write("<input type=hidden name=\"QUANTITY_"+ i +"\" value=\"" + fields[0] + "\">");
}
}


//================================================== ===================||
// END NOP Design SmartPost Shopping Cart ||
// V.3.0.1 ||
//================================================== ===================||

</SCRIPT>

<NOSCRIPT>
Whoops, we detected that your browser does not have JavaScript, or it is disabled. Our product catalog
requires that you have JavaScript enabled to order products. <a href="http://www.netscape.com">Netscape</a>
and <a href="http://www.microsoft.com/ie">Microsoft</a> offer free browsers which support JavaScript.
If you are using a JavaScript compliant browser and still have problems, make sure you have JavaScript enabled in
your browser's preferences.
</NOSCRIPT>
----- FIN thescript -----
  #4 (permalink)  
Antiguo 03/12/2002, 18:26
Avatar de sordo77  
Fecha de Ingreso: noviembre-2002
Ubicación: Rosario
Mensajes: 70
Antigüedad: 21 años, 5 meses
Puntos: 0
----- addtocartfrom -----
<!--Shopping Cart Product Begin-->
<P><FORM NAME=order>Quantity: <input type=text size=2 maxlength=3 name=QUANTITY onChange='this.value=CKquantity(this.value)' value="1">
<input type=button value=' Add to Cart ' onClick='AddToCart(this.form)'>
<input type=hidden name=PRICE value="19.95">
<input type=hidden name=NAME value="My Sample Product">
<input type=hidden name=ID_NUM value="Sample ID 001">
<a href="managecart.html">Check out now</a>
</FORM>
<!--Shopping Cart Product End -->
----- FIN addtocartfrom -----

----- managecartfrom -----
<!--Shopping Cart Checkout Begin-->
The items listed below are currently in your shopping cart:
<p>
<FORM ACTION="mailto:[email protected]" ENCTYPE="text/plain" onSubmit="return checkForm(this);" method=post>
<table border=1 cellspacing=0>
<tr>
<td bgcolor=#cccccc><b> Product Code </b></td>
<td bgcolor=#cccccc><b> Name </b></td>
<td bgcolor=#cccccc><b> Price </b></td>
<td bgcolor=#cccccc><b> Quantity </b></td>
<td bgcolor=#cccccc><b> Remove Item From Cart </b></td>
</tr>
<script>
GetFromCart();
</script>
</table>

<!-- Insert any additional fields you wish to collect (ie. Name, address -->
<!-- phone, email, etc. here. -->

</FONT><br><br><table border=1 width="500"><tr><td>First Name: <input type=text size=18 name="FIRST"> Last Name: <input type=text size=18 name="LAST">
Address: <input type=text size=49 name="ADDRESS">
<input type=text size=49 name="ADDRESS2"><br>
City: <input type=text size=26 name="CITY"> State: <input type=text size=2 name="STATE"> Zip: <input type=text size=5 name="ZIP"><br>
Phone: <input type=text size=26 name="PHONE"><br>
Email: <input type=text size=26 name="EMAIL"><br>

Please Enter your Method of Payment: <select name="PAYMENT">
<option value="VISA">Visa
<option value="MASTERCARD">Mastercard
</select><br>
Account Number: <input type=text size=16 name="ACCOUNT">
Expires On: <input type=text size=2 name="MONTH"> / <input type=text size=4 name="YEAR"></td></tr></table><br><INPUT type=SUBMIT value=" Complete Check Out ">
</FORM>
<!--Shopping Cart Checkout End -->
----- FIN managecartfrom -----

---[FIN Carrito de compras ]---------------------------------------
  #5 (permalink)  
Antiguo 03/12/2002, 19:41
Avatar de Kaopectate
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: Curaçao (Antillas Holandesas)
Mensajes: 3.179
Antigüedad: 22 años, 4 meses
Puntos: 38
Hola Sordo77.

¿Por que no colocas este mensaje en los FAQs? Podría ser un aporte importante.

Saludos.
  #6 (permalink)  
Antiguo 04/12/2002, 12:34
Avatar de sordo77  
Fecha de Ingreso: noviembre-2002
Ubicación: Rosario
Mensajes: 70
Antigüedad: 21 años, 5 meses
Puntos: 0
Es demasiado largo el script me parece como para ponerlo ahi... voy a ver si encuentro la URL de donde saque este script.

Cita:
Mensaje Original por Kaopectate
Hola Sordo77.

¿Por que no colocas este mensaje en los FAQs? Podría ser un aporte importante.

Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:32.