Skip to main content

Cart Architecture Overview

In this document, you’ll learn about the Cart entity, its relation to other entities, and how the cart completion strategy is implemented.

Overview

The cart allows customers to go from browsing products to completing an order. Many actions can be performed on a cart, like adding line items, applying discounts, creating shipping methods, and more before eventually completing it and creating an Order.


Cart Entity Overview

A cart is represented by the CartCopy to Clipboard entity. Some of the CartCopy to Clipboard entity’s attributes include:

  • emailCopy to Clipboard: The email the cart is associated with.
  • typeCopy to Clipboard: A string indicating what the cart is used for. Its value can be:
    • defaultCopy to Clipboard if the cart is used to place an order.
    • swapCopy to Clipboard if the cart is used to create and register a swap
    • draft_orderCopy to Clipboard if the cart is used to create and complete a draft order.
    • payment_linkCopy to Clipboard if the cart is used for a payment link.
    • claimCopy to Clipboard if the cart is used to create a claim.
  • completed_atCopy to Clipboard: the date the cart was completed. A completed cart means that it has been used for its main purpose. For example, if the cart is used to place an order, then a completed cart means that the order was placed.
  • payment_authorized_atCopy to Clipboard: the date a payment was authorized.

There are other important attributes discussed in later sections. Check out the full Cart entity in the entities reference.


Cart Totals Calculation

By default, the CartCopy to Clipboard entity doesn’t hold any details regarding the totals. These are computed and added to the cart instance using the CartServiceCopy to Clipboard's decorateTotals method. There's also a dedicated method in the CartServiceCopy to Clipboard, retrieveWithTotals, attaching the totals automatically. It is recommended to use this method by default when you need to retrieve the cart.

The cart’s totals are calculated based on the content and context of the cart. This includes the selected region, whether tax-inclusive pricing is enabled, the chosen shipping methods, and more.

The calculated cart’s totals include:

  • shipping_totalCopy to Clipboard: The total of the chosen shipping methods, with taxes.
  • shipping_tax_totalCopy to Clipboard: The applied taxes on the shipping total.
  • discount_totalCopy to Clipboard: The total of the applied discounts.
  • raw_discount_totalCopy to Clipboard: The total of the applied discounts without rounding.
  • item_tax_totalCopy to Clipboard: The total applied taxes on the cart’s items.
  • tax_totalCopy to Clipboard: The total taxes applied (the sum of shipping_tax_totalCopy to Clipboard and item_tax_totalCopy to Clipboard).
  • gift_card_totalCopy to Clipboard: The total gift card amount applied on the cart. If there are any taxes applied on the gift cards, they’re deducted from the total.
  • gift_card_tax_totalCopy to Clipboard: The total taxes applied on the cart’s gift cards.
  • subtotalCopy to Clipboard: The total of the items without taxes or discounts.
  • totalCopy to Clipboard: The overall total of the cart.

If you have tax-inclusive pricing enabled, you can learn about other available total fields here.

The cart’s totals are retrieved by default in all the cart’s store APIs.


Cart Completion

The cart completion functionality is implemented inside the strategy cartCompletionStrategyCopy to Clipboard. This allows you to customize how the process is implemented in your store.

You can initiate the cart completion process by sending a request to the Complete Cart endpoint.

Idempotency Key

An Idempotency Key is a unique key associated with a cart. It is generated when the cart completion process is started and can be used to retry cart completion safely if an error occurs. The idempotency key is stored in the Cart entity under the attribute idempotency_keyCopy to Clipboard.

You can learn more about idempotency keys here.

Cart Completion Process

You can learn how to override the cart completion strategy here.

Cart Completion Flowchart

The process is implemented as follows:

  1. When the idempotency key’s recovery point is set to startedCopy to Clipboard, the tax lines are created for the items in the cart. This is done using the CartServiceCopy to Clipboard's createTaxLines method. If that is completed with no errors, the recovery point is set to tax_lines_createdCopy to Clipboard and the process continues.
  2. When the idempotency key’s recovery point is set to tax_lines_createdCopy to Clipboard, the payment is authorized using the CartServiceCopy to Clipboard's method authorizePayment. If the payment requires more action or is pending authorization, then the tax lines that were created in the previous steps are deleted and the cart completion process is terminated. Once the payment is authorized, the process can be restarted.
  3. When the idempotency key’s recovery point is set to payment_authorizedCopy to Clipboard, tax lines are created again the same way as the first step. Then, the inventory of each of the items in the cart is confirmed using the ProductVariantInventoryServiceCopy to Clipboard's method confirmInventory. If an item is in stock, the quantity is reserved using the ProductVariantInventoryServiceCopy to Clipboard's method reserveQuantity. If an item is out of stock, any item reservations that were created are deleted, the payment is canceled, and an error is thrown, terminating the cart completion process. If all item quantities are confirmed to be available:
    1. If the cart belongs to a swap (the typeCopy to Clipboard attribute is set to swapCopy to Clipboard), the swap is registered as completed using the SwapServiceCopy to Clipboard's registerCartCompletion method and the inventory item reservations are removed using the Inventory module. The process ends successfully here for a swap.
    2. If the cart belongs to an order, the order is created using the OrderServiceCopy to Clipboard's method createFromCart. The order is then retrieved and sent in the response.
  4. Once the process detailed above is done, the idempotency key’s recovery point is set to finishedCopy to Clipboard.

