// calculates totals private void calculateTotals() { // get cart summary ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); IReadOnlyCollection <ShoppingCart_S2.ShoppingCartItem_S2> _items = _cart.Items; int _totalItems = 0, _totalQuantity = 0; double _totalPrice = 0, _totalWeight = 0, _finalPrice = 0; foreach (ShoppingCart_S2.ShoppingCartItem_S2 _cartItem in _items) { if (_cartItem.Quantity <= 0) { continue; } ++_totalItems; _totalQuantity += _cartItem.Quantity; Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_cartItem.ItemID); _totalPrice += _item.Price * _cartItem.Quantity; _totalWeight += _item.Weight * _cartItem.Quantity; } _finalPrice = _totalPrice + _totalWeight * costPerOunce; totalItems = _totalItems; totalQuantity = _totalQuantity; totalPrice = _totalPrice; totalWeight = _totalWeight; totalShipping = _totalWeight * costPerOunce; finalTotal = _finalPrice; }
// click add to cart private void ClickAddToCart(object _sender, EventArgs _e) { // get cart ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); // get label for response Label _response = (Label)contentItemArea.FindControl("LabelCartAdded"); if (_response == null) { Trace.Warn("Shop", "Failed to find label for cart response"); return; } // add item if (_cart.Add(item.ID)) { _response.Text = string.Format("Added to cart"); _response.ForeColor = System.Drawing.Color.YellowGreen; Response.Redirect("Cart.aspx", true); } else { _response.Text = string.Format("Failed to add to cart"); _response.ForeColor = System.Drawing.Color.Red; } }
// builds message body (HTML) private string buildMessageBody(string name, string street, string city, string state, string zip, string email, string orderStr) { // get totals calculateTotals(); string body = ""; // opening body += string.Format("<p>Order Confirmation #{0}</p>", orderStr); // order summary body += "<h5>Summary:</h5>"; body += string.Format("<table>{0}{1}{2}{3}{4}{5}</table>", string.Format("<tr><td>Items</td><td align=\"right\">{0}</td></tr>", totalItems), string.Format("<tr><td>Quantity</td><td align=\"right\">{0}</td></tr>", totalQuantity), string.Format("<tr><td>Price</td><td align=\"right\">{0:C2}</td></tr>", totalPrice), string.Format("<tr><td>Weight</td><td align=\"right\">{0:f2}</td></tr>", totalWeight), string.Format("<tr><td>Shipping Total</td align=\"right\"><td>{0:C2}</td></tr>", totalShipping), string.Format("<tr><td>Final Total</td><td align=\"right\">{0:C2}</td></tr>", finalTotal) ); // order information body += "<h5>Shipping Information</h5>"; body += string.Format("<table>{0}{1}{2}</table>", string.Format("<tr><td>{0}</td></tr>", name), string.Format("<tr><td>{0}</td></tr>", street), string.Format("<tr><td>{0} {1} {2}</td></tr>", city, state, zip) ); // order items body += "<h5>Items</h5>"; body += "<table>"; // build title row body += "<tr><td>ID</td><td>Name</td><td>Unit Price</td><td>Unit Weight</td><td>Quantity</td></tr>"; // build rows for each item ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); IReadOnlyCollection <ShoppingCart_S2.ShoppingCartItem_S2> _items = _cart.Items; foreach (ShoppingCart_S2.ShoppingCartItem_S2 _cartItem in _items) { Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_cartItem.ItemID); body += string.Format("<tr>{0}{1}{2}{3}{4}</tr>", string.Format("<td>{0}</td>", _item.ID), string.Format("<td>\"{0}\"</td>", _item.Name), string.Format("<td>{0:C2}</td>", _item.Price), string.Format("<td>{0:f2}</td>", _item.Weight), string.Format("<td>{0}</td>", _cartItem.Quantity) ); } body += "</table>"; return(body); }
// session methods // get shopping cart public object GetCart() { // get cart from session state ShoppingCart_S2 _cart = (ShoppingCart_S2)Session["Cart"]; // if does not exist, create if (_cart == null) { _cart = new ShoppingCart_S2(); Session.Add("Cart", _cart); } return(_cart); }
// update button click private void CommandUpdate(object _sender, CommandEventArgs _e) { Trace.Warn("cart", string.Format("CommandUpdate(\"{0}\":{1})", _e.CommandName, _e.CommandArgument)); if (_e.CommandName.Equals("ItemID", StringComparison.OrdinalIgnoreCase)) { int _id = Int32.Parse((string)_e.CommandArgument); Trace.Write("Shop", string.Format("Updating id {0}", _id)); // find row w/ item TableRowCollection _rows = tableCart.Rows; TableRow _row = null; int i = 1; while (i < _rows.Count) { _row = _rows[i]; // get ID for row HyperLink _link = (HyperLink)_row.Cells[0].Controls[0]; int _rowID = Int32.Parse(_link.Text); if (_id == _rowID) { break; } ++i; } if (i == _rows.Count) { Trace.Warn("Shop Update", string.Format("Failed to find row w/ ID: {0}", _id)); return; } // pull value from textbox int _quantity = Int32.Parse((((TextBox)(_row.Cells[4].Controls[0])).Text)); Trace.Write("Cart", string.Format("Quantity read: {0}", _quantity)); ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); if (_cart.Update(_id, _quantity)) { Trace.Write("cart", string.Format("updated id:{0}", _id)); } else { Trace.Warn("cart", string.Format("failed to update id:{0}", _id)); } } }
// builds cart table private void BuildCartTable() { // get cart from master page ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); // get items IReadOnlyCollection <ShoppingCart_S2.ShoppingCartItem_S2> _items = _cart.Items; // check if empty if (_items.Count > 0) { PopulateTable(_items); } else { DisplayEmptyCart(); } }
// remove button click private void CommandRemove(object _sender, CommandEventArgs _e) { Trace.Warn("cart", string.Format("CommandRemove(\"{0}\":{1})", _e.CommandName, _e.CommandArgument)); if (_e.CommandName.Equals("ItemID", StringComparison.OrdinalIgnoreCase)) { int _id = Int32.Parse((string)_e.CommandArgument); ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); if (_cart.Remove(_id)) { Trace.Warn("cart", string.Format("removed id:{0}", _id)); } else { Trace.Warn("cart", string.Format("failed to remove id:{0}", _id)); } } }
// page load protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } // validate previous page try { if (PreviousPage == null) { Response.Redirect("./Browse.aspx", true); } // IsPostBack? }catch (InvalidOperationException _e) { Response.Redirect("./Browse.aspx", true); } // check if cart empty ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); IReadOnlyCollection <ShoppingCart_S2.ShoppingCartItem_S2> _items = _cart.Items; if (_items.Count <= 0) { Response.Redirect("./Browse.aspx", true); } int _total = 0; foreach (ShoppingCart_S2.ShoppingCartItem_S2 _item in _items) { _total += _item.Quantity; } if (_total <= 0) { Response.Redirect("./Browse.aspx", true); } // build page buildDisplay(); }
// updates table private void RefreshCartTable() { // get cart ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); // cycle through each row, except the first one // find items w/ quantity of zero, remove row TableRowCollection _rows = tableCart.Rows; TableRow _row = null; int i = 1; while (i < _rows.Count) { _row = _rows[i]; // get ID for row HyperLink _link = (HyperLink)_row.Cells[0].Controls[0]; int _id = Int32.Parse(_link.Text); Trace.Write("Cart", string.Format("Refreshing row id: {0}", _id)); int _actualQuantity = _cart.Quantity(_id); // check quantity if (_actualQuantity <= 0) { _row.Cells.Clear(); _rows.Remove(_row); Trace.Warn("cart", string.Format("removed row id: {0}", _id)); } else { // always refresh int _quantity = Int32.Parse((((TextBox)(_row.Cells[4].Controls[0])).Text)); Trace.Write("Cart", string.Format("Quantity read: {0}", _quantity)); // get item info Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_id); // update quantity and ext price/weight //_row.Cells[4].Text = _actualQuantity.ToString(); _row.Cells[5].Text = (_item.Price * _actualQuantity).ToString("C2"); _row.Cells[6].Text = (_item.Weight * _actualQuantity).ToString("f2"); ++i; } } // check row count if (tableCart.Rows.Count <= 1) { DisplayEmptyCart(); return; } // calculate totals double _totalPrice = 0, _totalWeight = 0; foreach (ShoppingCart_S2.ShoppingCartItem_S2 _cartItem in _cart.Items) { // get item Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_cartItem.ItemID); _totalPrice += _item.Price * _cartItem.Quantity; _totalWeight += _item.Weight * _cartItem.Quantity; } // add totals row _row = new TableRow(); _row.CssClass = "TotalRow"; // populate empty cells TableCell _cell = null; for (i = 0; i < 5; ++i) { _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = " "; _row.Cells.Add(_cell); } // total ext price _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = _totalPrice.ToString("C2"); _row.Cells.Add(_cell); // total ext weight _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = _totalWeight.ToString("f2"); _row.Cells.Add(_cell); tableCart.Rows.Add(_row); }