Tuesday, July 30, 2013

Adding One Item at a Time to Shopping Cart : ATG

Adding One Item at a Time


The simplest way to add items to the current shopping cart is to add them one at a time. The following JSP code example serves as an illustration.

In the example, the user can select which SKU of the current product to add to the cart from a drop-down list. A ForEach servlet bean is used to iterate over the SKUs of the product and populate the drop-down list, which is associated with the catalogRefIds property of the CartModifierFormHandler. Additionally, the user can specify in a textbox a quantity of the selected SKU to add to the cart, which is associated with the quantity property of the CartModifierFormHandler.

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 adds the quantity (specified in CartModifierFormHandler.quantity) of the selected SKU (specified in CartModifierFormHandler.catalogRefIds and identified by repository ID) to the current Order and then reprices the Order.

<dsp:importbean
bean="/atg/commerce/order/purchase/CartModifierFormHandler"/>
<dsp:importbean bean="/atg/dynamo/droplet/ForEach"/>

<%--Create a form for user to select a SKU and add it to his/her cart:--%>
<dsp:getvalueof id="form10" bean="/OriginatingRequest.requestURI"
idtype="java.lang.String">
<dsp:form action="<%=form10%>" method="post">

<%--Store this product's ID in the Form Handler: --%>
<dsp:input bean="CartModifierFormHandler.ProductId"
paramvalue="Product.repositoryId" type="hidden"/>

<%--set id param so that the Navigator won't get messed up in case of an
error that makes us return to this page.--%>
<input value='<dsp:valueof param="Product.repositoryId"/>' type="hidden"
name="id">

 <table cellpadding=0  cellspacing=0 border=0>
   <tr><td class=box-top-store>Add to Cart</td></tr>
   <tr><td class=box>

<%--Display any errors that have been generated during Cart
        operations:--%>
       <dsp:include
page="../../common/DisplayCartModifierFormHandlerErrors.jsp"></dsp:include>

       Add

<%--Textbox with QTY the user wants to order: --%>
       <dsp:input bean="CartModifierFormHandler.quantity" size="4"
value="1" type="text"/>

<%--Create a dropdown list with all SKUs in the Product.
        Store the selected SKU's id in the form handler: --%>
       <dsp:select bean="CartModifierFormHandler.catalogRefIds">

<%--For each of the SKUs in this Product, add the SKU to the
        dropdown list:--%>
         <dsp:droplet name="ForEach">
           <dsp:param param="Product.childSKUs" name="array"/>
           <dsp:param value="Sku" name="elementName"/>
           <dsp:param value="skuIndex" name="indexName"/>
           <dsp:oparam name="output">

<%--This is the ID to store if this SKU is selected in
            dropdown:--%>
             <dsp:getvalueof id="option73" param="Sku.repositoryID"
idtype="java.lang.String">
<dsp:option value="<%=option73%>"/>
</dsp:getvalueof>

<%--Display the SKU's display name in the dropdown
            list:--%>
             <dsp:valueof param="Sku.displayName"/>
           </dsp:oparam>
         </dsp:droplet>
<%--ForEach SKU droplet--%>
       </dsp:select>
       <br>


<%-- ADD TO CART BUTTON: Adds this SKU to the Order--%>
       <dsp:input bean="CartModifierFormHandler.addItemToOrder"
value="Add to Cart" type="submit"/>

<%-- Go to this URL if NO errors are found during the ADD TO
CART button processing:--%>
       <dsp:input bean="CartModifierFormHandler.addItemToOrderSuccessURL"
value="/checkout/cart.jsp" type="hidden"/>
     </td>
   </tr>
 </table>
</dsp:form></dsp:getvalueof>








No comments:

Popular Posts