Skip to content

Merge Cart

Merge a guest cart into the authenticated customer's cart. This is used when a shopper adds items to a cart as a guest and then logs in — the guest cart's items are moved into the customer's own cart so nothing is lost.

Endpoint

POST /api/shop/merge-carts

Request Headers

HeaderRequiredDescription
Content-TypeYesapplication/json
X-STOREFRONT-KEYYesYour storefront API key
AuthorizationYesThe customer Bearer token (from customer login) — not the cart token

Request Body

json
{
  "cart_id": 6884
}
FieldTypeRequiredDescription
cart_idintegerYesThe id of the guest cart to merge into the customer cart

How the merge works

  1. Auth is required — the request must carry a customer Bearer token. A guest (no token) is rejected with 401.
  2. The guest cart is looked up by cart_id — an unknown id returns 404.
  3. The items are merged into the customer's active cart (one is created if the customer has none):
    • An item already in the customer cart with the same product and type has its quantities added together.
    • Any other item is copied into the customer cart (a configurable product's selected-variant child is copied too).
  4. The guest cart is deactivated after the merge.
  5. Invalid items are pruned (a deleted product, or a configurable item missing its variant), then totals are recalculated.
  6. The response is the full, up-to-date customer cart.

Response Fields (201 Created)

FieldTypeDescription
idintegerThe merged (customer) cart id
cartTokenstringCart token identifier
customerIdintegerOwner customer id
channelIdintegerSales channel id
itemsCountintegerDistinct item count
itemsQtyintegerTotal quantity across items
itemsarrayThe merged line items
subtotaldecimalSubtotal before tax and discount
grandTotaldecimalFinal total
taxAmountdecimalTax total
discountAmountdecimalDiscount total
couponCodestring | nullApplied coupon, if any
formattedSubtotalstringSubtotal with currency symbol
formattedGrandTotalstringGrand total with currency symbol
successbooleanWhether the merge succeeded
messagestringResult message

Notes

  • Send the customer token, not the guest cart token — the guest cart is identified by the cart_id in the body.
  • After a successful merge the guest cart is inactive; continue with the returned customer cart through checkout.

Released under the MIT License.