createCartWorkflow - Medusa Core Workflows Reference

This documentation provides a reference to the createCartWorkflow. It belongs to the @medusajs/medusa/core-flows package.

This workflow creates and returns a cart. You can set the cart's items, region, customer, and other details. This workflow is executed by the Create Cart Store API Route.

This workflow has a hook that allows you to perform custom actions on the created cart. You can see an example in this guide.

You can also use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around cart creation.

Source Code

Examples#

Steps#

Input#

CreateCartWorkflowInputCreateCartWorkflowInput
The data to create the cart, along with custom data that's passed to the workflow's hooks.
region_idstringOptional
The ID of the region that the cart belongs to.
customer_idstringOptional
The ID of the customer associated with the cart.
sales_channel_idstringOptional
The ID of the sales channel through which the cart is created.
emailstringOptional
The email address of the cart's customer.
currency_codestringOptional
The currency code of the cart. This defaults to the region's currency code.
shipping_address_idstringOptional
The ID of the associated shipping address.
billing_address_idstringOptional
The ID of the associated billing address.
shipping_addressstring | CreateCartAddressDTOOptional
The ID of an existing shipping address, or the details of a shipping address to create.
billing_addressstring | CreateCartAddressDTOOptional
The ID of an existing billing address, or the details of a billing address to create.
metadataRecord<string, unknown>Optional
Custom key-value pairs related to the cart.
The items to be added to the cart.
promo_codesstring[]Optional
The promotional codes applied on the cart.
additional_dataRecord<string, unknown>Optional
Additional data that can be passed through the additional_data property in HTTP requests. Learn more in this documentation.

Output#

CartDTOCartDTO
The cart details.
idstring
The ID of the cart.
currency_codestring
The currency of the cart
original_item_totalBigNumberValue
The original item total of the cart.
original_item_subtotalBigNumberValue
The original item subtotal of the cart.
original_item_tax_totalBigNumberValue
The original item tax total of the cart.
item_totalBigNumberValue
The item total of the cart.
item_subtotalBigNumberValue
The item subtotal of the cart.
item_tax_totalBigNumberValue
The item tax total of the cart.
original_totalBigNumberValue
The original total of the cart.
original_subtotalBigNumberValue
The original subtotal of the cart.
original_tax_totalBigNumberValue
The original tax total of the cart.
The total of the cart.
The subtotal of the cart. (Excluding taxes)
The tax total of the cart.
discount_totalBigNumberValue
The discount total of the cart.
discount_tax_totalBigNumberValue
The discount tax total of the cart.
gift_card_totalBigNumberValue
The gift card total of the cart.
gift_card_tax_totalBigNumberValue
The gift card tax total of the cart.
shipping_totalBigNumberValue
The shipping total of the cart.
shipping_subtotalBigNumberValue
The shipping subtotal of the cart.
shipping_tax_totalBigNumberValue
The shipping tax total of the cart.
original_shipping_totalBigNumberValue
The original shipping total of the cart.
original_shipping_subtotalBigNumberValue
The original shipping subtotal of the cart.
original_shipping_tax_totalBigNumberValue
The original shipping tax total of the cart.
raw_original_item_totalBigNumberRawValue
The raw original item total of the cart.
raw_original_item_subtotalBigNumberRawValue
The raw original item subtotal of the cart.
raw_original_item_tax_totalBigNumberRawValue
The raw original item tax total of the cart.
raw_item_totalBigNumberRawValue
The raw item total of the cart.
raw_item_subtotalBigNumberRawValue
The raw item subtotal of the cart.
raw_item_tax_totalBigNumberRawValue
The raw item tax total of the cart.
raw_original_totalBigNumberRawValue
The raw original total of the cart.
raw_original_subtotalBigNumberRawValue
The raw original subtotal of the cart.
raw_original_tax_totalBigNumberRawValue
The raw original tax total of the cart.
The raw total of the cart.
raw_subtotalBigNumberRawValue
The raw subtotal of the cart. (Excluding taxes)
raw_tax_totalBigNumberRawValue
The raw tax total of the cart.
raw_discount_totalBigNumberRawValue
The raw discount total of the cart.
raw_discount_tax_totalBigNumberRawValue
The raw discount tax total of the cart.
raw_gift_card_totalBigNumberRawValue
The raw gift card total of the cart.
raw_gift_card_tax_totalBigNumberRawValue
The raw gift card tax total of the cart.
raw_shipping_totalBigNumberRawValue
The raw shipping total of the cart.
raw_shipping_subtotalBigNumberRawValue
The raw shipping subtotal of the cart.
raw_shipping_tax_totalBigNumberRawValue
The raw shipping tax total of the cart.
raw_original_shipping_totalBigNumberRawValue
The raw original shipping total of the cart.
raw_original_shipping_subtotalBigNumberRawValue
The raw original shipping subtotal of the cart.
raw_original_shipping_tax_totalBigNumberRawValue
The raw original shipping tax total of the cart.
region_idstringOptional
The ID of the region the cart belongs to.
customer_idstringOptional
The ID of the associated customer
sales_channel_idstringOptional
The ID of the sales channel the cart belongs to.
emailstringOptional
The email of the customer that owns the cart.
shipping_addressCartAddressDTOOptional
The associated shipping address.
billing_addressCartAddressDTOOptional
The associated billing address.
itemsCartLineItemDTO[]Optional
The associated line items.
shipping_methodsCartShippingMethodDTO[]Optional
The associated shipping methods
metadatanull | Record<string, unknown>Optional
Holds custom data in key-value pairs.
completed_atstring | DateOptional
When the cart was completed.
created_atstring | DateOptional
When the cart was created.
updated_atstring | DateOptional
When the cart was updated.

Hooks#

Hooks allow you to inject custom functionalities into the workflow. You'll receive data from the workflow, as well as additional data sent through an HTTP request.

Learn more about Hooks and Additional Data.

validate#

This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.

Example

Code
1import { createCartWorkflow } from "@medusajs/medusa/core-flows"2
3createCartWorkflow.hooks.validate(4  (async ({ input, cart }, { container }) => {5    //TODO6  })7)

Input

Handlers consuming this hook accept the following input.

inputobject
The input data for the hook.
inputobject
cartobject

cartCreated#

This hook is executed after a cart is created. You can consume this hook to perform custom actions on the created cart.

Example

Code
1import { createCartWorkflow } from "@medusajs/medusa/core-flows"2
3createCartWorkflow.hooks.cartCreated(4  (async ({ cart, additional_data }, { container }) => {5    //TODO6  })7)

Input

Handlers consuming this hook accept the following input.

inputobject
The input data for the hook.
The cart details.
additional_dataRecord<string, unknown> | undefined
Additional data that can be passed through the additional\_data property in HTTP requests. Learn more in this documentation.
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break