addToCartWorkflow - Medusa Core Workflows Reference
This documentation provides a reference to the addToCartWorkflow
. It belongs to the @medusajs/medusa/core-flows
package.
This workflow adds a product variant to a cart as a line item. It's executed by the Add Line Item Store API Route.
You can use this workflow within your own customizations or custom workflows, allowing you to wrap custom logic around adding an item to the cart. For example, you can use this workflow to add a line item to the cart with a custom price.
Source CodeExamples#
Steps#
Workflow hook
Step conditioned by when
View step details
Input#
AddToCartWorkflowInputDTO & AdditionalData
AddToCartWorkflowInputDTO & AdditionalData
AddToCartWorkflowInputDTO & AdditionalData
AddToCartWorkflowInputDTO & AdditionalDataAddToCartWorkflowInputDTO
AddToCartWorkflowInputDTOThe details of adding items to the cart.
AddToCartWorkflowInputDTO
AddToCartWorkflowInputDTOAdditionalData
AdditionalData
AdditionalData
AdditionalDataHooks#
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
Input
Handlers consuming this hook accept the following input.
input
inputThe input data for the hook.
input
inputinput
AdditionalData
input
AdditionalDatacart
any
cart
anysetPricingContext#
This hook is executed after the cart is retrieved and before the line items are created. You can consume this hook to return any custom context useful for the prices retrieval of the variants to be added to the cart.
For example, assuming you have the following custom pricing rule:
You can consume the setPricingContext
hook to add the location_id
context to the prices calculation:
1import { addToCartWorkflow } from "@medusajs/medusa/core-flows";2import { StepResponse } from "@medusajs/workflows-sdk";3 4addToCartWorkflow.hooks.setPricingContext((5 { cart, variantIds, items, additional_data }, { container }6) => {7 return new StepResponse({8 location_id: "sloc_123", // Special price for in-store purchases9 });10});
The variants' prices will now be retrieved using the context you return.
Example
Input
Handlers consuming this hook accept the following input.
input
inputThe input data for the hook.
input
inputcart
any
cart
anyvariantIds
(undefined | string)[]
variantIds
(undefined | string)[]items
CreateCartCreateLineItemDTO[]
items
CreateCartCreateLineItemDTO[]additional_data
Record<string, unknown> | undefinedAdditional data that can be passed through the additional_data
property in HTTP requests.
Learn more in this documentation.
additional_data
Record<string, unknown> | undefinedadditional_data
property in HTTP requests.
Learn more in this documentation.