Get Cart
Retrieve the current cart contents and totals.
Updated
On this page
Overview
Retrieve the current cart contents. The SDK automatically tracks the cart ID in localStorage, so you don't need to manage it manually.
SDK Usage
import dash from "@/lib/dash"
const { cart_id, items, subtotal, item_count } = await dash.cart.get()
// items = [
// {
// size_id: "size_abc",
// product_id: "prod_123",
// product_name: "Blue Dream",
// product_slug: "blue-dream",
// variation_name: "Blue Dream",
// size_label: "1g",
// unit_price: "29.99",
// quantity: 2,
// subtotal: "59.98",
// in_stock: true,
// product_image: "https://...",
// }
// ]
Cart Properties
The SDK also provides local getters for quick access without an API call:
// Local state (no API call)
const cartId = dash.cart.cartId // "cart_xyz" or null
const items = dash.cart.items // [] — local cache of items
const totals = dash.cart.getTotals() // { itemCount: 3, subtotal: "89.97" }
const qty = dash.cart.getTotalQuantity() // 3
Load an Existing Cart
If you have a cart ID stored (e.g., in a cookie), load it explicitly:
const cart = await dash.cart.load("cart_xyz789")
Authenticated User Cart
For logged-in customers, load their persistent cart:
const cart = await dash.cart.loadUserCart()
// Returns the customer's saved cart
Cart Migration (Guest → User)
When a guest customer logs in, merge their guest cart with their account:
// After successful login/registration
await dash.cart.migrateToUser()
// Guest cart items are merged into the customer's persistent cart
Tip: Always call
migrateToUser() after authentication. If the customer had no guest cart, this is a no-op.
REST API
# Get cart by ID
GET /storefront/cart/{cart_id}
X-API-Key: pk_live_your_key
# Get authenticated user's cart
GET /storefront/cart/user
Authorization: Bearer {access_token}
Cart Response
{
"cart_id": "cart_xyz789",
"items": [
{
"size_id": "size_abc",
"product_id": "prod_123",
"product_name": "Blue Dream",
"product_slug": "blue-dream",
"variation_name": "Blue Dream",
"variation_slug": "blue-dream",
"size_label": "1g",
"unit_price": "29.99",
"quantity": 2,
"subtotal": "59.98",
"in_stock": true,
"product_image": "https://cdn.dashfordevs.com/...",
"lab_report": null
}
],
"subtotal": "59.98",
"discount_code": null,
"discount_amount": "0.00",
"item_count": 2
}
Next Steps
- Add to Cart — Adding items
- Update Cart Item — Changing quantities
- Apply Discount Code — Applying coupons
- Create Order — Starting the checkout process