In addition to containing items a customer wants to buy, the shopping cart domain object acts like an order form that contains or calculates most of the information about the order the customer is assembling as they shop and check out.
Key classes and files
Classes related to shopping cart are found in the com.elasticpath.service.shoppingcart package in the /commerce-engine/ep-core module.
- ShoppingCart - Represents the customer's shopping cart.
- CartDirector - Responsible for adding and updating items in and persisting the shopping cart.
- ShoppingItem - Represents a quantity of an item (SKU) in a shopping cart, wish list, order, etc.
- CartItem - Subclass of ShoppingItem representing a quantity of a SKU in a shopping cart.
The products a customer wants to buy are kept in the ShoppingCart as a collection of CartItem objects. A CartItem represents a quantity of a specific product SKU that is to be purchased. A CartItem can also be thought of as a single row or line item on an order form. A CartItem can have "child" cart items that are dependent on it. In this case, the items are to be displayed together and the child cart item cannot exist in the cart independently of its parent. Therefore, child cart items must be removed when the parent cart item is removed. The ShoppingCart contains methods for adding and removing CartItem objects. Dependent cart items typically arise from the targets of ProductAssociation objects, and the cart contains a convenience method for adding a cart item from a product association.
A major responsibility of the ShoppingCart is to compute values derived from the set of CartItem objects. Examples include the subtotal, total, taxes, and shipping costs.
All shopping carts are persisted. After an item is added or removed, clients should request that the ShoppingCartService update the record of the shopping cart. However, only the cart items are saved. Upon retrieval from storage, several values may be recalculated, but selections such as the shipping method and billing address are no longer available. Therefore, a customer can continue shopping with a newly retrieved saved cart, but the checkout process must be restarted during each session.
Wish list items are stored in the user's WishList as a collection of ShoppingItem objects. A wish list item's quantity is always equal to one. Wish list items are added to the wish list via the addSkuToWishList() method of in theCartDirector.
The WishListService is responsible for sending the wish list to others via the EmailService. When a shopper views a WishList, the system uses the viweWishList.vm file to render it.
Shopping cart and add-to-cart functionality are validated by classes in the com.elasticpath.service.shoppingcart.validation package. These validators all extend the Validator interface and return a collection of StructuredErrorMessage objects.
For more information on what validations are performed, see Add-to-cart and purchase validation.