Cart API
The cart API manages the shopping cart for the current session.
Get Cart
GET /ec/v1/cart
Response
{
"success": true,
"data": {
"items": [
{
"id": "cart_item_abc123",
"product_id": 1,
"variant_id": null,
"title": "Product Name",
"price": "29.99",
"quantity": 2,
"total": "59.98",
"image": "https://..."
}
],
"subtotal": "59.98",
"shipping": "5.00",
"tax": "15.60",
"total": "80.58",
"item_count": 2,
"coupons": []
}
}
Add Item
POST /ec/v1/cart/add
Body
{
"product_id": 1,
"variant_id": null,
"quantity": 2
}
caution
Parameter order matters in the PHP service: CartService::addItem($product_id, $variant_id, $quantity) — variant before quantity!
Update Quantity
PUT /ec/v1/cart/update
{
"item_id": "cart_item_abc123",
"quantity": 3
}
Remove Item
DELETE /ec/v1/cart/remove
{
"item_id": "cart_item_abc123"
}
Coupons
POST /ec/v1/coupons/apply
{ "code": "SUMMER2026" }
POST /ec/v1/coupons/remove
{ "code": "SUMMER2026" }
GET /ec/v1/coupons/applied
danger
Use /ec/v1/coupons/apply|remove|applied — NOT /ec/v1/cart/coupons. The cart endpoint does not handle coupons.
JavaScript Integration
When consuming the Cart API from JavaScript, always unwrap the response envelope:
const response = await fetch('/wp-json/ec/v1/cart');
const data = await response.json();
// Always unwrap — response might or might not have envelope
const cart = data.cart || data.data || data;
// Normalize field names (API may return snake_case or camelCase)
const itemCount = cart.item_count ?? cart.itemCount ?? 0;
Hooks
| Hook | Parameters | Description |
|---|---|---|
wpec_cart_item_price | $price, $item, $cart | Modify item price in cart |
wpec_cart_calculated_totals | $totals, $cart | Modify final cart totals |