📄 shoppingcarthelper.java
字号:
try {
quantity = Double.parseDouble(quantStr);
} catch (NumberFormatException nfe) {
quantity = 0;
}
if (quantity > 0.0) {
try {
this.cart.addOrIncreaseItem(productCategoryMember.getString("productId"), 0.00, quantity, null, null, catalogId, dispatcher);
} catch (CartItemModifyException cartException) {
result = ServiceUtil.returnError(cartException.getMessage());
return result;
}
}
}
}
//Indicate there were no non critical errors
result = ServiceUtil.returnSuccess();
return result;
}
/**
* 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("No category specified to add from.");
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);
return result;
}
if (prodCatMemberCol == null) {
Map messageMap = UtilMisc.toMap("categoryId", categoryId);
errMsg = UtilProperties.getMessage(resource,"cart.could_not_get_products_in_category", messageMap, this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
double totalQuantity = 0;
Iterator pcmIter = prodCatMemberCol.iterator();
while (pcmIter.hasNext()) {
GenericValue productCategoryMember = (GenericValue) pcmIter.next();
Double quantity = productCategoryMember.getDouble("quantity");
if (quantity != null && quantity.doubleValue() > 0.0) {
try {
this.cart.addOrIncreaseItem(productCategoryMember.getString("productId"), 0.00, quantity.doubleValue(), null, null, catalogId, dispatcher);
totalQuantity += quantity.doubleValue();
} catch (CartItemModifyException 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
}
result = ServiceUtil.returnSuccess();
result.put("totalQuantity", new Double(totalQuantity));
return result;
}
/** Delete an item from the shopping cart. */
public Map deleteFromCart(Map context) {
Map result = null;
Set names = context.keySet();
Iterator i = names.iterator();
ArrayList errorMsgs = new ArrayList();
while (i.hasNext()) {
String o = (String) i.next();
if (o.toUpperCase().startsWith("DELETE")) {
try {
String indexStr = o.substring(o.lastIndexOf('_') + 1);
int index = Integer.parseInt(indexStr);
try {
this.cart.removeCartItem(index, dispatcher);
} catch (CartItemModifyException e) {
errorMsgs.add(e.getMessage());
}
} catch (NumberFormatException nfe) {}
}
}
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
}
result = ServiceUtil.returnSuccess();
return result;
}
/** Update the items in the shopping cart. */
public Map modifyCart(Security security, GenericValue userLogin, Map context, boolean removeSelected, String[] selectedItems) {
Map result = null;
ArrayList deleteList = new ArrayList();
ArrayList errorMsgs = new ArrayList();
Set names = context.keySet();
Iterator i = names.iterator();
while (i.hasNext()) {
String o = (String) i.next();
int underscorePos = o.lastIndexOf('_');
if (underscorePos >= 0) {
try {
String indexStr = o.substring(underscorePos + 1);
int index = Integer.parseInt(indexStr);
String quantString = (String) context.get(o);
double quantity = -1;
// get the cart item
ShoppingCartItem item = this.cart.findCartItem(index);
if (o.toUpperCase().startsWith("OPTION")) {
if (quantString.toUpperCase().startsWith("NO^")) {
if (quantString.length() > 2) { // the length of the prefix
String featureTypeId = this.getRemoveFeatureTypeId(o);
if (featureTypeId != null) {
item.removeAdditionalProductFeatureAndAppl(featureTypeId);
}
}
} else {
GenericValue featureAppl = this.getFeatureAppl(item.getProductId(), o, quantString);
if (featureAppl != null) {
item.putAdditionalProductFeatureAndAppl(featureAppl);
}
}
} else {
quantity = NumberFormat.getNumberInstance().parse(quantString).doubleValue();
if (quantity < 0) {
throw new CartItemModifyException("Quantity must be a positive number.");
}
}
if (o.toUpperCase().startsWith("UPDATE")) {
if (quantity == 0.0) {
deleteList.add(item);
} else {
if (item != null) {
try {
item.setQuantity(quantity, dispatcher, this.cart);
} catch (CartItemModifyException e) {
errorMsgs.add(e.getMessage());
}
}
}
}
if (o.toUpperCase().startsWith("PRICE")) {
if (security.hasEntityPermission("ORDERMGR", "_CREATE", userLogin)) {
if (item != null) {
item.setBasePrice(quantity); // this is quanity because the parsed number variable is the same as quantity
}
}
}
if (o.toUpperCase().startsWith("DELETE")) {
deleteList.add(this.cart.findCartItem(index));
}
} catch (NumberFormatException nfe) {
Debug.logWarning(nfe, "Caught number format exception on cart update.", module);
} catch (ParseException pe) {
Debug.logWarning(pe, "Caught parse exception on cart update.", module);
} catch (Exception e) {
Debug.logWarning(e, "Caught exception on cart update.", module);
}
} // else not a parameter we need
}
// get a list of the items to delete
if (removeSelected) {
for (int si = 0; si < selectedItems.length; si++) {
String indexStr = selectedItems[si];
ShoppingCartItem item = null;
try {
int index = Integer.parseInt(indexStr);
item = this.cart.findCartItem(index);
} catch (Exception e) {
Debug.logWarning(e, "Problems getting the cart item by index", module);
}
if (item != null) {
deleteList.add(item);
}
}
}
Iterator di = deleteList.iterator();
while (di.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) di.next();
int itemIndex = this.cart.getItemIndex(item);
if (Debug.infoOn())
Debug.logInfo("Removing item index: " + itemIndex, module);
try {
this.cart.removeCartItem(itemIndex, dispatcher);
} catch (CartItemModifyException e) {
ServiceUtil.returnError(new Vector());
errorMsgs.add(e.getMessage());
}
}
if (context.containsKey("alwaysShowcart")) {
this.cart.setViewCartOnAdd(true);
} else {
this.cart.setViewCartOnAdd(false);
}
if (errorMsgs.size() > 0) {
result = ServiceUtil.returnError(errorMsgs);
return result;
}
result = ServiceUtil.returnSuccess();
return result;
}
/** Empty the shopping cart. */
public boolean clearCart() {
this.cart.clear();
return true;
}
/** Returns the shopping cart this helper is wrapping. */
public ShoppingCart getCartObject() {
return this.cart;
}
public GenericValue getFeatureAppl(String productId, String optionField, String featureId) {
if (delegator == null) {
throw new IllegalArgumentException("No delegator available to lookup ProductFeature");
}
Map fields = UtilMisc.toMap("productId", productId, "productFeatureId", featureId);
if (optionField != null) {
int featureTypeStartIndex = optionField.indexOf('^') + 1;
int featureTypeEndIndex = optionField.lastIndexOf('_');
if (featureTypeStartIndex > 0 && featureTypeEndIndex > 0) {
fields.put("productFeatureTypeId", optionField.substring(featureTypeStartIndex, featureTypeEndIndex));
}
}
GenericValue productFeatureAppl = null;
List features = null;
try {
features = delegator.findByAnd("ProductFeatureAndAppl", fields, UtilMisc.toList("-fromDate"));
} catch (GenericEntityException e) {
Debug.logError(e, module);
return null;
}
if (features != null) {
if (features.size() > 1) {
features = EntityUtil.filterByDate(features);
}
productFeatureAppl = EntityUtil.getFirst(features);
}
return productFeatureAppl;
}
public String getRemoveFeatureTypeId(String optionField) {
if (optionField != null) {
int featureTypeStartIndex = optionField.indexOf('^') + 1;
int featureTypeEndIndex = optionField.lastIndexOf('_');
if (featureTypeStartIndex > 0 && featureTypeEndIndex > 0) {
return optionField.substring(featureTypeStartIndex, featureTypeEndIndex);
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -