> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/medusajs/medusa/llms.txt
> Use this file to discover all available pages before exploring further.

# Cart Module

> Learn about the Cart Module and how to manage shopping carts, line items, and cart totals in Medusa.

## Overview

The Cart Module manages shopping carts and their associated data including line items, shipping methods, addresses, and real-time total calculations. It provides the foundation for the checkout process.

**Key Features:**

* Cart and line item management
* Real-time total calculations
* Tax line and adjustment tracking
* Shipping method integration
* Cart completion tracking
* Credit lines for store credit
* Multi-currency support

## When to Use

Use the Cart Module when you need to:

* Create and manage shopping carts
* Add/remove products to carts
* Calculate cart totals with taxes and discounts
* Track shipping methods and costs
* Store customer shipping and billing addresses
* Handle cart-to-order conversion
* Support guest and registered customer carts

## Data Models

### Cart

The core cart entity representing a shopping session.

<ResponseField name="id" type="string" required>
  Unique cart identifier (prefix: `cart_`)
</ResponseField>

<ResponseField name="region_id" type="string">
  ID of the associated region
</ResponseField>

<ResponseField name="customer_id" type="string">
  ID of the customer (null for guest carts)
</ResponseField>

<ResponseField name="sales_channel_id" type="string">
  ID of the sales channel
</ResponseField>

<ResponseField name="email" type="string">
  Customer email address
</ResponseField>

<ResponseField name="currency_code" type="string" required>
  Three-letter ISO currency code
</ResponseField>

<ResponseField name="locale" type="string">
  BCP 47 language tag (e.g., "en-US")
</ResponseField>

<ResponseField name="completed_at" type="DateTime">
  Timestamp when cart was completed (converted to order)
</ResponseField>

<ResponseField name="items" type="LineItem[]">
  Products added to the cart
</ResponseField>

<ResponseField name="shipping_address" type="Address">
  Shipping address for the cart
</ResponseField>

<ResponseField name="billing_address" type="Address">
  Billing address for the cart
</ResponseField>

<ResponseField name="shipping_methods" type="ShippingMethod[]">
  Selected shipping methods
</ResponseField>

<ResponseField name="credit_lines" type="CreditLine[]">
  Store credit applied to the cart
</ResponseField>

### LineItem

Represents a product in the cart.

<ResponseField name="id" type="string" required>
  Unique line item identifier
</ResponseField>

<ResponseField name="cart_id" type="string" required>
  ID of the parent cart
</ResponseField>

<ResponseField name="title" type="string" required>
  Product title
</ResponseField>

<ResponseField name="subtitle" type="string">
  Product subtitle or variant title
</ResponseField>

<ResponseField name="thumbnail" type="string">
  Product thumbnail URL
</ResponseField>

<ResponseField name="quantity" type="number" required>
  Item quantity
</ResponseField>

<ResponseField name="product_id" type="string">
  ID of the product
</ResponseField>

<ResponseField name="variant_id" type="string">
  ID of the product variant
</ResponseField>

<ResponseField name="unit_price" type="BigNumber" required>
  Price per unit before discounts
</ResponseField>

<ResponseField name="is_discountable" type="boolean">
  Whether discounts can be applied
</ResponseField>

<ResponseField name="is_tax_inclusive" type="boolean">
  Whether price includes tax
</ResponseField>

<ResponseField name="tax_lines" type="LineItemTaxLine[]">
  Tax lines applied to this item
</ResponseField>

<ResponseField name="adjustments" type="LineItemAdjustment[]">
  Discount adjustments applied to this item
</ResponseField>

<ResponseField name="metadata" type="object">
  Additional custom data
</ResponseField>

### ShippingMethod

Represents a shipping option for the cart.

<ResponseField name="id" type="string" required>
  Unique shipping method identifier
</ResponseField>

<ResponseField name="cart_id" type="string" required>
  ID of the parent cart
</ResponseField>

<ResponseField name="name" type="string" required>
  Shipping method name
</ResponseField>

<ResponseField name="amount" type="BigNumber" required>
  Shipping cost before discounts
</ResponseField>

<ResponseField name="is_tax_inclusive" type="boolean">
  Whether amount includes tax
</ResponseField>

<ResponseField name="shipping_option_id" type="string">
  Reference to the fulfillment shipping option
</ResponseField>

<ResponseField name="data" type="object">
  Provider-specific data
</ResponseField>

<ResponseField name="tax_lines" type="ShippingMethodTaxLine[]">
  Tax lines applied to shipping
</ResponseField>

<ResponseField name="adjustments" type="ShippingMethodAdjustment[]">
  Discount adjustments applied to shipping
</ResponseField>

### Address

Shipping or billing address for the cart.

<ResponseField name="id" type="string" required>
  Unique address identifier
</ResponseField>

<ResponseField name="customer_id" type="string">
  Associated customer ID
</ResponseField>

<ResponseField name="first_name" type="string">
  First name
</ResponseField>

<ResponseField name="last_name" type="string">
  Last name
</ResponseField>

<ResponseField name="phone" type="string">
  Phone number
</ResponseField>

<ResponseField name="company" type="string">
  Company name
</ResponseField>

<ResponseField name="address_1" type="string">
  Address line 1
</ResponseField>

<ResponseField name="address_2" type="string">
  Address line 2
</ResponseField>

<ResponseField name="city" type="string">
  City
</ResponseField>

<ResponseField name="country_code" type="string">
  Two-letter ISO country code
</ResponseField>

<ResponseField name="province" type="string">
  State or province
</ResponseField>

<ResponseField name="postal_code" type="string">
  Postal/ZIP code
</ResponseField>

## Calculated Totals

The Cart Module automatically calculates totals when specific fields are requested. These are not stored but computed on-demand:

<ResponseField name="total" type="BigNumber">
  Final cart total after all adjustments and taxes
</ResponseField>

<ResponseField name="subtotal" type="BigNumber">
  Sum of line item subtotals before shipping
</ResponseField>

<ResponseField name="tax_total" type="BigNumber">
  Total tax amount
</ResponseField>

<ResponseField name="discount_total" type="BigNumber">
  Total discount amount
</ResponseField>

<ResponseField name="shipping_total" type="BigNumber">
  Total shipping cost after discounts and taxes
</ResponseField>

<ResponseField name="item_total" type="BigNumber">
  Sum of all line item totals
</ResponseField>

<ResponseField name="item_tax_total" type="BigNumber">
  Total tax on line items
</ResponseField>

<ResponseField name="shipping_tax_total" type="BigNumber">
  Total tax on shipping
</ResponseField>

## Service Interface

The Cart Module service is available at `@medusajs/medusa/cart`.

### Retrieve Cart

Retrieve a single cart with totals.

<CodeGroup>
  ```typescript Example theme={null}
  import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
  import { ICartModuleService } from "@medusajs/framework/types"
  import { Modules } from "@medusajs/framework/utils"

  export async function GET(
    req: MedusaRequest,
    res: MedusaResponse
  ) {
    const cartModuleService: ICartModuleService = req.scope.resolve(
      Modules.CART
    )

    const cart = await cartModuleService.retrieveCart("cart_123", {
      select: [
        "id",
        "email",
        "currency_code",
        "total",
        "subtotal",
        "tax_total",
      ],
      relations: [
        "items",
        "items.tax_lines",
        "items.adjustments",
        "shipping_methods",
        "shipping_address",
      ],
    })

    res.json({ cart })
  }
  ```
</CodeGroup>

<ParamField path="cartId" type="string" required>
  The ID of the cart to retrieve
</ParamField>

<ParamField path="config" type="FindConfig">
  Configuration for the query

  <ParamField path="select" type="string[]">
    Fields to select (include total fields to calculate them)
  </ParamField>

  <ParamField path="relations" type="string[]">
    Relations to load. Required relations for totals: `items`, `items.tax_lines`, `items.adjustments`, `shipping_methods`, `shipping_methods.tax_lines`, `shipping_methods.adjustments`
  </ParamField>
</ParamField>

<ResponseField name="cart" type="CartDTO">
  The retrieved cart with calculated totals
</ResponseField>

### List Carts

List carts with filtering.

<CodeGroup>
  ```typescript Example theme={null}
  const carts = await cartModuleService.listCarts(
    {
      customer_id: "cus_123",
      completed_at: null,
    },
    {
      relations: ["items"],
      take: 10,
    }
  )
  ```
</CodeGroup>

<ParamField path="filters" type="FilterableCartProps">
  Filters to apply

  <ParamField path="id" type="string | string[]">
    Filter by cart IDs
  </ParamField>

  <ParamField path="customer_id" type="string | string[]">
    Filter by customer IDs
  </ParamField>

  <ParamField path="sales_channel_id" type="string | string[]">
    Filter by sales channel IDs
  </ParamField>

  <ParamField path="email" type="string">
    Filter by email
  </ParamField>

  <ParamField path="completed_at" type="null | object">
    Filter by completion status (null for active carts)
  </ParamField>
</ParamField>

### Create Carts

Create a new shopping cart.

<CodeGroup>
  ```typescript Example theme={null}
  const cart = await cartModuleService.createCarts({
    region_id: "reg_us",
    customer_id: "cus_123",
    email: "customer@example.com",
    currency_code: "usd",
    sales_channel_id: "sc_web",
    items: [
      {
        title: "Medusa T-Shirt",
        subtitle: "Size: Large",
        product_id: "prod_123",
        variant_id: "variant_456",
        quantity: 2,
        unit_price: 2000,
      },
    ],
  })
  ```
</CodeGroup>

<ParamField path="data" type="CreateCartDTO | CreateCartDTO[]" required>
  Cart data to create

  <ParamField path="region_id" type="string">
    ID of the region
  </ParamField>

  <ParamField path="customer_id" type="string">
    ID of the customer (omit for guest cart)
  </ParamField>

  <ParamField path="email" type="string">
    Customer email
  </ParamField>

  <ParamField path="currency_code" type="string" required>
    Three-letter ISO currency code
  </ParamField>

  <ParamField path="sales_channel_id" type="string">
    ID of the sales channel
  </ParamField>

  <ParamField path="items" type="CreateLineItemDTO[]">
    Initial line items
  </ParamField>
</ParamField>

<ResponseField name="cart" type="CartDTO | CartDTO[]">
  The created cart(s)
</ResponseField>

### Update Carts

Update cart information.

<CodeGroup>
  ```typescript Example theme={null}
  const cart = await cartModuleService.updateCarts("cart_123", {
    email: "newemail@example.com",
    region_id: "reg_eu",
    currency_code: "eur",
  })
  ```
</CodeGroup>

<ParamField path="cartId" type="string" required>
  The ID of the cart to update
</ParamField>

<ParamField path="data" type="UpdateCartDTO" required>
  Cart data to update
</ParamField>

### Create Line Items

Add products to a cart.

<CodeGroup>
  ```typescript Example theme={null}
  const lineItem = await cartModuleService.createLineItems({
    cart_id: "cart_123",
    title: "Medusa Hoodie",
    subtitle: "Size: Medium, Color: Black",
    product_id: "prod_789",
    variant_id: "variant_101",
    quantity: 1,
    unit_price: 5000,
  })
  ```
</CodeGroup>

<ParamField path="data" type="CreateLineItemDTO | CreateLineItemDTO[]" required>
  Line item data

  <ParamField path="cart_id" type="string" required>
    ID of the cart
  </ParamField>

  <ParamField path="title" type="string" required>
    Product title
  </ParamField>

  <ParamField path="product_id" type="string">
    ID of the product
  </ParamField>

  <ParamField path="variant_id" type="string">
    ID of the product variant
  </ParamField>

  <ParamField path="quantity" type="number" required>
    Item quantity
  </ParamField>

  <ParamField path="unit_price" type="number" required>
    Price per unit
  </ParamField>
</ParamField>

### Update Line Items

Update cart line items (e.g., change quantity).

<CodeGroup>
  ```typescript Example theme={null}
  const lineItem = await cartModuleService.updateLineItems("item_123", {
    quantity: 3,
  })
  ```
</CodeGroup>

### Delete Line Items

Remove items from the cart.

<CodeGroup>
  ```typescript Example theme={null}
  await cartModuleService.deleteLineItems(["item_123", "item_456"])
  ```
</CodeGroup>

### Create Shipping Methods

Add shipping method to cart.

<CodeGroup>
  ```typescript Example theme={null}
  const shippingMethod = await cartModuleService.createShippingMethods({
    cart_id: "cart_123",
    name: "Standard Shipping",
    amount: 500,
    shipping_option_id: "so_standard",
  })
  ```
</CodeGroup>

<ParamField path="data" type="CreateShippingMethodDTO | CreateShippingMethodDTO[]" required>
  Shipping method data

  <ParamField path="cart_id" type="string" required>
    ID of the cart
  </ParamField>

  <ParamField path="name" type="string" required>
    Shipping method name
  </ParamField>

  <ParamField path="amount" type="number" required>
    Shipping cost
  </ParamField>

  <ParamField path="shipping_option_id" type="string">
    Reference to fulfillment shipping option
  </ParamField>
</ParamField>

### Add Addresses

Set shipping and billing addresses.

<CodeGroup>
  ```typescript Example theme={null}
  const cart = await cartModuleService.updateCarts("cart_123", {
    shipping_address: {
      first_name: "John",
      last_name: "Doe",
      address_1: "123 Main St",
      city: "New York",
      country_code: "us",
      postal_code: "10001",
    },
    billing_address: {
      first_name: "John",
      last_name: "Doe",
      address_1: "456 Billing Ave",
      city: "New York",
      country_code: "us",
      postal_code: "10002",
    },
  })
  ```
</CodeGroup>

## Integration Examples

### With Product Module

Add products to cart by retrieving product data.

<CodeGroup>
  ```typescript Example theme={null}
  import { Modules } from "@medusajs/framework/utils"

  const productModule = container.resolve(Modules.PRODUCT)
  const cartModule = container.resolve(Modules.CART)

  // Retrieve product with variant
  const product = await productModule.retrieveProduct("prod_123", {
    relations: ["variants"],
  })

  const variant = product.variants[0]

  // Add to cart
  await cartModule.createLineItems({
    cart_id: "cart_123",
    title: product.title,
    subtitle: variant.title,
    product_id: product.id,
    variant_id: variant.id,
    quantity: 1,
    unit_price: 2000, // From pricing module
  })
  ```
</CodeGroup>

### With Pricing Module

Calculate prices before adding to cart.

<CodeGroup>
  ```typescript Example theme={null}
  import { Modules } from "@medusajs/framework/utils"

  const pricingModule = container.resolve(Modules.PRICING)

  // Calculate price for variant
  const priceSet = await pricingModule.calculatePrices(
    { id: ["variant_456"] },
    {
      context: {
        currency_code: "usd",
        region_id: "reg_us",
      },
    }
  )

  const price = priceSet[0].calculated_amount

  // Add to cart with calculated price
  await cartModule.createLineItems({
    cart_id: "cart_123",
    variant_id: "variant_456",
    quantity: 1,
    unit_price: price,
    // ...
  })
  ```
</CodeGroup>

### With Promotion Module

Apply promotions as adjustments.

<CodeGroup>
  ```typescript Example theme={null}
  import { Modules } from "@medusajs/framework/utils"

  const promotionModule = container.resolve(Modules.PROMOTION)

  // Compute promotions
  const promotions = await promotionModule.computeActions(
    ["promo_123"],
    {
      items: cart.items,
    }
  )

  // Apply as line item adjustments
  for (const action of promotions.actions) {
    await cartModule.createLineItemAdjustments({
      item_id: action.item_id,
      amount: -action.amount,
      code: action.code,
    })
  }
  ```
</CodeGroup>

### With Tax Module

Calculate and apply tax lines.

<CodeGroup>
  ```typescript Example theme={null}
  import { Modules } from "@medusajs/framework/utils"

  const taxModule = container.resolve(Modules.TAX)

  // Calculate taxes
  const taxLines = await taxModule.getTaxLines(
    cart.items,
    {
      address: cart.shipping_address,
    }
  )

  // Apply tax lines to items
  for (const taxLine of taxLines) {
    await cartModule.createLineItemTaxLines({
      item_id: taxLine.item_id,
      rate: taxLine.rate,
      code: taxLine.code,
      name: taxLine.name,
    })
  }
  ```
</CodeGroup>

### Converting to Order

Mark cart as completed and create order.

<CodeGroup>
  ```typescript Example theme={null}
  import { Modules } from "@medusajs/framework/utils"

  const orderModule = container.resolve(Modules.ORDER)

  // Create order from cart
  const order = await orderModule.createOrders({
    customer_id: cart.customer_id,
    email: cart.email,
    currency_code: cart.currency_code,
    items: cart.items,
    shipping_address: cart.shipping_address,
    billing_address: cart.billing_address,
    shipping_methods: cart.shipping_methods,
  })

  // Mark cart as completed
  await cartModule.updateCarts(cart.id, {
    completed_at: new Date(),
  })
  ```
</CodeGroup>

## Best Practices

1. **Total Calculation**: To calculate totals, you must include total fields in `select` and required relations (`items`, `items.tax_lines`, `items.adjustments`, `shipping_methods`, etc.). The module automatically includes needed relations when total fields are selected.

2. **Currency Consistency**: Ensure all prices (line items, shipping, adjustments) use the same currency as the cart.

3. **Guest Carts**: For guest carts, omit `customer_id` but always include `email` for order conversion.

4. **Cart Completion**: Set `completed_at` when converting to an order to prevent reuse.

5. **Adjustments**: Use negative amounts for discounts in LineItemAdjustment and ShippingMethodAdjustment.

6. **Tax-Inclusive Pricing**: Use the `is_tax_inclusive` flag to indicate if prices already include tax.

## Related Modules

* [Product Module](/modules/product) - Retrieve products for cart
* [Pricing Module](/modules/pricing) - Calculate product prices
* [Promotion Module](/modules/promotion) - Apply discounts
* [Order Module](/modules/order) - Convert carts to orders
* [Region Module](/modules/region) - Region-based cart configuration
