📄 cart.asp
字号:
var indexQuantity = this.GetIndexOfColName(this.QUANTITY);
var iDest = 0;
for (var iRow=0; iRow<this.GetItemCount(); iRow++) {
if (this.SC[indexQuantity][iRow] != 0) {
for (iCol=0; iCol<this.numCols; iCol++) {
tmpSC[iCol][iDest] = this.SC[iCol][iRow];
}
iDest++;
}
}
this.SC = tmpSC;
}
function Update(formElementName){
// Get new quantity values from Request object.
// Assume they are all named the same, so you will get
// an array. The array length should be the same as the number
// of line items and in the same order.
this.UpdateQuantities(formElementName);
this.DeleteItemsWithNoQuantity();
this.UpdateTotals();
this.persist();
}
function BuildInsertColumnList(orderIDCol, mappings){
var colList = orderIDCol;
for (var i = 0; i < mappings.length; i++) {
if (mappings[i] != ""){
colList += ", " + mappings[i];
}
}
colList = "(" + colList + ")";
return colList;
}
function BuildInsertValueList(orderIDColType, orderIDVal, destCols, destColTypes, row){
var values = "";
if (orderIDColType == "num") {
values += orderIDVal;
} else {
values += "'" + orderIDVal.toString().replace(/'/g, "''") + "'";
}
for (var iCol=0; iCol<this.numCols; iCol++){
if (destCols[iCol] != "") {
if (destColTypes[iCol] == "num") {
assert(this.SC[iCol][row] != "", "SaveToDatabase: A numeric value is missing in the SQL statement in column " + this.colNames[iCol]);
values += ", " + this.SC[iCol][row];
} else {
values += ", '" + (this.SC[iCol][row]).toString().replace(/'/g, "''") + "'";
}
}
}
values = "(" + values + ")";
return values;
}
function SaveToDatabase(adoConn, dbTable, orderIDCol, orderIDColType, orderIDVal, destCols, destColTypes){
// we are going to build SQL INSERT statements and
// throw it at the connection / table
// Similar to existing UD insert to database behavior
var ERR_MAPPINGS_LENGTH = "Array length must match the number of cart columns<BR>";
var ERR_TRANS = "An error occured when inserting cart items in the database. The transaction was rolled back<BR>";
destCols = UC_VbToJsArray(destCols);
destColTypes = UC_VbToJsArray(destColTypes);
assert (destCols.length == this.numCols, "SaveToDatabase: " + "destCols - " + ERR_MAPPINGS_LENGTH);
assert (destColTypes.length == this.numCols, "SaveToDatabase: " + "destColTypes - " + ERR_MAPPINGS_LENGTH);
var insertColList = this.BuildInsertColumnList(orderIDCol, destCols);
if (insertColList != "") { //proceed only if we have a column list
var insertClause = "INSERT INTO " + dbTable + " " + insertColList + " VALUES ";
var recs;
adoConn.BeginTrans();
for (var iRow=0; iRow<this.GetItemCount(); iRow++){
var valList = this.BuildInsertValueList(orderIDColType, orderIDVal, destCols, destColTypes, iRow);
var sql = insertClause + valList;
adoConn.Execute(sql, recs, 1 /*adCmdText*/);
}
if (adoConn.Errors.Count == 0){
adoConn.CommitTrans();
this.Destroy(); // All items saved to database, we can trash the cart
} else {
adoConn.RollbackTrans();
//assert(false, "SaveToDatabase: " + ERR_TRANS); Don't assert here - let ASP display the database error.
}
}
}
function GetItemCount(){
return this.SC[0].length
}
function GetColumnTotal(colName){
// Generic column Total function
var colTotal = 0.0;
index = this.GetIndexOfColName(colName);
for (var i=0; i<this.SC[index].length; i++)
colTotal += parseFloat(this.SC[index][i]);
return colTotal
}
function DeleteLineItem(row){
assert(!isNaN(row), "Failure in call to DeleteLineItem - row is not a number");
assert(row>=0 && row <this.GetItemCount(), "failure in call to DeleteLineItem (internal error 121)");
var tmpSC= new Array(this.numCols);
var iDest = 0;
for (var iCol=0; iCol<this.numCols; iCol++) tmpSC[iCol] = new Array();
for (var iRow=0; iRow<this.GetItemCount(); iRow++) {
if (iRow != row) {
for (iCol=0; iCol<this.numCols; iCol++) {
tmpSC[iCol][iDest] = this.SC[iCol][iRow];
}
iDest++;
}
}
this.SC = tmpSC;
this.persist();
}
function UCGetColNamesSerial(colDelim) {
var serialCols = "";
for (var iCol=0; iCol<this.numCols; iCol++) {
if (iCol != 0) serialCols += colDelim;
serialCols += this.colNames[iCol];
}
return serialCols;
}
function UCGetContentsSerial(colDelim, rowDelim) {
var serialCart = "";
for (var iRow=0; iRow<this.GetItemCount(); iRow++) {
if (iRow != 0) serialCart += rowDelim
for (var iCol=0; iCol<this.numCols; iCol++) {
if (iCol != 0) serialCart += colDelim;
serialCart += this.SC[iCol][iRow];
}
}
return serialCart;
}
function UCSetContentsSerial(serialCart, colDelim, rowDelim) {
var Rows = String(serialCart).split(rowDelim)
for (iRow = 0; iRow < Rows.length; iRow++) {
if (Rows[iRow] != "undefined" && Rows[iRow] != "") {
Cols = Rows[iRow].split(colDelim)
iCol = 0
for (iCol = 0; iCol<Cols.length; iCol++) {
this.SC[iCol][iRow] = Cols[iCol]
}
}
}
this.persist();
}
function SetCookie(){
var cookieName = this.GetCookieName()
var cookieStr = this.GetContentsSerial(this.cookieColDel, this.cookieRowDel)
var cookieExp = GetCookieExp(this.cookieLifetime)
Response.Cookies(cookieName) = cookieStr
Response.Cookies(cookieName).expires = cookieExp
}
function GetCookieName(){
var server = Request.ServerVariables("SERVER_NAME");
return server + this.Name;
}
function UCDestroyCookie(){
cookieName = this.GetCookieName();
Response.Cookies(cookieName) = ""
Response.Cookies(cookieName).expires = "1/1/90"
}
function PopulateFromCookie(cookieStr){
this.SetContentsSerial(cookieStr, this.cookieColDel, this.cookieRowDel)
}
// ***************** debug code ********************
function assert(bool, msg) {
if (!bool) {
Response.Write("<BR><BR>An error occured in the UltraDev shopping cart:<BR>" + msg + "<BR>");
//Response.End();
}
}
function AssertCartValid(colNames, msg) {
// go through all cart data structures and insure consistency.
// For example all column arrays should be the same length.
// this function should be called often, especially just after
// makeing changes to the data structures (adding, deleting, etc.)
// also verify we always have the required columns:
// ProductID, Quantity, Price, Total
// the input arg is some I add as I code this package like
// "Prior to return from AddToCart"
//
var ERR_COOKIE_SETTINGS = "Cookie settings on this page are inconsistent with those stored in the session cart<BR>";
var ERR_BAD_NAME = "Cart name defined on this page is inconsistent with the cart name stored in the session<BR>";
var ERR_COLUMN_COUNT = "The number of cart columns defined on this page is inconsistent with the cart stored in the session<BR>";
var ERR_REQUIRED_COLUMNS = "Too few columns; minimum number of columns is 4<BR>";
var ERR_REQUIRED_COLUMN_NAME = "Required Column is missing or at the wrong offset: ";
var ERR_COLUMN_NAMES = "Cart column names defined on this page are inconsistent with the cart stored in the session";
var ERR_INCONSISTENT_ARRAY_LENGTH = "Length of the arrays passed to cart constructor are inconsistent<BR>"
var errMsg = "";
var sessCart = Session(this.Name);
if (sessCart != null) { // Validate inputs against session cart if it exists
if (sessCart.Name != this.Name) errMsg += ERR_BAD_NAME;
if (this.numCols < 4) errMsg += ERR_REQUIRED_COLUMNS;
if (sessCart.numCols != this.numCols) errMsg += "Column Name Array: " + ERR_COLUMN_COUNT;
if (sessCart.numCols != this.colComputed.length) errMsg += "Computed Column Array: " + ERR_COLUMN_COUNT;
if (sessCart.bStoreCookie != this.bStoreCookie) errMsg += "Using Cookies: " + ERR_COOKIE_SETTINGS;
if (sessCart.cookieLifetime != this.cookieLifetime) errMsg += "Cookie Lifetime: " + ERR_COOKIE_SETTINGS;
// check that required columns are in the same place
var productIndex = this.GetIndexOfColName(this.PRODUCTID);
var quantityIndex = this.GetIndexOfColName(this.QUANTITY);
var priceIndex = this.GetIndexOfColName(this.PRICE);
var totalIndex = this.GetIndexOfColName(this.TOTAL);
if (colNames[productIndex] != "ProductID") errMsg += ERR_REQUIRED_COLUMN_NAME + "ProductID<BR>";
if (colNames[quantityIndex] != "Quantity") errMsg += ERR_REQUIRED_COLUMN_NAME + "Quantity<BR>";
if (colNames[priceIndex] != "Price") errMsg += ERR_REQUIRED_COLUMN_NAME + "Price<BR>";
if (colNames[totalIndex] != "Total") errMsg += ERR_REQUIRED_COLUMN_NAME + "Total<BR>";
}
else { // if cart doesn't exist in session, validate input array lengths and presence of reqiured columns
if (this.numCols != this.colComputed.length) errMsg += ERR_INCONSISTENT_ARRAY_LENGTH;
var bProductID = false, bQuantity = false, bPrice = false, bTotal = false;
for (var j = 0; j < colNames.length; j++) {
if (colNames[j] == "ProductID") bProductID = true;
if (colNames[j] == "Quantity") bQuantity= true;
if (colNames[j] == "Price") bPrice = true;
if (colNames[j] == "Total") bTotal = true;
}
if (!bProductID) errMsg += ERR_REQUIRED_COLUMN_NAME + "ProductID<BR>";
if (!bQuantity) errMsg += ERR_REQUIRED_COLUMN_NAME + "Quantity<BR>";
if (!bPrice) errMsg += ERR_REQUIRED_COLUMN_NAME + "Price<BR>";
if (!bTotal) errMsg += ERR_REQUIRED_COLUMN_NAME + "Total<BR>";
}
if (errMsg != "") {
Response.Write(msg + "<BR>");
Response.Write(errMsg + "<BR>");
Response.End();
}
}
function VBConstuctCart(Name, cookieLifetime, vbArrColNames, vbArrColComputed){
var myObj;
var a = new VBArray(vbArrColNames);
var b = new VBArray(vbArrColComputed);
eval("myObj = new UC_ShoppingCart(Name, cookieLifetime, a.toArray(), b.toArray())");
return myObj;
}
</SCRIPT>
<SCRIPT LANGUAGE=vbscript runat=server NAME="UC_CART">
Function GetCookieExp(expDays)
vDate = DateAdd("d", CInt(expDays), Now())
GetCookieExp = CStr(vDate)
End Function
</SCRIPT>
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT NAME="UC_CART">
function DoNumber(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)
DoNumber = FormatNumber(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)
End Function
function DoCurrency(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)
DoCurrency = FormatCurrency(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)
End Function
function DoDateTime(str, nNamedFormat, nLCID)
dim strRet
dim nOldLCID
strRet = str
If (nLCID > -1) Then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -