As previously mentioned in this chapter, the ShoppingCart component stores a user’s current and saved shopping carts. You can use the ShoppingCart component’s properties and handle methods to create a new shopping cart or retrieve one of the user’s saved shopping carts and make it the user’s current shopping cart.

The following JSP example illustrates how to create and retrieve shopping carts. In the example, the ShoppingCart.savedEmpty property is checked to determine whether the current user has any saved shopping carts. If the user doesn’t have any saved shopping carts, the user is given the option to create one. If the user has saved shopping carts, the user is given the option to select one of the saved shopping carts to either delete or make the current shopping cart, to delete all of the saved shopping carts, or to create a new shopping cart.

<dsp:importbean bean="/atg/commerce/ShoppingCart"/>

<dsp:form action="shoppingcart.jsp" method="post">
 <dsp:droplet name="/atg/dynamo/droplet/Switch">
   <dsp:param bean="ShoppingCart.savedEmpty" name="value"/>

   <dsp:oparam name="true">
     <!-- since there are no saved carts, we cannot switch to another so
          we only give them the option to create a new cart -->
     <dsp:input bean="ShoppingCart.create" value="Create" type="submit"/>
another shopping cart
   </dsp:oparam>

   <dsp:oparam name="false">
     <!-- We have other shopping carts, so let them do everything -->
     Shopping Cart <dsp:select bean="ShoppingCart.handlerOrderId">
     <dsp:droplet name="ForEach">
       <dsp:param bean="ShoppingCart.saved" name="array"/>
       <dsp:param value="savedcart" name="elementName"/>
       <dsp:oparam name="output">
         <dsp:getvalueof id="option26" param="savedcart.id"
idtype="java.lang.String">
<dsp:option value="<%=option26%>"/>
</dsp:getvalueof><dsp:valueof param="savedcart.id"/>
       </dsp:oparam>
     </dsp:droplet>
     </dsp:select>:
  <dsp:input bean="ShoppingCart.switch" value="Switch" type="submit"/> to,
  <dsp:input bean="ShoppingCart.delete" value="Delete" type="submit"/> or
  <dsp:input bean="ShoppingCart.create" value="Create" type="submit"/>
another shopping cart.<BR>
     <dsp:input bean="ShoppingCart.deleteAll"
value="Delete All Shopping Carts" type="submit"/>
   </dsp:oparam>

 </dsp:droplet>
</dsp:form>
Implementing Order Retrieval

You can also use the OrderLookup servlet bean to retrieve a user’s incomplete orders (that is, shopping carts). OrderLookup enables you to retrieve a single order, all orders assigned to a particular cost center , all orders placed by a particular user, or all orders placed by a particular user that are in specific state, such as INCOMPLETE.

Once the desired shopping cart is moved to ShoppingCart.current, you can use a ForEach servlet bean to iterate over the commerce items in the cart and display them. The following JSP code example illustrates how to do this. In the example, a checkbox is rendered beside each item to allow the user to remove any item from the cart; likewise, a textbox is rendered beside each item to allow the user to modify the quantity of any item.

<droplet bean="ForEach">
  <param name="array" value="bean:ShoppingCart.current.commerceItems">
  <param name="elementName" value="item">
  <oparam name="output">
<tr valign=top>
<td>
  <input type="checkbox" unchecked
    bean="CartModifierFormHandler.removalCatalogRefIds"
    value="param:item.catalogRefId">
</td>
<td>
  <input size=4 name="param:item.catalogRefId"
value="param:item.quantity">
</td>
<td>
  <droplet src="product_fragment.jsp">
  <param name="childProduct" value="param:item.auxiliaryData.productRef">
  <%@ include file="product_fragment.jsp"%>
  </droplet>
  </oparam>
</droplet>

See <ATG11dir>\MotorpriseJSP\j2ee-apps\motorprise\web-app\en\catalog\cart.jsp for an example of similar functionality.


Copyright © 1997, 2019 Oracle and/or its affiliates. All rights reserved. Legal Notices