If you are using the out-of-the-box setOrderByCommerceId
method (which is definitely the way to go since it already takes care of all the transaction related code) then you need to keep the following in mind.
setOrderByCommerceId
will call modifyOrderByCommerceId
in the out-of-the-box CartModifierFormHandler
. This in turn calls getQuantity
public long getQuantity(String pCatalogRefId, DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse) throws ServletException, IOException, NumberFormatException {
Object value = pRequest.getParameter(pCatalogRefId);
if (value != null) {
return Long.parseLong(value.toString());
}
return getQuantity();
}
So from this snippet it should be clear that, unless you pass the individual quantity of each existing commerceItem
to the request, you will simply update the quantity of each of the commerceItem
s to the same quantity.
Here is a simplified version of updating the quantity:
public boolean handleUpdateItemQuantityToOrder(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws ServletException, IOException {
Order order = getOrder();
//Get All the Commerce Items on the Order
List<CommerceItem> commerceItems = order.getCommerceItems();
String currentSku = "";
// currentCommerceItem is the one that you passed from your JSP page
String currentId = getCurrentCommerceItem();
// Add all the existing commerce item quantities to the request
for (CommerceItem commerceItem : commerceItems) {
request.setParameter(commerceItem.getId(), commerceItem.getQuantity());
if (commerceItem.getId().equals(currentId)) {
currentSku = commerceItem.getCatalogRefId();
}
}
// the quantity element is from the JSP page. Set it to the Sku you want to change on the request.
request.setParameter(currentId, getQuantity());
// Set the new quantity for the commerce item being updated.
setCheckForChangedQuantity(true);
handleSetOrderByCommerceId(request, response); // Pass the order updates to a REAL ATG method so you don't have to write it yourself
return checkFormRedirect(getUpdateSuccessURL(), getUpdateErrorURL(), request, response);
}
Hope this helps.