📄 shoppingcarthelper.java
字号:
String orderItemTypeId = null; if (itemIter != null && itemIter.hasNext()) { while (itemIter.hasNext()) { GenericValue orderItem = (GenericValue) itemIter.next(); orderItemTypeId = orderItem.getString("orderItemTypeId"); // do not store rental items if (orderItemTypeId.equals("RENTAL_ORDER_ITEM")) continue; // never read: int itemId = -1; if (orderItem.get("productId") != null && orderItem.get("quantity") != null) { double amount = 0.00; if (orderItem.get("selectedAmount") != null) { amount = orderItem.getDouble("selectedAmount").doubleValue(); } try { this.cart.addOrIncreaseItem(orderItem.getString("productId"), amount, orderItem.getDouble("quantity").doubleValue(), null, null, catalogId, dispatcher); noItems = false; } catch (CartItemModifyException e) { errorMsgs.add(e.getMessage()); } catch (ItemNotFoundException e) { errorMsgs.add(e.getMessage()); } } } if (errorMsgs.size() > 0) { result = ServiceUtil.returnError(errorMsgs); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); return result; // don't return error because this is a non-critical error and should go back to the same page } } else { noItems = true; } } else { noItems = true; if (itemIds != null) { for (int i = 0; i < itemIds.length; i++) { String orderItemSeqId = itemIds[i]; GenericValue orderItem = null; try { orderItem = delegator.findByPrimaryKey("OrderItem", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId)); } catch (GenericEntityException e) { Debug.logWarning(e.getMessage(), module); errorMsgs.add("Order line \"" + orderItemSeqId + "\" not found, so not added."); continue; } if (orderItem != null) { if (orderItem.get("productId") != null && orderItem.get("quantity") != null) { double amount = 0.00; if (orderItem.get("selectedAmount") != null) { amount = orderItem.getDouble("selectedAmount").doubleValue(); } try { this.cart.addOrIncreaseItem(orderItem.getString("productId"), amount, orderItem.getDouble("quantity").doubleValue(), null, null, catalogId, dispatcher); noItems = false; } catch (CartItemModifyException e) { errorMsgs.add(e.getMessage()); } catch (ItemNotFoundException e) { errorMsgs.add(e.getMessage()); } } } } if (errorMsgs.size() > 0) { result = ServiceUtil.returnError(errorMsgs); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); return result; // don't return error because this is a non-critical error and should go back to the same page } } // else no items } if (noItems) { result = ServiceUtil.returnSuccess(); result.put("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderNoItemsFoundToAdd", this.cart.getLocale())); return result; // don't return error because this is a non-critical error and should go back to the same page } result = ServiceUtil.returnSuccess(); return result; } /** * Adds all products in a category according to quantity request parameter * for each; if no parameter for a certain product in the category, or if * quantity is 0, do not add */ public Map addToCartBulk(String catalogId, String categoryId, Map context) { String keyPrefix = "quantity_"; // iterate through the context and find all keys that start with "quantity_" Iterator entryIter = context.entrySet().iterator(); while (entryIter.hasNext()) { Map.Entry entry = (Map.Entry) entryIter.next(); String productId = null; if (entry.getKey() instanceof String) { String key = (String) entry.getKey(); //Debug.logInfo("Bulk Key: " + key, module); if (key.startsWith(keyPrefix)) { productId = key.substring(keyPrefix.length()); } else { continue; } } else { continue; } String quantStr = (String) entry.getValue(); if (quantStr != null && quantStr.length() > 0) { double quantity = 0; try { quantity = Double.parseDouble(quantStr); } catch (NumberFormatException nfe) { quantity = 0; } if (quantity > 0.0) { try { if (Debug.verboseOn()) Debug.logVerbose("Bulk Adding to cart [" + quantity + "] of [" + productId + "]", module); this.cart.addOrIncreaseItem(productId, 0.00, quantity, null, null, catalogId, dispatcher); } catch (CartItemModifyException e) { return ServiceUtil.returnError(e.getMessage()); } catch (ItemNotFoundException e) { return ServiceUtil.returnError(e.getMessage()); } } } } //Indicate there were no non critical errors return ServiceUtil.returnSuccess(); } /** * Adds a set of requirements to the cart. */ public Map addToCartBulkRequirements(String catalogId, Map context) { NumberFormat nf = NumberFormat.getNumberInstance(this.cart.getLocale()); // check if we are using per row submit boolean useRowSubmit = (!context.containsKey("_useRowSubmit"))? false : "Y".equalsIgnoreCase((String)context.get("_useRowSubmit")); // check if we are to also look in a global scope (no delimiter) boolean checkGlobalScope = (!context.containsKey("_checkGlobalScope"))? false : "Y".equalsIgnoreCase((String)context.get("_checkGlobalScope")); int rowCount = 0; // parsed int value try { if (context.containsKey("_rowCount")) { rowCount = Integer.parseInt((String)context.get("_rowCount")); } } catch (NumberFormatException e) { //throw new EventHandlerException("Invalid value for _rowCount"); } // now loop throw the rows and prepare/invoke the service for each for (int i = 0; i < rowCount; i++) { String productId = null; String quantStr = null; String requirementId = null; String thisSuffix = UtilHttp.MULTI_ROW_DELIMITER + i; boolean rowSelected = (!context.containsKey("_rowSubmit" + thisSuffix))? false : "Y".equalsIgnoreCase((String)context.get("_rowSubmit" + thisSuffix)); // make sure we are to process this row if (useRowSubmit && !rowSelected) { continue; } // build the context if (context.containsKey("productId" + thisSuffix)) { productId = (String) context.get("productId" + thisSuffix); quantStr = (String) context.get("quantity" + thisSuffix); requirementId = (String) context.get("requirementId" + thisSuffix); if (quantStr != null && quantStr.length() > 0) { double quantity = 0; try { quantity = nf.parse(quantStr).doubleValue(); } catch (ParseException nfe) { quantity = 0; } if (quantity > 0.0) { Iterator items = this.cart.iterator(); boolean requirementAlreadyInCart = false; while (items.hasNext() && !requirementAlreadyInCart) { ShoppingCartItem sci = (ShoppingCartItem)items.next(); if (sci.getRequirementId() != null && sci.getRequirementId().equals(requirementId)) { requirementAlreadyInCart = true; continue; } } if (requirementAlreadyInCart) { if (Debug.warningOn()) Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderTheRequirementIsAlreadyInTheCartNotAdding", UtilMisc.toMap("requirementId",requirementId), cart.getLocale()), module); continue; } try { if (Debug.verboseOn()) Debug.logVerbose("Bulk Adding to cart requirement [" + quantity + "] of [" + productId + "]", module); int index = this.cart.addOrIncreaseItem(productId, 0.00, quantity, null, null, catalogId, dispatcher); ShoppingCartItem sci = (ShoppingCartItem)this.cart.items().get(index); sci.setRequirementId(requirementId); } catch (CartItemModifyException e) { return ServiceUtil.returnError(e.getMessage()); } catch (ItemNotFoundException e) { return ServiceUtil.returnError(e.getMessage()); } } } } } //Indicate there were no non critical errors return ServiceUtil.returnSuccess(); } /** * Adds all products in a category according to default quantity on ProductCategoryMember * for each; if no default for a certain product in the category, or if * quantity is 0, do not add */ public Map addCategoryDefaults(String catalogId, String categoryId) { ArrayList errorMsgs = new ArrayList(); Map result = null; String errMsg = null; if (categoryId == null || categoryId.length() <= 0) { errMsg = UtilProperties.getMessage(resource,"cart.category_not_specified_to_add_from", this.cart.getLocale()); result = ServiceUtil.returnError(errMsg);// result = ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderNoCategorySpecifiedToAddFrom.",this.cart.getLocale())); return result; } Collection prodCatMemberCol = null; try { prodCatMemberCol = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productCategoryId", categoryId)); } catch (GenericEntityException e) { Debug.logWarning(e.toString(), module); Map messageMap = UtilMisc.toMap("categoryId", categoryId); messageMap.put("message", e.getMessage()); errMsg = UtilProperties.getMessage(resource,"cart.could_not_get_products_in_category_cart", messageMap, this.cart.getLocale()); result = ServiceUtil.returnError(errMsg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -