ART#305 - How to add single item to cart in ATG?

Now that we know the pre-requisites of adding an item to cart, we can go on and proceed to understand the concept of /atg/commerce/order/purchase/CartModifierFormHandler. This FormHandler is responsible for most of the cart operations. Add/Remove/Update, you name it and CartModifierFormHandler does that!

Since we will discuss the topic of adding and item to cart, we should first understand what CartModifierFormHandler consists of, and how do we use its properties.
Before we proceed with this topic, i would strongly suggest to go through the topic on the basics of FormHandlers HERE. Even if you have read it, please do a quick read to understand this topic better. 

What does CartModifierFormHandler contain?
From the perspective of Add To Cart, CartModifierFormHandler contains the following:-

1. handleAddItemToOrder: This method is present out of the box, and is called to add item to cart. It does not matter if we are adding multiple items to cart in one go or single item to cart, this is our method!

2. getItems() method: This method returns an array of type AddCommerceItemInfo. This class [AddCommerceItemInfo] is present OOTB, and is a property-holder for all the details we need to add an item to cart.
AddCommerceItemInfo contains a lot of properties. The properties required for the operation of adding to cart are listed below:-
  • catalogRefId: This is the SKU-ID which you need to add to cart.
  • productId: ID of parent product of above SKU.
  • quantity: The quantity of this item, this can by any positive integer.
  • commerceItemType: The value of this field will be the string "default" [WHAT? WHY? HOW? - We will discuss this later]
3. addItemCount: Number of different items to be added to cart. Possible values:-
For adding single item to cart: 1
For adding multiple items to cart: any positive integer > 1

See below screenshot for clarity


NOTE: Do not confuse this property with "quantity" property in AddCommerceItemInfo.

Difference Between "quantity" and "addItemCount"

Consider the following analogies:

SCENARIO#1: Suppose you go to a store and see a pair of Jeans. You like that so much, that you decide to buy two pairs of exactly same Jeans. Now, since you are buying two pairs of same item;
addItemCount = 1 [One unique item]
quantity = 2 [multiple quantity for a single item]

SCENARIO#2: You go to the same store and buy the same jeans, with 2 quantity. Now, you also like a T-Shirt, and you want to buy one. Now, the property values in this scenario will be:-
addItemCount = 2 [Two unique items]
For Jeans: quantity = 2 [multiple quantity for item1]
For T-Shirt: quantity = 1 [single quantity for item2]

Adding Single Item To Cart

CartModifierFormHandler is present OOTB. We can surely customize it like any other form handler, but we will keep that discussion for a later time. OOTB CartModifierFormHandler is fully capable of doing the whole cart operation by itself.
Let us assume that we will use OOTB CartModifierFormHandler [we will discuss the customization later].
Let us have a look at below JSP snippet:-

STEP#1: Import the component -  CartModifierFormHandler. Just like any other formhandler, this is a request-scoped component. This can also be extended like other formhandlers.

STEP#2: Since we are adding single item to cart, we set the property "addItemCount" property of CartModifierFormHandler will be "1". Since we want this property to be unchangeable, we set this <dsp:input> field as hidden.

STEP#3: Next, we set the relevant details in the AddCommerceItemInfo[] array. Again, since we are adding a single unique item to cart [which may have single or multiple quantity], we use the index - 0 of the array. [CartModifierFormHandler.getItems[], can be used directly in JSP using CartModifierFormHandler.items[], this is the capability of ATG's OOTB Dynamic Beans concept]
At Index Zero, we set
  • quantity: quantity of item to be added [any positive integer]
  • catalogRefId: sku id we wish to add
  • productId: ID of parent product for above SKU
  • commerceItemType: the string "default". [We will discuss details later]

STEP#4: Create a submit button referring to CartModifierFormHandler's handleAddItemToOrder method. When this method is called, the OOTB flow starts and adds the item to cart.

Adding an item to cart was a piece of cake! Lets move on and understand how we can add multiple items to cart. We will also discuss the concept of commerceItems and what to extend in CartModifierFormHandler!





Get All The Latest Updates Delivered Straight Into Your Inbox For Free!