Files
medusa-store/www/docs/content/modules/carts-and-checkout/cart.md
Shahed Nasser 914d773d3a api-ref: custom API reference (#4770)
* initialized next.js project

* finished markdown sections

* added operation schema component

* change page metadata

* eslint fixes

* fixes related to deployment

* added response schema

* resolve max stack issue

* support for different property types

* added support for property types

* added loading for components

* added more loading

* type fixes

* added oneOf type

* removed console

* fix replace with push

* refactored everything

* use static content for description

* fixes and improvements

* added code examples section

* fix path name

* optimizations

* fixed tag navigation

* add support for admin and store references

* general enhancements

* optimizations and fixes

* fixes and enhancements

* added search bar

* loading enhancements

* added loading

* added code blocks

* added margin top

* add empty response text

* fixed oneOf parameters

* added path and query parameters

* general fixes

* added base path env variable

* small fix for arrays

* enhancements

* design enhancements

* general enhancements

* fix isRequired

* added enum values

* enhancements

* general fixes

* general fixes

* changed oas generation script

* additions to the introduction section

* added copy button for code + other enhancements

* fix response code block

* fix metadata

* formatted store introduction

* move sidebar logic to Tags component

* added test env variables

* fix code block bug

* added loading animation

* added expand param + loading

* enhance operation loading

* made responsive + improvements

* added loading provider

* fixed loading

* adjustments for small devices

* added sidebar label for endpoints

* added feedback component

* fixed analytics

* general fixes

* listen to scroll for other headings

* added sample env file

* update api ref files + support new fields

* fix for external docs link

* added new sections

* fix last item in sidebar not showing

* move docs content to www/docs

* change redirect url

* revert change

* resolve build errors

* configure rewrites

* changed to environment variable url

* revert changing environment variable name

* add environment variable for API path

* fix links

* fix tailwind settings

* remove vercel file

* reconfigured api route

* move api page under api

* fix page metadata

* fix external link in navigation bar

* update api spec

* updated api specs

* fixed google lint error

* add max-height on request samples

* add padding before loading

* fix for one of name

* fix undefined types

* general fixes

* remove response schema example

* redesigned navigation bar

* redesigned sidebar

* fixed up paddings

* added feedback component + report issue

* fixed up typography, padding, and general styling

* redesigned code blocks

* optimization

* added error timeout

* fixes

* added indexing with algolia + fixes

* fix errors with algolia script

* redesign operation sections

* fix heading scroll

* design fixes

* fix padding

* fix padding + scroll issues

* fix scroll issues

* improve scroll performance

* fixes for safari

* optimization and fixes

* fixes to docs + details animation

* padding fixes for code block

* added tab animation

* fixed incorrect link

* added selection styling

* fix lint errors

* redesigned details component

* added detailed feedback form

* api reference fixes

* fix tabs

* upgrade + fixes

* updated documentation links

* optimizations to sidebar items

* fix spacing in sidebar item

* optimizations and fixes

* fix endpoint path styling

* remove margin

* final fixes

* change margin on small devices

* generated OAS

* fixes for mobile

* added feedback modal

* optimize dark mode button

* fixed color mode useeffect

* minimize dom size

* use new style system

* radius and spacing design system

* design fixes

* fix eslint errors

* added meta files

* change cron schedule

* fix docusaurus configurations

* added operating system to feedback data

* change content directory name

* fixes to contribution guidelines

* revert renaming content

* added api-reference to documentation workflow

* fixes for search

* added dark mode + fixes

* oas fixes

* handle bugs

* added code examples for clients

* changed tooltip text

* change authentication to card

* change page title based on selected section

* redesigned mobile navbar

* fix icon colors

* fix key colors

* fix medusa-js installation command

* change external regex in algolia

* change changeset

* fix padding on mobile

* fix hydration error

* update depedencies
2023-08-15 18:07:54 +03:00

11 KiB
Raw Blame History

description
description
Learn about the Cart entity, its relation to other entities, and how the cart completion process is implemented.

Cart Architecture Overview

In this document, youll 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 Cart entity. Some of the Cart entitys attributes include:

  • email: The email the cart is associated with.
  • type: A string indicating what the cart is used for. Its value can be:
    • default if the cart is used to place an order.
    • swap if the cart is used to create and register a swap
    • draft_order if the cart is used to create and complete a draft order.
    • payment_link if the cart is used for a payment link.
    • claim if the cart is used to create a claim.
  • completed_at: 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_at: 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 Cart entity doesnt hold any details regarding the totals. These are computed and added to the cart instance using the CartService's decorateTotals method. There's also a dedicated method in the CartService, retrieveWithTotals, attaching the totals automatically. It is recommended to use this method by default when you need to retrieve the cart.

The carts 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 carts totals include:

  • shipping_total: The total of the chosen shipping methods, with taxes.
  • shipping_tax_total: The applied taxes on the shipping total.
  • discount_total: The total of the applied discounts.
  • raw_discount_total: The total of the applied discounts without rounding.
  • item_tax_total: The total applied taxes on the carts items.
  • tax_total: The total taxes applied (the sum of shipping_tax_total and item_tax_total).
  • gift_card_total: The total gift card amount applied on the cart. If there are any taxes applied on the gift cards, theyre deducted from the total.
  • gift_card_tax_total: The total taxes applied on the carts gift cards.
  • subtotal: The total of the items without taxes or discounts.
  • total: The overall total of the cart.

:::note

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

:::

The carts totals are retrieved by default in all the carts store APIs.


Cart Completion

The cart completion functionality is implemented inside the strategy cartCompletionStrategy. 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_key.

You can learn more about idempotency keys here.

Cart Completion Process

:::tip

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

:::

Cart Completion Flowchart

The process is implemented as follows:

  1. When the idempotency keys recovery point is set to started, the tax lines are created for the items in the cart. This is done using the CartService's createTaxLines method. If that is completed with no errors, the recovery point is set to tax_lines_created and the process continues.
  2. When the idempotency keys recovery point is set to tax_lines_created, the payment is authorized using the CartService'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 keys recovery point is set to payment_authorized, 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 ProductVariantInventoryService's method confirmInventory. If an item is in stock, the quantity is reserved using the ProductVariantInventoryService'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 type attribute is set to swap), the swap is registered as completed using the SwapService'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 OrderService's method createFromCart. The order is then retrieved and sent in the response.
  4. Once the process detailed above is done, the idempotency keys recovery point is set to finished.

Carts Relation to Other Entities

Region

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

The regions ID is stored in the Cart entity under the attribute region_id. You can also access the region by expanding the region relation and accessing cart.region.

Sales Channel

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

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

The sales channels ID is stored in the sales_channel_id attribute of the Cart entity. You can also access the sales channel by expanding the sales_channel relation and accessing cart.sales_channel.

Address

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

The billing addresss ID is stored in the billing_address_id attribute of the Cart entity. You can also access the billing address by expanding the billing_address relation and accessing cart.billing_address.

The shipping addresss ID is stored in the shipping_address_id attribute of the Cart entity. You can also access the shipping address by expanding the shipping_address relation and accessing cart.shipping_address.

LineItem

Products added to the cart are represented by the LineItem entity. You can access the carts items by expanding the items relation and accessing cart.items.

Discount

Discounts can be added to the cart to deduct the carts total. A discount is represented by the Discount entity.

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

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 GiftCard entity.

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

Customer

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

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

PaymentSession

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

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

You can also access the currently selected payment session through cart.payment_session.

Payment

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

The ID of the payment is stored in the payment_id attribute of the Cart entity. You can also access the payment by expanding the payment relation and accessing cart.payment.

ShippingMethod

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

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


See Also