Add item to cart
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."
]
}
}Add Item to Cart
Adds a single product item to the customer’s cart.
POST
/
cart
/
add-item
Add item to cart
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."
]
}
}Body
application/json
Response
Item added to cart successfully.
- Default
- Item Only
Returns a full updated cart response.