Skip to main content
Skip to main content

Examples of the Cart Module

In this guide, you’ll find common examples of how you can use the Cart Module in your application.

Create a Cart

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const cart = await cartModuleService.create({
currency_code: "usd",
shipping_address: {
address_1: "1512 Barataria Blvd",
country_code: "us",
},
items: [
{
title: "Shirt",
unit_price: 1000,
quantity: 1,
},
],
})

res.json({ cart })
}

Retrieve Cart by ID

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const cart = await cartModuleService.retrieve("cart_123")

res.json({ cart })
}

Add Item to Cart

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const lineItem = await cartModuleService.addLineItems({
cart_id: "cart_123",
title: "Shirt",
quantity: 2,
unit_price: 5000,
})

res.json({
line_item: lineItem,
})
}

Add Shipping Method to Cart

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const shippingMethod =
await cartModuleService.addShippingMethods({
cart_id: "cart_123",
name: "Custom shipping",
amount: 1000,
})

res.json({
shipping_method: shippingMethod,
})
}

Add Item Adjustment Line

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const itemAdjustment =
await cartModuleService.addLineItemAdjustments({
item_id: "cali_123",
amount: 500,
code: "50%OFF",
})

res.json({
adjustment: itemAdjustment,
})
}

Add Shipping Method Adjustment Line

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

const shippingMethodAdjustment =
await cartModuleService.addShippingMethodAdjustments({
shipping_method_id: "casm_123",
amount: 500,
code: "FREESHIPPING",
})

res.json({
adjustment: shippingMethodAdjustment,
})
}

Remove Line Item from Cart

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

await cartModuleService.deleteLineItems(["cali_123"])

res.status(200)
}

Remove Shipping Method from Cart

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)

await cartModuleService.deleteLineItems(["cali_123"])

res.status(200)
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?