Skip to content

cart ​

The shopper's cart. Reads need no authentication; writes need CSRF, which the client bootstraps on its own. merge requires a signed-in customer.

Guest carts are identified by the X-Mawjod-Cart-Token header, not by the session cookie. The client captures and replays that token for you. See Cart for the whole story.

ts
mawjod.cart.get()
mawjod.cart.quote()
mawjod.cart.addLine(input)
mawjod.cart.updateLine(lineId, quantity)
mawjod.cart.removeLine(lineId)
mawjod.cart.applyCoupon(code)
mawjod.cart.removeCoupon()
mawjod.cart.merge(guestToken?)

cart.get() ​

ts
get(): Promise<Cart>

GET /api/v1/cart. Reads the caller's cart with refreshed prices and availability.

ts
const cart = await mawjod.cart.get()

cart.id is null when the caller has no cart yet. The API answers a zeroed cart rather than a 404, so "no cart" is a normal state and not an error to handle.

Cart ​

ts
interface Cart {
  id: string | null
  status: string                  // 'active'
  is_guest: boolean
  guest_token: string | null      // non-null exactly once, at creation
  item_count: number
  vendor: VendorSummary | null    // marketplace: the cart's one seller; null when empty or single shop
  subtotal: Money
  has_unpurchasable_lines: boolean
  lines: CartLine[]
  adjustments: CartAdjustment[]
  last_activity_at: string | null
}

interface CartLine {
  id: string
  variant_id: string
  sku: string
  name_ar: string
  name_en: string
  option_selection: Record<string, unknown>  // reserved; always {} in release one
  quantity: number
  unit_price: Money
  line_total: Money
  purchasable: boolean
  in_stock: boolean
  product_slug: string | null
  image: Image | null
}

purchasable and in_stock are two checks. purchasable: false means the variant was switched off, archived or unpublished after it was added; the line stays so the shopper sees what changed, and it counts nothing toward the subtotal. in_stock: false means stock on hand does not cover the line's quantity right now. Checkout refuses such a cart with insufficient_stock, naming exactly these lines' variants, so flag them on the cart page before the shopper gets that far:

ts
// Buyable
{ variant_id: 'var-1', quantity: 2, purchasable: true, in_stock: true, /* … */ }

// Still sold, but not two of it right now
{ variant_id: 'var-2', quantity: 2, purchasable: true, in_stock: false, /* … */ }

Lines carry both locales, unlike the catalog which resolves one server-side. A stored line therefore renders correctly whichever locale the shopper was in when they added it.

A row can show a picture and link back to the product. image is the variant's own picture where it has one and the product's first otherwise, the same Image the catalog returns; product_slug is the address of the product page, for /products/{product_slug}:

vue
<NuxtLink v-if="line.product_slug" :to="`/products/${line.product_slug}`">
  <img v-if="line.image" v-bind="imageSrcSet(line.image)" sizes="64px" :alt="line.image.alt ?? ''">
  {{ line.name_ar }}
</NuxtLink>
<span v-else>{{ line.name_ar }}</span>

Both are read off the catalogue on every cart projection rather than snapshotted when the line was added, so a renamed product or a replaced picture reaches an old cart on its own. product_slug is null only when the product is gone, which is the case the fallback above covers; image is null when nothing has been uploaded.

subtotal is the plain line total. Discounts and tax do not appear on Cart; they live on cart.quote().

cart.quote() ​

ts
quote(): Promise<CartQuote>

GET /api/v1/cart/quote. Reprices the cart and re-evaluates promotions. It does not mutate anything.

ts
interface CartQuote {
  subtotal: Money
  discount_total: Money
  discounted_subtotal: Money
  tax_amount: Money
  tax_rate_basis_points: number     // 1400 = 14%
  applied_discount: AppliedDiscount | null
  lines: QuotedLine[]               // [{ line_id, gross, discount, net }]
  rejected_discounts: RejectedDiscount[]
}

interface AppliedDiscount {
  discount_id: string
  name_ar: string
  name_en: string
  type: string
  scope: string
  coupon_code: string | null
  amount: Money
}

discounted_subtotal.minor is what checkout's expected_items_subtotal_minor guard wants, and what a fulfillment quote's subtotal_minor wants.

rejected_discounts lists promotions that were evaluated and did not apply, each with a reason.

cart.addLine() ​

ts
addLine(input: AddCartLineInput): Promise<Cart>

POST /api/v1/cart/lines. Creates the cart when there is none.

ts
interface AddCartLineInput {
  variant_id: string
  quantity: number                      // 1-999
  options?: Record<string, never>       // reserved; release one accepts only {}
  replace_cart?: boolean                // marketplace: empty the cart first (see below)
}

Adding a variant that is already in the cart sums into the existing line rather than creating a second one.

For a guest, this is the response that issues guest_token. The client stores it and sends it as X-Mawjod-Cart-Token from here on. It is never issued again.

A variant that cannot be sold is 409 variant_not_purchasable.

In a marketplace a cart buys from one seller. Adding another seller's item is 409 cart_vendor_mismatch, with the seller already in the cart on problem.cart_vendor. Ask the shopper, then add again with replace_cart: true: the cart is emptied (lines and coupon) and the item added, and the removed lines come back as removed adjustments.

ts
import { CartErrorCode, isMawjodApiError } from '@mawjod/api'

try {
  await mawjod.cart.addLine({ variant_id, quantity: 1 })
} catch (error) {
  if (isMawjodApiError(error) && error.code === CartErrorCode.VendorMismatch) {
    // "Your cart has items from {error.problem.cart_vendor?.name_en}. Empty it and add this?"
    await mawjod.cart.addLine({ variant_id, quantity: 1, replace_cart: true })
  }
}

cart.updateLine() ​

ts
updateLine(lineId: string, quantity: number): Promise<Cart>

PUT /api/v1/cart/lines/{lineId}. Replaces the line's quantity (1 to 999). This is a replace, not a delta.

cart.removeLine() ​

ts
removeLine(lineId: string): Promise<Cart>

DELETE /api/v1/cart/lines/{lineId}. Returns the cart without that line.

cart.applyCoupon() ​

ts
applyCoupon(code: string): Promise<CartQuote>

POST /api/v1/cart/coupon. The code is 3 to 40 characters and matched case-insensitively.

Returns a CartQuote, not a Cart.

Every refusal is one code, 409 pricing_conflict, covering unknown, inactive, expired, unqualified, fully redeemed, and beaten-by-a-better-promotion. The problem document may carry a reason slug ('expired', for instance), readable through error.problem.reason.

cart.removeCoupon() ​

ts
removeCoupon(): Promise<CartQuote>

DELETE /api/v1/cart/coupon. The returned quote has applied_discount: null.

cart.merge() ​

ts
merge(guestToken?: string): Promise<Cart>

POST /api/v1/cart/merge. Requires an authenticated customer.

Defaults to the stored guest token, so you usually call it with no arguments. It throws a plain Error when no token was passed and none is stored: there is nothing to merge, which is a programming mistake rather than an API failure.

ts
const cart = await mawjod.cart.merge()

cart.is_guest    // false
cart.adjustments // [{ reason: 'merged_quantity_summed', line_id, variant_id,
                 //    previous_quantity, quantity }]

The merge is additive and deterministic: identical lines sum, guest-only lines move across, and replaying the same token merges only once. On success the client clears the stored token.

In a marketplace a cart holds one seller's items, so the guest cart replaces the saved one instead. The saved lines come back as replaced_on_merge adjustments (CartAdjustmentReason.ReplacedOnMerge): tell the shopper their saved cart was replaced.

The token moves to the body here

Every other cart call carries the guest token in the X-Mawjod-Cart-Token header. This one takes it in the request body as guest_token. The client handles the difference; it is documented because it is visible in the network tab.

checkout.place can also merge inline, by taking guest_token in its own body.

Errors ​

CodeStatusWhere
variant_not_purchasable409addLine
pricing_conflict409applyCoupon
validation_failed422everywhere
rate_limited429everywhere
store_unavailable503everywhere

merge also requires authentication, so 401 unauthenticated applies to it.

In Nuxt ​

ts
const { cart, lines, itemCount, subtotal, latestQuote, addLine, updateLine, removeLine } = useCart()

useCart() keeps the cart in shared state and writes every mutation's response into it. See Composables → useCart.