Tuesday, July 30, 2013

Adding Multiple Items at Once to Shoppint Cart : ATG

Adding Multiple Items at Once


You can create pages that allow users to add multiple items to the current shopping cart in a single form submission. The items can refer to different products, different SKUs, and have different quantities. The CartModifierFormHandler contains an items property that allows you to set per-item property values.

The following JSP code example illustrates adding multiple items for more than one SKU for a single product. In the example, the user can specify in a textbox a different quantity for each SKU to add to the cart. There are hidden input fields for the product ID and SKUs. Each product ID, SKU, and quantity textbox is associated with a subproperty of one element in the CartModifierFormHandler.items array.

When the user clicks the Add To Cart submit button, the form is processed, the properties of CartModifierFormHandler are set, and the handleAddItemToOrder method of CartModifierFormHandler is invoked. The handleAddItemToOrder method iterates through the CartModifierFormHandler.items elements and adds an item for each element with a non-zero quantity, using that element’s productId and catalogRefId for the new item.

<dsp:importbean
bean="/atg/commerce/order/purchase/CartModifierFormHandler"/>
<dsp:importbean bean="/atg/dynamo/droplet/ForEach"/>
<dsp:form action="display_product.jsp" method="post">
<input name="id" type="hidden" value='<dsp:valueof
param="product.repositoryId"/>'>
<dsp:input bean="CartModifierFormHandler.addItemToOrderSuccessURL"
type="hidden" value="shoppingcart.jsp"/>
<table border=1>
<tr>
<td>SKU</td>
<td>Quantity</td>
</tr>
<dsp:droplet name="ForEach">
  <dsp:param name="array" param="product.childSKUs"/>
  <dsp:param name="elementName" value="sku"/>
  <dsp:param name="indexName" value="skuIndex"/>
  <dsp:oparam name="outputStart">
    <dsp:input bean="CartModifierFormHandler.addItemCount"
paramvalue="size" type="hidden"/>
  </dsp:oparam>
  <dsp:oparam name="output">
    <tr>
    <td><dsp:valueof param="sku.displayName"/></td>
    <td>
      <dsp:input
bean="CartModifierFormHandler.items[param:skuIndex].quantity" size="4"
type="text" value="0"/>
      <dsp:input
bean="CartModifierFormHandler.items[param:skuIndex].catalogRefId"
paramvalue="sku.repositoryId" type="hidden"/>
      <dsp:input
bean="CartModifierFormHandler.items[param:skuIndex].productId"
paramvalue="product.repositoryId" type="hidden"/>
    </td>
    </tr>
  </dsp:oparam>

</dsp:droplet>
</table>
<BR>
<dsp:input bean="CartModifierFormHandler.addItemToOrder" type="submit"
value="Add To Cart"/>
</dsp:form>

The CartModifierFormHandler must be told how many elements to allocate for the items array. This is done by setting the CartModifierFormHandler.addItemCount property. In the preceding example, addItemCount is set to the number of SKUs defined for the product in a hidden input field . This technique works in the example because all of the CartModifierFormHandler.items input fields have explicit value or paramvalue attributes.

The next code fragment illustrates a more complex technique for setting CartModifierFormHandler.addItemCount. This technique is appropriate if you want to preserve a user’s input when a page is redisplayed because of a form submission error. The dsp:setvalue tag is not executed if the page is redisplayed.

<dsp:droplet name="/atg/dynamo/droplet/Switch">
  <dsp:param name="value" bean="CartModifierFormHandler.addItemCount"/>
  <dsp:oparam name="0">
    <dsp:setvalue bean="CartModifierFormHandler.addItemCount" value="5"/>
  </dsp:oparam>
</dsp:droplet>

<dsp:input bean="CartModifierFormHandler.addItemCount" value="5" type="hidden"/>






No comments:

Popular Posts