Cart’s Relation to Other Entities

Region

A cart is associated with a region, which is represented by the RegionCopy to Clipboard entity. This ensures the prices, discounts, and other conditions that depend on the region are accurate for a customer.

The region’s ID is stored in the CartCopy to Clipboard entity under the attribute region_idCopy to Clipboard. You can also access the region by expanding the regionCopy to Clipboard relation and accessing cart.regionCopy to Clipboard.

Sales Channel

A sales channel indicates different selling points a business offers its products in. It is represented by the SalesChannelCopy to Clipboard entity.

A cart can be associated with a sales channel. When adding products to the cart, it’s important that the cart and the product belong to the same sales channel.

The sales channel’s ID is stored in the sales_channel_idCopy to Clipboard attribute of the CartCopy to Clipboard entity. You can also access the sales channel by expanding the sales_channelCopy to Clipboard relation and accessing cart.sales_channelCopy to Clipboard.

Address

A cart can have a shipping and a billing address, both represented by the AddressCopy to Clipboard entity.

The billing address’s ID is stored in the billing_address_idCopy to Clipboard attribute of the CartCopy to Clipboard entity. You can also access the billing address by expanding the billing_addressCopy to Clipboard relation and accessing cart.billing_addressCopy to Clipboard.

The shipping address’s ID is stored in the shipping_address_idCopy to Clipboard attribute of the CartCopy to Clipboard entity. You can also access the shipping address by expanding the shipping_addressCopy to Clipboard relation and accessing cart.shipping_addressCopy to Clipboard.

LineItem

Products added to the cart are represented by the LineItemCopy to Clipboard entity. You can access the cart’s items by expanding the itemsCopy to Clipboard relation and accessing cart.itemsCopy to Clipboard.

Discount

Discounts can be added to the cart to deduct the cart’s total. A discount is represented by the DiscountCopy to Clipboard entity.

You can access the discounts applied on a cart by expanding the discountsCopy to Clipboard relation and accessing cart.discountsCopy to Clipboard.

GiftCard

Gift cards can be applied on a cart to benefit from a pre-paid balance during payment. A gift card is represented by the GiftCardCopy to Clipboard entity.

You can access the gift cards applied on a cart by expanding the gift_cardsCopy to Clipboard relation and accessing cart.gift_cardsCopy to Clipboard.

Customer

A cart can be associated with either a logged-in customer or a guest customer, both represented by the CustomerCopy to Clipboard entity.

You can access the customer by expanding the customerCopy to Clipboard relation and accessing cart.customerCopy to Clipboard.

PaymentSession

A payment session is an available payment method that the customer can use during the checkout process. It is represented by the PaymentSessionCopy to Clipboard entity.

A cart can have multiple payment sessions that the customer can choose from. You can access the payment sessions by expanding the payment_sessionsCopy to Clipboard relation and accessing cart.payment_sessionsCopy to Clipboard.

You can also access the currently selected payment session through cart.payment_sessionCopy to Clipboard.

Payment

A payment is the authorized amount required to complete the cart. It is represented by the PaymentCopy to Clipboard entity.

The ID of the payment is stored in the payment_idCopy to Clipboard attribute of the CartCopy to Clipboard entity. You can also access the payment by expanding the paymentCopy to Clipboard relation and accessing cart.paymentCopy to Clipboard.

ShippingMethod

A shipping method indicates the chosen shipping method used to fulfill the order created later. It is represented by the ShippingMethodCopy to Clipboard entity.

A cart can have more than one shipping method. You can access the shipping methods by expanding the shipping_methodsCopy to Clipboard relation and accessing cart.shipping_methodsCopy to Clipboard.


See Also

Was this page helpful?