2

I want to modify the quantity of commerceItem in the current order using CartModifierFormHandler.

I have passed catalogRefId & new quantity then called either CartModifierFormHandler.setOrderByCommerceId or setOrder but it modified all items' quantity besides the one I want to modify. Could someone tell me what's wrong or how to do that?

Charles
  • 50,943
  • 13
  • 104
  • 142
Jarry Zhou
  • 21
  • 1
  • 2

4 Answers4

3

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 commerceItems 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.

radimpe
  • 3,197
  • 2
  • 27
  • 46
0

The javadoc for modifyOrder and modifyOrderByCommerceId explains it all -

modifyOrder

Modify the order (accessed from the order property) based on the changes in the request. This method iterates over each of the current CommerceItems in the order and finds the current quantity submitted. In addition we check to make sure that the item should even be included in the order any longer. If the quantity is greater than zero we adjust the quantity in the CommerceItem and the ShippingGroupCommerceItemRelationship. Otherwise we remove the CommerceItem and the relationships.

modifyOrderByCommerceId

Modify the order (accessed from the order property) based on the changes in the request. This method iterates over each of the current CommerceItems in the order and finds the current quantity submitted. Note, this replaces the use of modifyOrder method because that method made use of the catalogRefId to identify the commerce item to be modified instead of using the commerce item id itself. In addition we check to make sure that the item should even be included in the order any longer. If the quantity is greater than zero we adjust the quantity in the CommerceItem and the ShippingGroupCommerceItemRelationship. Otherwise we remove the CommerceItem and the relationships.

You can also look at the source code of CartModifierFormHandler at

ATG_INSTALL_LOCATION\DCS\src\Java\atg\commerce\order\purchase

user1339772
  • 783
  • 5
  • 19
0

if you want update quantity of specific item in cart page you will need to pass quantity as stated below :

<input type="text"  maxlength="3" name="<dsp:valueof param='commerceItem.id'/>" value="<dsp:valueof param='commerceItem.quantity'/>">

update cart button:

<dsp:input bean="CartModifierFormHandler.moveToPurchaseInfoByCommerceId" type="submit" value="Update Cart" id="sbmtUpdateCart" />

moveToPurchaseInfoByCommerceId in above code will fetch the quantity by commerceItem Id from request parameter and updates the commerceitem with the quantity.

Atg does:

int qty=request.getParameter(commerceItem.id);
commerceItem.setQuantity(qty);

and the quantity of comerce item is update.

0

A bit more concise than @radimpe's answer. I have added error handling for "order". I have also expect a single request parameter, the commerce item ID as the name and the quantity as the value (e.g. "ci10000099":"2"). There does not seem to be a need to call setCheckForChangedQuantity(true), unless I am missing something.

Order order = getOrder();
if (order == null) {
    String msg = formatUserMessage(MSG_NO_ORDER_TO_MODIFY, pRequest, pResponse);
    throw new ServletException(msg);
}

// Add the commerce items and their quantities to the request so that they are not removed from the order
List<CommerceItem> commerceItems = order.getCommerceItems();
for (CommerceItem commerceItem : commerceItems) {
    if (pRequest.getParameter(commerceItem.getId()) == null) {
        pRequest.setParameter(commerceItem.getId(), commerceItem.getQuantity());
    }
}

handleSetOrderByCommerceId(pRequest, pResponse);

return checkFormRedirect(getUpdateSuccessURL(), getUpdateErrorURL(), pRequest, pResponse);
Matt Sidesinger
  • 2,124
  • 1
  • 22
  • 18
  • You should use the `setCheckForChangedQuantity` flag to inform the formhandler to read the new quantity from the request and not from the item itself. If you don't set this it will do an `item.getQuantity()` instead. So looking at the code you've given above it does the same thing, you read the quantity from the `commerceItem` and not from the request so you won't be changing the quantity on the order. – radimpe Mar 26 '15 at 09:19