public void addItem(ShoppingCartItem newItem) { // Check to see if this item is already present, if so, inc the qty int size = getSize(); ShoppingCartItem cartItem = findItem(Integer.toString(newItem.getItemId())); if (cartItem != null) { cartItem.setQuantity(cartItem.getQuantity() + newItem.getQuantity()); } else { // Must have been a different item, so add it to the cart items.add(newItem); } }
/** Update the item in the shopping cart. */ public void updateItem(ShoppingCartItem si) { table.put(si.getID(), new Integer(si.getQuantity())); }
public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); if (cart == null) { Debug.logInfo("No cart to save, doing nothing.", module); return; } String delegatorName = (String) session.getAttribute("delegatorName"); Delegator delegator = null; if (UtilValidate.isNotEmpty(delegatorName)) { delegator = DelegatorFactory.getDelegator(delegatorName); } if (delegator == null) { Debug.logError( "Could not find delegator with delegatorName in session, not saving abandoned cart info.", module); return; } boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); GenericValue visit = VisitHandler.getVisit(session); if (visit == null) { Debug.logError("Could not get the current visit, not saving abandoned cart info.", module); return; } Debug.logInfo("Saving abandoned cart", module); Iterator cartItems = cart.iterator(); int seqId = 1; while (cartItems.hasNext()) { ShoppingCartItem cartItem = (ShoppingCartItem) cartItems.next(); GenericValue cartAbandonedLine = delegator.makeValue("CartAbandonedLine"); cartAbandonedLine.set("visitId", visit.get("visitId")); cartAbandonedLine.set("cartAbandonedLineSeqId", (Integer.valueOf(seqId)).toString()); cartAbandonedLine.set("productId", cartItem.getProductId()); cartAbandonedLine.set("prodCatalogId", cartItem.getProdCatalogId()); cartAbandonedLine.set("quantity", cartItem.getQuantity()); cartAbandonedLine.set("reservStart", cartItem.getReservStart()); cartAbandonedLine.set("reservLength", cartItem.getReservLength()); cartAbandonedLine.set("reservPersons", cartItem.getReservPersons()); cartAbandonedLine.set("unitPrice", cartItem.getBasePrice()); cartAbandonedLine.set("reserv2ndPPPerc", cartItem.getReserv2ndPPPerc()); cartAbandonedLine.set("reservNthPPPerc", cartItem.getReservNthPPPerc()); if (cartItem.getConfigWrapper() != null) { cartAbandonedLine.set("configId", cartItem.getConfigWrapper().getConfigId()); } cartAbandonedLine.set("totalWithAdjustments", cartItem.getItemSubTotal()); // not doing pre-reservations now, so this is always N cartAbandonedLine.set("wasReserved", "N"); cartAbandonedLine.create(); seqId++; } } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error saving abandoned cart info", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } Debug.logError( e, "An entity engine error occurred while saving abandoned cart information", module); } finally { // only commit the transaction if we started one... this will throw an exception if it fails try { TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { Debug.logError( e, "Could not commit transaction for entity engine error occurred while saving abandoned cart information", module); } } }