📄 shoppingcartevents.java
字号:
}
// Translate the parameters and add to the cart
result = cartHelper.addToCart(catalogId, shoppingListId, shoppingListItemSeqId, productId, productCategoryId,
itemType, itemDescription, price, amount, quantity, paramMap);
controlDirective = processResult(result, request);
// Determine where to send the browser
if (controlDirective.equals(NON_CRITICAL_ERROR)) {
return "success";
} else if (controlDirective.equals(ERROR)) {
return "error";
} else if (cart.viewCartOnAdd()) {
return "viewcart";
} else {
return "success";
}
}
public static String addToCartFromOrder(HttpServletRequest request, HttpServletResponse response) {
String orderId = request.getParameter("order_id");
String[] itemIds = request.getParameterValues("item_id");
// not used yet: Locale locale = UtilHttp.getLocale(request);
ShoppingCart cart = getCartObject(request);
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart);
String catalogId = CatalogWorker.getCurrentCatalogId(request);
Map result;
String controlDirective;
boolean addAll = ("true".equals(request.getParameter("add_all")));
result = cartHelper.addToCartFromOrder(catalogId, orderId, itemIds, addAll);
controlDirective = processResult(result, request);
//Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
} else {
return "success";
}
}
/** 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 static String addToCartBulk(HttpServletRequest request, HttpServletResponse response) {
String categoryId = request.getParameter("category_id");
ShoppingCart cart = getCartObject(request);
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart);
String controlDirective;
Map result;
// not used yet: Locale locale = UtilHttp.getLocale(request);
//Convert the params to a map to pass in
Map paramMap = UtilHttp.getParameterMap(request);
String catalogId = CatalogWorker.getCurrentCatalogId(request);
result = cartHelper.addToCartBulk(catalogId, categoryId, paramMap);
controlDirective = processResult(result, request);
//Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
} else {
return "success";
}
}
/** 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 static String addCategoryDefaults(HttpServletRequest request, HttpServletResponse response) {
String categoryId = request.getParameter("category_id");
String catalogId = CatalogWorker.getCurrentCatalogId(request);
ShoppingCart cart = getCartObject(request);
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart);
String controlDirective;
Map result;
Double totalQuantity;
Locale locale = UtilHttp.getLocale(request);
result = cartHelper.addCategoryDefaults(catalogId, categoryId);
controlDirective = processResult(result, request);
//Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
} else {
totalQuantity = (Double)result.get("totalQuantity");
Map messageMap = UtilMisc.toMap("totalQuantity", UtilFormatOut.formatQuantity(totalQuantity) );
request.setAttribute("_EVENT_MESSAGE_",
UtilProperties.getMessage(resource, "cart.add_category_defaults",
messageMap, locale ));
return "success";
}
}
/** Delete an item from the shopping cart. */
public static String deleteFromCart(HttpServletRequest request, HttpServletResponse response) {
ShoppingCart cart = getCartObject(request);
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(null, dispatcher, cart);
String controlDirective;
Map result;
Map paramMap = UtilHttp.getParameterMap(request);
// not used yet: Locale locale = UtilHttp.getLocale(request);
//Delegate the cart helper
result = cartHelper.deleteFromCart(paramMap);
controlDirective = processResult(result, request);
//Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
} else {
return "success";
}
}
/** Update the items in the shopping cart. */
public static String modifyCart(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
ShoppingCart cart = getCartObject(request);
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Security security = (Security) request.getAttribute("security");
ShoppingCartHelper cartHelper = new ShoppingCartHelper(null, dispatcher, cart);
String controlDirective;
Map result;
// not used yet: Locale locale = UtilHttp.getLocale(request);
Map paramMap = UtilHttp.getParameterMap(request);
String removeSelectedFlag = request.getParameter("removeSelected");
String selectedItems[] = request.getParameterValues("selectedItem");
boolean removeSelected = ("true".equals(removeSelectedFlag) && selectedItems != null && selectedItems.length > 0);
result = cartHelper.modifyCart(security, userLogin, paramMap, removeSelected, selectedItems);
controlDirective = processResult(result, request);
//Determine where to send the browser
if (controlDirective.equals(ERROR)) {
return "error";
} else {
return "success";
}
}
/** Empty the shopping cart. */
public static String clearCart(HttpServletRequest request, HttpServletResponse response) {
ShoppingCart cart = getCartObject(request);
cart.clear();
return "success";
}
/** Totally wipe out the cart, removes all stored info. */
public static String destroyCart(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
clearCart(request, response);
session.removeAttribute("shoppingCart");
session.removeAttribute("orderPartyId");
session.removeAttribute("orderMode");
return "success";
}
/** Gets the shopping cart from the session. Used by all events. */
public static ShoppingCart getCartObject(HttpServletRequest request) {
HttpSession session = request.getSession(true);
ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
if (cart == null) {
cart = new WebShoppingCart(request);
session.setAttribute("shoppingCart", cart);
}
return cart;
}
/**
* This should be called to translate the error messages of the
* <code>ShoppingCartHelper</code> to an appropriately formatted
* <code>String</code> in the request object and indicate whether
* the result was an error or not and whether the errors were
* critical or not
*
* @param result The result returned from the
* <code>ShoppingCartHelper</code>
* @param request The servlet request instance to set the error messages
* in
* @return one of NON_CRITICAL_ERROR, ERROR or NO_ERROR.
*/
private static String processResult(Map result, HttpServletRequest request) {
//Check for errors
StringBuffer errMsg = new StringBuffer();
if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
List errorMsgs = (List)result.get(ModelService.ERROR_MESSAGE_LIST);
Iterator iterator = errorMsgs.iterator();
errMsg.append("<ul>");
while (iterator.hasNext()) {
errMsg.append("<li>");
errMsg.append(iterator.next());
errMsg.append("</li>");
}
errMsg.append("</ul>");
} else if (result.containsKey(ModelService.ERROR_MESSAGE)) {
errMsg.append(result.get(ModelService.ERROR_MESSAGE));
request.setAttribute("_ERROR_MESSAGE_", errMsg.toString());
}
//See whether there was an error
if (errMsg.length() > 0) {
request.setAttribute("_ERROR_MESSAGE_", errMsg.toString());
if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) {
return NON_CRITICAL_ERROR;
} else {
return ERROR;
}
} else {
return NO_ERROR;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -