curl --request POST \
--url {protocol}://{host}/wp-json/cocart/v2/cart/add-item \
--header 'Content-Type: application/json' \
--data '
{
"id": "<string>",
"quantity": "1",
"variation": {},
"item_data": {},
"email": "jsmith@example.com",
"phone": "<string>",
"price": "<string>",
"return_item": false
}
'import requests
url = "{protocol}://{host}/wp-json/cocart/v2/cart/add-item"
payload = {
"id": "<string>",
"quantity": "1",
"variation": {},
"item_data": {},
"email": "jsmith@example.com",
"phone": "<string>",
"price": "<string>",
"return_item": False
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: '<string>',
quantity: '1',
variation: {},
item_data: {},
email: 'jsmith@example.com',
phone: '<string>',
price: '<string>',
return_item: false
})
};
fetch('{protocol}://{host}/wp-json/cocart/v2/cart/add-item', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "{protocol}://{host}/wp-json/cocart/v2/cart/add-item",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'id' => '<string>',
'quantity' => '1',
'variation' => [
],
'item_data' => [
],
'email' => 'jsmith@example.com',
'phone' => '<string>',
'price' => '<string>',
'return_item' => false
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "{protocol}://{host}/wp-json/cocart/v2/cart/add-item"
payload := strings.NewReader("{\n \"id\": \"<string>\",\n \"quantity\": \"1\",\n \"variation\": {},\n \"item_data\": {},\n \"email\": \"jsmith@example.com\",\n \"phone\": \"<string>\",\n \"price\": \"<string>\",\n \"return_item\": false\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("{protocol}://{host}/wp-json/cocart/v2/cart/add-item")
.header("Content-Type", "application/json")
.body("{\n \"id\": \"<string>\",\n \"quantity\": \"1\",\n \"variation\": {},\n \"item_data\": {},\n \"email\": \"jsmith@example.com\",\n \"phone\": \"<string>\",\n \"price\": \"<string>\",\n \"return_item\": false\n}")
.asString();require 'uri'
require 'net/http'
url = URI("{protocol}://{host}/wp-json/cocart/v2/cart/add-item")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"id\": \"<string>\",\n \"quantity\": \"1\",\n \"variation\": {},\n \"item_data\": {},\n \"email\": \"jsmith@example.com\",\n \"phone\": \"<string>\",\n \"price\": \"<string>\",\n \"return_item\": false\n}"
response = http.request(request)
puts response.read_body{
"cart_hash": "e629fa6598d732768f7c726b4b621285",
"cart_key": "cb7a23af96a3f23af3c28a8b4b8c6d5e",
"currency": {
"currency_code": "USD",
"currency_symbol": "$",
"currency_symbol_pos": "left",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": ""
},
"customer": {
"billing_address": {
"billing_first_name": "",
"billing_last_name": "",
"billing_company": "",
"billing_country": "",
"billing_address_1": "",
"billing_address_2": "",
"billing_postcode": "",
"billing_city": "",
"billing_state": "",
"billing_phone": "",
"billing_email": ""
},
"shipping_address": {
"shipping_first_name": "",
"shipping_last_name": "",
"shipping_company": "",
"shipping_country": "",
"shipping_address_1": "",
"shipping_address_2": "",
"shipping_postcode": "",
"shipping_city": "",
"shipping_state": ""
}
},
"items": {
"a3c5e7f9b1d3e5f7a9b1c3d5e7f9a1b3": {
"item_key": "a3c5e7f9b1d3e5f7a9b1c3d5e7f9a1b3",
"id": 145,
"name": "Wireless Bluetooth Headphones",
"title": "Wireless Bluetooth Headphones",
"price": "$18.00",
"quantity": {
"value": 1,
"min_purchase": 1,
"max_purchase": -1
},
"totals": {
"subtotal": 18,
"subtotal_tax": 0,
"total": 18,
"tax": 0
},
"slug": "wireless-bluetooth-headphones",
"meta": {
"product_type": "simple",
"sku": "WBH-001",
"dimensions": {
"length": "",
"width": "",
"height": "",
"unit": "cm"
},
"weight": "0.5",
"variation": {}
},
"backorders": "no",
"cart_item_data": {},
"featured_image": "https://example-store.com/wp-content/uploads/2024/03/headphones-blue.jpg"
}
},
"item_count": 1,
"items_weight": 0.5,
"coupons": [],
"needs_payment": true,
"needs_shipping": true,
"shipping": {
"total_packages": 0,
"show_package_details": true,
"has_calculated_shipping": false,
"packages": []
},
"fees": [],
"taxes": [],
"totals": {
"subtotal": "18.00",
"subtotal_tax": "0.00",
"fee_total": "0.00",
"fee_tax": "0.00",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"total": "18.00",
"total_tax": "0.00"
},
"removed_items": [],
"cross_sells": [],
"notices": {
"success": [
"Wireless Bluetooth Headphones has been added to your cart."
]
}
}Adds a single product item to the customer’s cart.
A valid request URL is required to generate request examples{
"cart_hash": "e629fa6598d732768f7c726b4b621285",
"cart_key": "cb7a23af96a3f23af3c28a8b4b8c6d5e",
"currency": {
"currency_code": "USD",
"currency_symbol": "$",
"currency_symbol_pos": "left",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": ""
},
"customer": {
"billing_address": {
"billing_first_name": "",
"billing_last_name": "",
"billing_company": "",
"billing_country": "",
"billing_address_1": "",
"billing_address_2": "",
"billing_postcode": "",
"billing_city": "",
"billing_state": "",
"billing_phone": "",
"billing_email": ""
},
"shipping_address": {
"shipping_first_name": "",
"shipping_last_name": "",
"shipping_company": "",
"shipping_country": "",
"shipping_address_1": "",
"shipping_address_2": "",
"shipping_postcode": "",
"shipping_city": "",
"shipping_state": ""
}
},
"items": {
"a3c5e7f9b1d3e5f7a9b1c3d5e7f9a1b3": {
"item_key": "a3c5e7f9b1d3e5f7a9b1c3d5e7f9a1b3",
"id": 145,
"name": "Wireless Bluetooth Headphones",
"title": "Wireless Bluetooth Headphones",
"price": "$18.00",
"quantity": {
"value": 1,
"min_purchase": 1,
"max_purchase": -1
},
"totals": {
"subtotal": 18,
"subtotal_tax": 0,
"total": 18,
"tax": 0
},
"slug": "wireless-bluetooth-headphones",
"meta": {
"product_type": "simple",
"sku": "WBH-001",
"dimensions": {
"length": "",
"width": "",
"height": "",
"unit": "cm"
},
"weight": "0.5",
"variation": {}
},
"backorders": "no",
"cart_item_data": {},
"featured_image": "https://example-store.com/wp-content/uploads/2024/03/headphones-blue.jpg"
}
},
"item_count": 1,
"items_weight": 0.5,
"coupons": [],
"needs_payment": true,
"needs_shipping": true,
"shipping": {
"total_packages": 0,
"show_package_details": true,
"has_calculated_shipping": false,
"packages": []
},
"fees": [],
"taxes": [],
"totals": {
"subtotal": "18.00",
"subtotal_tax": "0.00",
"fee_total": "0.00",
"fee_tax": "0.00",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"total": "18.00",
"total_tax": "0.00"
},
"removed_items": [],
"cross_sells": [],
"notices": {
"success": [
"Wireless Bluetooth Headphones has been added to your cart."
]
}
}Body
Unique identifier for the product or variation ID.
Quantity of this item to add to the cart.
Variable attributes that identify the variation of the item.
Additional item data passed to make item unique.
Set the customers billing email address.
Set the customers billing phone number.
Overrides the general or sale price with a custom price for the item if set.
Returns the item details once added.
Response
Item added to cart successfully.
- Default
- Item Only
Returns a full updated cart response.
Unique cart hash.
Unique cart key for the customer.
Show child attributes
Show child attributes
Show child attributes
Show child attributes
Cart items indexed by item key.
Show child attributes
Show child attributes
Total number of items in cart.
Total weight of all items.
Applied coupons.
Show child attributes
Show child attributes
Whether the cart needs payment.
Whether the cart needs shipping.
Show child attributes
Show child attributes
Cart fees.
Show child attributes
Show child attributes
Cart taxes.
Show child attributes
Show child attributes
Show child attributes
Show child attributes
Recently removed items that can be restored.
Show child attributes
Show child attributes
Cross-sell products.
Show child attributes
Show child attributes
Cart notices and messages.
Additional data from extensions.
Was this page helpful?