NAV
cURL Node.js jQuery PHP WP API

Introduction

GitHub Mark Logo Edit on GitHub

Welcome to CoCart Pro. An extension that completes the CoCart API package with more control over the cart from coupons, shipping methods to additional fees and more.

This section of the documentation is for CoCart Pro only so if you are looking for the core features in the CoCart API then you can access that part of the CoCart documentation here.

Requirements

To use the latest version of the CoCart Pro you must be using:

Request/Response Format

The default response format is in JSON. Requests with a message-body use plain JSON to set or update resource attributes. Successful requests will return a 200 OK HTTP status.

Some general information about response:

Body Types

There are different types of body types when making a request. Make sure when using either of these body types that you have the correct Content-Type for the header.

Body Type Content-Type
form-data  application/json
raw  application/json
x-www-form-urlencoded application/x-www-form-urlencoded

I personally find using raw works best if you are passing an object of data for a variation or cart_item_data.

Errors

Occasionally you might encounter errors when accessing the REST API. Here are the possible types:

Error Code Error Type
400 Bad Request Invalid request, e.g. using an unsupported HTTP method
401 Unauthorized Authentication or permission error, e.g. incorrect login
403 Forbidden Not allowed to process this action or have permission.
404 Not Found Requests to resources that don't exist or are missing
405 Method Not Allowed A request method is not supported for the requested resource.
500 Internal Server Error Server error

WP REST API error example

{
  "code": "rest_no_route",
  "message": "No route was found matching the URL and request method",
  "data": {
    "status": 404
  }
}

CoCart error example

{
  "code": "cocart_clear_cart_failed",
  "message": "Clearing the cart failed!",
  "data": {
    "status": 500
  }
}

Errors return both an appropriate HTTP status code and response object which contains a code, message and data attribute.

Getting Started

GitHub Mark Logo Edit on GitHub

Install and Activate

If you have a copy of the plugin as a zip file, you can manually upload it and install it through the Plugins admin screen.

  1. Navigate to Plugins > Add New.
  2. Click the Upload Plugin button at the top of the screen.
  3. Select the zip file from your local filesystem.
  4. Click the Install Now button.
  5. When installation is complete, you’ll see “Plugin installed successfully.” Click the Activate Plugin button at the bottom of the page.

List of server requirements.

Error Responses

GitHub Mark Logo Edit on GitHub

You might encounter an error when using CoCart Pro. These error responses explain what might have happened.

Error Status Error Response
cocart_pro_coupons_not_enabled Coupons are not enabled for this store!
cocart_pro_coupon_required Coupon is required in order to apply!
cocart_pro_coupons_expired The following coupons have expired or no longer valid: %s %s are the coupons expired
cocart_pro_coupon_failed_to_apply Coupon failed to apply to cart.
cocart_pro_coupon_failed_to_remove Coupon failed to remove from cart.
cocart_pro_add_fee_missing_name The name of the fee is missing!
cocart_pro_add_fee_missing_amount The amount of the fee is missing!
cocart_pro_cannot_add_fee There is nothing in the cart so no fee can be added.
cocart_pro_fee_already_exists Fee has already been added.
cocart_pro_removing_fees_failed Cart fees failed to remove.
cocart_pro_no_payment_gateways Please fill in your details above to see available payment methods.
cocart_pro_set_payment_method_failed The gateway ID is required to set a payment method.
cocart_pro_payment_method_incorrect The gateway ID is either incorrect or does not exist.
cocart_pro_shipping_not_enabled Shipping has not been enabled for this store!
cocart_pro_shipping_not_needed Cart does not contain an item that requires shipping.
cocart_pro_no_recurring_cart_key The recurring cart key does not exists.
cocart_pro_shipping_not_calculated Customer has not calculated shipping.
cocart_pro_set_shipping_method_failed Both the method ID and instance ID are required to set a shipping method.
cocart_pro_shipping_method_incorrect The shipping method is either incorrect or does not exist.
cocart_pro_invalid_postcode Please enter a valid postcode / ZIP.
cocart_pro_no_shipping_methods There are no shipping methods available!

Authentication

GitHub Mark Logo Edit on GitHub

The WordPress REST API incorporates a method called nonces to deal with CSRF issues. This ensures that all activities on the website remain segregated. This is because the WordPress REST API just like the WooCommerce REST API is designed for the back-end.

CoCart however, is designed for the front-end so for any CoCart requests made, the cookie authentication is disabled allowing the ability to authenticate in any code language without fault.

REST API authentication methods:

Basic Authentication

What is the Basic Authentication method?

If you use the Basic Authentication method (which you may find the easiest to use for your customers to login with), please make sure that you have SSL enabled when making requests with any of the CoCart API routes. Check out the example adding an item to the cart as a registered customer.

Steps to follow

  1. Download and Install Basic Auth plugin

Adding an item to the cart as a customer.

curl -X POST https://example.com/wp-json/cocart/v1/add-item \
  -u username:password \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": "32",
    "quantity": 1
  }'
// import CoCartAPI from "@cocart/cocart-rest-api"; // Supports ESM
const CoCartAPI = require("@cocart/cocart-rest-api").default;

const CoCart = new CoCartAPI({
  url: "https://example.com",
  consumerKey: 'sebtest123',
  consumerSecret: 'happycoding24'
});

var data = {
  "product_id": "32",
  "quantity": 1
};

CoCart.post("add-item", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/add-item",
  headers: {
      "Authorization": "Basic " + btoa('username:password'),
  },
  method: "POST",
  data: JSON.stringify({
    "product_id" : "32",
    "quantity" : 1
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = json_encode( array(
  'product_id' => "32",
  'quantity' => 1
) );

$headers = array(
  'Accept: application/json',
  'User-Agent: CoCart API/v1',
  'Authorization: Basic ' . base64_encode($username . ':' . $password)
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/add-item",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => $headers
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'product_id' => "32",
    'quantity' => 1
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/add-item', $args );
$body = wp_remote_retrieve_body( $response );
{
  "404dcc91b2aeaa7caa47487d1483e48a":{
    "key":"404dcc91b2aeaa7caa47487d1483e48a",
    "product_id":32,
    "variation_id":0,
    "variation":[],
    "quantity":1,
    "line_tax_data":{
      "subtotal":[],
      "total":[]
    },
    "line_subtotal":18,
    "line_subtotal_tax":0,
    "line_total":18,
    "line_tax":0,
    "data":{}
  }
}

Authentication issues?

If the authentication header is not passing or authentication is not working you, adding a Rewrite rule to your .htaccess file might help.

.htaccess Rewrite rule

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
</IfModule>

JWT Authentication

JWT is a simple web token authentication method for WP REST API. To know more about JSON Web Tokens, please visit https://jwt.io.

It's important that you follow the installation and configurations exactly for this method to work. See instructions.

Steps to follow

  1. Download and Install JWT Authentication plugin from WordPress.org.
  2. Configure the Secret Key.
  3. Get your token.
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/add-item",
  headers: {
      "Authorization": "Bearer " + token,
  },
  method: "POST",
  data: JSON.stringify({
    "product_id" : 32,
    "quantity" : 1
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = json_encode( array(
  'product_id' => 32,
  'quantity' => 1
) );

$headers = array(
  'Accept: application/json',
  'User-Agent: CoCart API/v1',
  'Authorization: Bearer ' . $token
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/add-item",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => $headers
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Authorization' => 'Bearer ' . $token,
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'product_id' => "32",
    'quantity' => 1
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/add-item', $args );
$body = wp_remote_retrieve_body( $response );

WooCommerce API Keys

Though this method is designed for the backend with WooCommerce REST API, with some custom coding you can make it work for the frontend with CoCart. Checkout my guide on how to authenticate with WooCommerce.

Cart Key

GitHub Mark Logo Edit on GitHub

What is a Cart Key?

A cart key is what identifies the cart stored in session along with it's cart data and expiration. This cart key is normally stored in a cookie and is used to load the cart in session if the cart still exists.

For registered customers, the cart key is the user ID/customer ID and starts from a single digit and up.

For guest customers, the cart key is randomly generated of 42 characters. This is the database limit.

Accessing the Cart Key

If cookies don't work for you due to the limitations of the framework your using to build with, then don't worry. The cart key created for the customer is returned via the returned headers once the first item is added to the cart.

Look for X-CoCart-API and you will see the value of the cart key returned. You can then use this to set all future CoCart API requests to load this cart when required using the global cart_key parameter.

Read this guide on how to create a cart and keep using the same one.

Cart Key in Cart Response

It's also possible to return the cart key when you get the cart. Simply download and activate the Get Cart Enhanced add-on plugin and the cart key is returned along with other enhancements in the response.

Coupons

GitHub Mark Logo Edit on GitHub

This API helps you get applied coupons, add coupons to cart, remove coupons from cart and check coupons for validation.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
coupon string Coupon code is required in order to apply it to or remove it from cart. required

Applied Coupons

Returns all applied coupons to the cart.

HTTP request

GET
/wp-json/cocart/v1/coupon
curl -X GET https://example.com/wp-json/cocart/v1/coupon \
  -H "Content-Type: application/json"
CoCart.get("coupon")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/coupon",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/coupon",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/coupon' );
$body = wp_remote_retrieve_body( $response );
{
  "summer20"
}

Apply Coupon

Apply a coupon to the cart.

HTTP request

POST
/wp-json/cocart/v1/coupon
curl -X POST https://example.com/wp-json/cocart/v1/coupon \
  -H "Content-Type: application/json" \
  -d '{
    "coupon": "summer20"
  }'
var data = {
  "coupon": "summer20"
};

CoCart.post("coupon", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/coupon",
  method: "POST",
  data: {
    "coupon": "summer20"
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  'coupon' => 'summer20'
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/coupon",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'coupon' => 'summer20'
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/coupon', $args );
$body = wp_remote_retrieve_body( $response );
{
    "message": "Coupon was successfully added to cart.",
    "coupon": "summer20",
    "response": true
}

Remove Coupon

Remove a coupon from the cart.

HTTP request

DELETE
/wp-json/cocart/v1/coupon
curl -X DELETE https://example.com/wp-json/cocart/v1/coupon \
  -H "Content-Type: application/json" \
  -d '{
    "coupon": "summer20"
  }'
var data = {
  "coupon": "summer20"
};

CoCart.delete("coupon", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/coupon",
  method: "DELETE",
  data: JSON.stringify({
    "coupon": "summer20"
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  'coupon' => 'summer20'
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/coupon",
  CURLOPT_CUSTOMREQUEST => "DELETE",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'coupon' => 'summer20'
  ] ),
  'method' => 'DELETE',
  'timeout' => 30
);

$response = wp_remote_request( 'https://example.com/wp-json/cocart/v1/coupon', $args );
$body = wp_remote_retrieve_body( $response );
{
    "message": "Coupon was successfully removed from cart.",
    "coupon": "summer20",
    "response": true
}

Check Coupons

Checks applied coupons to see if they are still valid or if they have expired or no longer valid.

HTTP request

GET
/wp-json/cocart/v1/check-coupons
curl -X GET https://example.com/wp-json/cocart/v1/check-coupons \
  -H "Content-Type: application/json"
CoCart.get("check-coupons")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/check-coupons",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/check-coupons",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/check-coupons' );
$body = wp_remote_retrieve_body( $response );
"Coupons applied are still valid!"

Cross Sells

GitHub Mark Logo Edit on GitHub

This API helps you get the cross sells. It returns not just the product ID's but the product name, price, regular price and sale price.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
thumb bool Set as true to return the product thumbnail for each item.

HTTP request

GET
/wp-json/cocart/v1/cross-sells
curl -X GET https://example.com/wp-json/cocart/v1/cross-sells \
  -H "Content-Type: application/json"
CoCart.get("cross-sells")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/cross-sells",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/cross-sells",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/cross-sells' );
$body = wp_remote_retrieve_body( $response );
{
    "29": {
        "id": 29,
        "product_name": "Hoodie with Logo",
        "product_title": "Hoodie with Logo",
        "price": "£45.00",
        "regular_price": "£45.00",
        "sale_price": "£0.00"
    },
    "30": {
        "id": 30,
        "product_name": "Hoodie with Pocket",
        "product_title": "Hoodie with Pocket",
        "price": "£35.00",
        "regular_price": "£45.00",
        "sale_price": "£35.00"
    },
    "31": {
        "id": 31,
        "product_name": "Hoodie with Zipper",
        "product_title": "Hoodie with Zipper",
        "price": "£45.00",
        "regular_price": "£45.00",
        "sale_price": "£0.00"
    }
}```

Cart Owner

GitHub Mark Logo Edit on GitHub

This API helps you get the cart owner's details of the customer.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/customer
curl -X GET https://example.com/wp-json/cocart/v1/customer \
  -H "Content-Type: application/json"
CoCart.get("customer")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/customer",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});

<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/customer",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/customer' );
$body = wp_remote_retrieve_body( $response );
{
  "user": {
    "ID": 1,
    "first_name": "John",
    "last_name": "Doe"
  },
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "Company Name LTD",
    "email": "3naxbgjjttb@thrubay.com",
    "phone": "070 4502 9873",
    "country": "GB",
    "state": "",
    "postcode": "L23 6ST",
    "city": "LITTLE CROSBY",
    "address": "48 Victoria Road",
    "address_1": "48 Victoria Road",
    "address_2": ""
  },
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "Company Name LTD",
    "country": "GB",
    "state": "",
    "postcode": "L23 6ST",
    "city": "LITTLE CROSBY",
    "address": "48 Victoria Road",
    "address_1": "48 Victoria Road",
    "address_2": ""
  },
  "has_calculated_shipping": true,
  "is_vat_exempt": "no"
}

Cart Weight

GitHub Mark Logo Edit on GitHub

This API helps you get the cart weight. The weight unit returned is set in your WooCommerce settings.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/weight
curl -X GET https://example.com/wp-json/cocart/v1/weight \
  -H "Content-Type: application/json"
CoCart.get("weight")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/weight",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/weight",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/weight' );
$body = wp_remote_retrieve_body( $response );
"4 kg"

Fees

GitHub Mark Logo Edit on GitHub

Calculate Fees

This API calculate the fees applied.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
set_session bool Sets the cart fees in session once calculated. optional, used mainly for internal use.
return bool Returns the cart fees once calculated.

HTTP request

POST
/wp-json/cocart/v1/calculate/fees
curl -X POST https://example.com/wp-json/cocart/v1/calculate/fees \
  -H "Content-Type: application/json"
CoCart.post("calculate/fees")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/calculate/fees",
  method: "POST",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/calculate/fees",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/calculate/fees' );
$body = wp_remote_retrieve_body( $response );
"Cart fees have been calculated."

Get Fees

This API returns all applied fees to the cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/fees
curl -X GET https://example.com/wp-json/cocart/v1/fees \
  -H "Content-Type: application/json"
CoCart.get("fees")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/fees",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/fees",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/fees' );
$body = wp_remote_retrieve_body( $response );
{
  "cocart-extra-package-protection":{
    "id": "cocart-extra-package-protection",
    "name": "Extra Package Protection",
    "amount": "40",
    "taxable": false,
    "tax_class": "",
    "total": 4000
  }
}

Add a Fee

This API applies a fee to the cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
name string Name of the fee. required
amount string Amount for the fee. required
taxable bool Determines if the fee is taxable. optional
tax_class string The tax class the fee applies to. optional

HTTP request

POST
/wp-json/cocart/v1/fees
curl -X POST https://example.com/wp-json/cocart/v1/fees \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Extra Package Protection",
    "amount": "40"
  }'
var data = {
  "name": "Extra Package Protection",
  "amount": "40"
};

CoCart.post("fees", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/fees",
  method: "POST",
  data: JSON.stringify({
    "name": "Extra Package Protection",
    "amount": "40"
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  "name"   => "Extra Package Protection",
  "amount" => "40"
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/fees",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  "name"   => "Extra Package Protection",
  "amount" => "40"
);
$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/fees', $args );
$body = wp_remote_retrieve_body( $response );
{
    "Fee \"Extra Package Protection\" for £40.00 has been added."
}

Remove Fees

This API removes all fees from the cart.

Properties

Property Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

DELETE
/wp-json/cocart/v1/fees
curl -X DELETE https://example.com/wp-json/cocart/v1/fees \
  -H "Content-Type: application/json" \
CoCart.delete("fees")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/fees",
  method: "DELETE",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/fees",
  CURLOPT_CUSTOMREQUEST => "DELETE",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'method' => 'DELETE'
);
$response = wp_remote_request( 'https://example.com/wp-json/cocart/v1/fees', $args );
$body = wp_remote_retrieve_body( $response );
{
    "All cart fees have been removed."
}

Payment Methods

GitHub Mark Logo Edit on GitHub

This API helps you get the payment methods available.

Get Payment Methods

Returns all available payment methods once the customer has added an item to the cart and the cart requires payment.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/payment-methods
curl -X GET https://example.com/wp-json/cocart/v1/payment-methods \
  -H "Content-Type: application/json"
CoCart.get("payment-methods")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/payment-methods",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/payment-methods",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/payment-methods' );
$body = wp_remote_retrieve_body( $response );
{
    "bacs": {
        "id": "bacs",
        "method_title": "Direct bank transfer",
        "title": "Direct bank transfer",
        "description": "Make your payment directly into our bank account. Please use your Order ID as the payment reference. Your order will not be shipped until the funds have cleared in our account.",
        "icon": "",
        "icon_urls": "",
        "has_fields": false,
        "countries": null,
        "availability": null,
        "supports": [
            "products"
        ],
        "order_button_text": null,
        "chosen_gateway": false
    },
    "cheque": {
        "id": "cheque",
        "method_title": "Check payments",
        "title": "Check payments",
        "description": "Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.",
        "icon": "",
        "icon_urls": "",
        "has_fields": false,
        "countries": null,
        "availability": null,
        "supports": [
            "products"
        ],
        "order_button_text": null,
        "chosen_gateway": false
    },
    "cod": {
        "id": "cod",
        "method_title": "Cash on delivery",
        "title": "Cash on delivery",
        "description": "Pay with cash upon delivery.",
        "icon": "",
        "icon_urls": "",
        "has_fields": false,
        "countries": null,
        "availability": null,
        "supports": [
            "products"
        ],
        "order_button_text": null,
        "chosen_gateway": false
    },
    "stripe": {
        "id": "stripe",
        "method_title": "Stripe",
        "title": "Credit Card (Stripe)",
        "description": "Pay with your credit card via Stripe.",
        "icon": "<img src=\"https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/visa.svg\" class=\"stripe-visa-icon stripe-icon\" alt=\"Visa\" /><img src=\"https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/amex.svg\" class=\"stripe-amex-icon stripe-icon\" alt=\"American Express\" /><img src=\"https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/mastercard.svg\" class=\"stripe-mastercard-icon stripe-icon\" alt=\"Mastercard\" />",
        "icon_urls": [
            "https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/visa.svg",
            "https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/amex.svg",
            "https://yourdomain.xyz/wp-content/plugins/woocommerce-gateway-stripe/assets/images/mastercard.svg"
        ],
        "has_fields": true,
        "countries": null,
        "availability": null,
        "supports": [
            "products",
            "refunds",
            "tokenization",
            "add_payment_method",
            "subscriptions",
            "subscription_cancellation",
            "subscription_suspension",
            "subscription_reactivation",
            "subscription_amount_changes",
            "subscription_date_changes",
            "subscription_payment_method_change",
            "subscription_payment_method_change_customer",
            "subscription_payment_method_change_admin",
            "multiple_subscriptions",
            "pre-orders"
        ],
        "order_button_text": null,
        "chosen_gateway": false
    }
}

Set Payment Method

Apply the payment method to the cart. Can only apply once the customer has added an item to the cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attributes Type Description
gateway_id string The gateway ID of the payment method. required

HTTP request

POST
/wp-json/cocart/v1/payment-methods
curl -X POST https://example.com/wp-json/cocart/v1/payment-methods \
  -H "Content-Type: application/json" \
  -d '{
    "gateway_id": "stripe"
  }'
var data = {
  "gateway_id": "stripe"
};

CoCart.post("payment-methods", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/payment-methods",
  method: "POST",
  data: JSON.stringify({
    "gateway_id": "stripe"
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  "gateway_id" => "stripe"
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/payment-methods",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'gateway_id' => 'stripe'
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/payment-methods', $args );
$body = wp_remote_retrieve_body( $response );
true

Quantities

GitHub Mark Logo Edit on GitHub

This API helps you get the quantities of each product added to the cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/quantities
curl -X GET https://example.com/wp-json/cocart/v1/quantities \
  -H "Content-Type: application/json"
CoCart.get("quantities")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/quantities",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/quantities",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/quantities' );
$body = wp_remote_retrieve_body( $response );
{
  "31": 1,
  "32": 1,
  "36": 2
}

Removed Items

GitHub Mark Logo Edit on GitHub

This API helps you get items removed by the customer. This can be helpful should you wish to remind and allow customers to re-add an item back to the cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

HTTP request

GET
/wp-json/cocart/v1/removed-items
curl -X GET https://example.com/wp-json/cocart/v1/removed-items \
  -H "Content-Type: application/json"
CoCart.get("removed-items")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/removed-items",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/removed-items",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/removed-items' );
$body = wp_remote_retrieve_body( $response );
{
  "40d8882729aa85d43fe007db12880105": {
    "key": "40d8882729aa85d43fe007db12880105",
    "product_id": 34,
    "variation_id": 0,
    "variation": [],
    "quantity": 1,
    "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
    "line_tax_data": {
      "subtotal": [],
      "total": []
    },
    "line_subtotal": 20,
    "line_subtotal_tax": 0,
    "line_total": 16,
    "line_tax": 0
  }
}

Shipping Methods

GitHub Mark Logo Edit on GitHub

Calculate Shipping

This API helps you calculate the shipping costs and get the available methods. You can also request to return the shipping methods once calculated to reduce API requests.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
country string Set the country code of the shipping location. Country codes required
state string Setting the state is optional but maybe required under some circumstances. State codes optional
city string Set the city to specify location in country. optional
postcode string Enter postcode / ZIP to narrow down location for more accurate shipping cost. optional
return_methods bool Set as true to return the shipping methods once calculated. optional

HTTP request

POST
/wp-json/cocart/v1/calculate/shipping

Example of calculating shipping methods for Great Britain.

curl -X POST https://example.com/wp-json/cocart/v1/calculate/shipping \
  -H "Content-Type: application/json"
  -d '{
    "country": "GB"
  }'
var data = {
  "country": "GB"
};

CoCart.post("calculate/shipping", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/calculate/shipping",
  method: "POST",
  data: JSON.stringify({
    "country" : "GB",
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  'country' => 'GB',
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/calculate/shipping",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'country' => 'GB',
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/calculate/shipping', $args );
$body = wp_remote_retrieve_body( $response );
"Shipping costs updated."

Get Shipping Methods

This API returns all available shipping methods once the customer has calculated shipping.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
recurring_cart_key string The recurring cart key identifies each subscription in cart. required, only for subscriptions.

HTTP request

GET
/wp-json/cocart/v1/shipping-methods

Example of getting the shipping methods.

curl -X GET https://example.com/wp-json/cocart/v1/shipping-methods \
  -H "Content-Type: application/json"
CoCart.get("shipping-methods")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/shipping-methods",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/shipping-methods",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/shipping-methods' );
$body = wp_remote_retrieve_body( $response );
{
  "flat_rate:1":{
    "key": "flat_rate:1",
    "method_id": "flat_rate",
    "instance_id": 1,
    "label": "Flat rate",
    "cost": "33.00",
    "html": "Flat rate: £33.00",
    "taxes": {
      "12": 6.6
    },
    "chosen_method": true
  },
  "free_shipping:3":{
    "key": "free_shipping:3",
    "method_id": "free_shipping",
    "instance_id": 3,
    "label": "Free shipping",
    "cost": "0.00",
    "html": "Free shipping",
    "taxes": [],
    "chosen_method": false
  }
}

Set Shipping Method

This API applies a shipping method to the cart. Can only apply once the customer has calculated shipping.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
key string The key of the shipping method. required
recurring_cart_key string The recurring cart key identifies each subscription in cart. required, only for subscriptions.

HTTP request

POST
/wp-json/cocart/v1/shipping-methods

Example of setting the shipping method for the cart.

curl -X POST https://example.com/wp-json/cocart/v1/shipping-methods \
  -H "Content-Type: application/json" \
  -d '{
    "key": "free_shipping:3"
  }'
var data = {
  "key": "free_shipping:3"
};

CoCart.post("shipping-methods", data)
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/shipping-methods",
  method: "POST",
  data: JSON.stringify({
    "key": "free_shipping:3"
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  "key" => "free_shipping:3"
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/shipping-methods",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    "key" => "free_shipping:3"
  ] ),
  'timeout' => 30
);

$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/shipping-methods', $args );
$body = wp_remote_retrieve_body( $response );
true

Totals

GitHub Mark Logo Edit on GitHub

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Properties

Attribute Type Description
return bool Set as true to return the totals once calculated. optional

Calculate Totals

This API helps you calculate the cart totals. You can also request to retrieve the totals once calculated to reduce API requests and use the Retrieve Cart Totals properties.

HTTP request

POST
/wp-json/cocart/v1/calculate

Example of calculating totals.

curl -X POST https://example.com/wp-json/cocart/v1/calculate \
    -H "Content-Type: application/json"
CoCart.post("calculate")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/calculate",
  method: "POST",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/calculate",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/calculate' );
$body = wp_remote_retrieve_body( $response );
"Cart totals have been calculated."

Get Cart Totals

This API helps you get the cart totals. You can also retrieve the totals pre-formatted to save you time applying the currency symbol. Retrieve Cart Totals properties.

The fee_total, fee_tax and fee_taxes are only updated if the fees are calculated or the totals are calculated once the fees have been added.

HTTP request

GET
/wp-json/cocart/v1/totals

Example of getting the cart totals.

curl -X GET https://example.com/wp-json/cocart/v1/totals \
 -H "Content-Type: application/json"
CoCart.get("totals")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals' );
$body = wp_remote_retrieve_body( $response );
{
  "subtotal":"123.00",
  "subtotal_tax":8.4,
  "shipping_total":"33.00",
  "shipping_tax":6.6,
  "shipping_taxes": {
    "12": 6.6
  },
  "discount_total":24.6,
  "discount_tax":0,
  "cart_contents_total":"98.40",
  "cart_contents_tax":8.4,
  "cart_contents_taxes": {
    "12": 8.4
  },
  "fee_total":"5.00",
  "fee_tax":0.4,
  "fee_taxes":{
    "12": 0.4
  },
  "total":"146.40",
  "total_tax":15,
  "fees": {
    "cocart-extra-package-protection":{
        "id": "cocart-extra-package-protection",
        "name": "Extra Package Protection",
        "amount": "5",
        "taxable": false,
        "tax_class": "",
        "total": 500
    }
  }
}

Get Discount Total

HTTP request

GET
/wp-json/cocart/v1/totals/discount

Example of getting the cart discount total.

curl -X GET https://example.com/wp-json/cocart/v1/totals/discount \
 -H "Content-Type: application/json"
CoCart.get("totals/discount")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/discount",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/discount",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/discount' );
$body = wp_remote_retrieve_body( $response );
"£8.40"

Get Discount Totals

This API returns each coupon applied and their discount total.

HTTP request

GET
/wp-json/cocart/v1/totals/discount/coupon-totals

Example of getting the discount totals.

curl -X GET https://example.com/wp-json/cocart/v1/totals/discount/coupon-totals \
 -H "Content-Type: application/json"
CoCart.get("totals/discount/coupon-totals")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/discount/coupon-totals",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/discount/coupon-totals",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/discount/coupon-totals' );
$body = wp_remote_retrieve_body( $response );
{
  "blizzard": "£8.40"
}

Get Discount Tax Totals

HTTP request

GET
/wp-json/cocart/v1/totals/discount/coupon-tax
curl -X GET https://example.com/wp-json/cocart/v1/totals/discount/coupon-tax \
 -H "Content-Type: application/json"
CoCart.get("totals/discount/coupon-tax")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/discount/coupon-tax",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/discount/coupon-tax",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/discount/coupon-tax' );
$body = wp_remote_retrieve_body( $response );

Get Fee Total

HTTP request

GET
/wp-json/cocart/v1/totals/fee
curl -X GET https://example.com/wp-json/cocart/v1/totals/fee \
 -H "Content-Type: application/json"
CoCart.get("totals/fee")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/fee",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/fee",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/fee' );
$body = wp_remote_retrieve_body( $response );
"£40.00"

Get Fee Tax

HTTP request

GET
/wp-json/cocart/v1/totals/fee/tax
curl -X GET https://example.com/wp-json/cocart/v1/totals/fee/tax \
 -H "Content-Type: application/json"
CoCart.get("totals/fee/tax")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/fee/tax",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/fee/tax",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/fee/tax' );
$body = wp_remote_retrieve_body( $response );

Get Shipping Total

HTTP request

GET
/wp-json/cocart/v1/totals/shipping
curl -X GET https://example.com/wp-json/cocart/v1/totals/shipping \
 -H "Content-Type: application/json"
CoCart.get("totals/shipping")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/shipping",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/shipping",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/shipping' );
$body = wp_remote_retrieve_body( $response );

Get Shipping Tax

HTTP request

GET
/wp-json/cocart/v1/totals/shipping/tax
curl -X GET https://example.com/wp-json/cocart/v1/totals/shipping/tax \
 -H "Content-Type: application/json"
CoCart.get("totals/shipping/tax")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/shipping/tax",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/shipping/tax",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/shipping/tax' );
$body = wp_remote_retrieve_body( $response );

Get Subtotal

HTTP request

GET
/wp-json/cocart/v1/totals/subtotal
curl -X GET https://example.com/wp-json/cocart/v1/totals/subtotal \
 -H "Content-Type: application/json"
CoCart.get("totals/subtotal")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/subtotal",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/subtotal",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/subtotal' );
$body = wp_remote_retrieve_body( $response );
"£42.00"

Get Subtotal Tax

HTTP request

GET
/wp-json/cocart/v1/totals/subtotal/tax
curl -X GET https://example.com/wp-json/cocart/v1/totals/subtotal/tax \
 -H "Content-Type: application/json"
CoCart.get("totals/subtotal/tax")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/subtotal/tax",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/subtotal/tax",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/subtotal/tax' );
$body = wp_remote_retrieve_body( $response );

Get Cart Total Tax

HTTP request

GET
/wp-json/cocart/v1/totals/tax
curl -X GET https://example.com/wp-json/cocart/v1/totals/tax \
 -H "Content-Type: application/json"
CoCart.get("totals/tax")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/tax",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/tax",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/tax' );
$body = wp_remote_retrieve_body( $response );

Get Cart Total

HTTP request

GET
/wp-json/cocart/v1/totals/total
curl -X GET https://example.com/wp-json/cocart/v1/totals/total \
 -H "Content-Type: application/json"
CoCart.get("totals/total")
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/totals/total",
  method: "GET",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/totals/total",
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$response = wp_remote_get( 'https://example.com/wp-json/cocart/v1/totals/total' );
$body = wp_remote_retrieve_body( $response );
"£33.60"

WooCommerce Extensions

GitHub Mark Logo Edit on GitHub

These extensions either support CoCart or is supported in CoCart Pro.

Mix and Match Products

Selling cases of wine? A dozen donuts? Fruit baskets? Six-packs of T-shirts? Mix and Match Products is ideal for offering similar products in bulk containers. Perfect for encouraging customers to buy in bulk without forcing them to buy items that don’t interest them.

Add item to Cart

Once you have setup "Mix and Match Products" WooCommerce extension. Here is how you can add a container with the customers selected items and quantity.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Example of adding a Mix and Match Product

curl -X POST https://example.com/wp-json/cocart/v1/add-item \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": "1102",
    "quantity": 2,
    "cart_item_data": {
        "mnm_config": [
            {
                "product_id": 987,
                "quantity": 1
            },
            {
                "product_id": 1001,
                "quantity": 2
            },
            {
                "product_id": 1003,
                "quantity": 3
            }
        ]
    }
}'
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/add-item",
  method: "POST",
  data: JSON.stringify({
    "product_id": "1102",
    "quantity": 5,
    "cart_item_data": {
        "mnm_config": [
            {
                "product_id": 987,
                "quantity": 1
            },
            {
                "product_id": 1001,
                "quantity": 2
            },
            {
                "product_id": 1003,
                "quantity": 3
            }
        ]
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  'product_id' => '1102',
  'quantity' => 5,
  'cart_item_data' => array(
      'mnm_config' => array(
          array(
              'product_id' => 987,
              'quantity' => 1
          ),
          array(
              'product_id' => 1001,
              'quantity' => 2
          ),
          array(
              'product_id' => 1003,
              'quantity' => 3
          )
      )
  )
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/add-item",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'product_id' => '1102',
    'quantity' => 5,
    'cart_item_data' => array(
        'mnm_config' => array(
            array(
                'product_id' => 987,
                'quantity' => 1
            ),
            array(
                'product_id' => 1001,
                'quantity' => 2
            ),
            array(
                'product_id' => 1003,
                'quantity' => 3
            )
        )
    )
  ] ),
  'timeout' => 30
);
$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/add-item', $args );
$body = wp_remote_retrieve_body( $response );
{
    "6563b32f054b00c7ed83ce4c9bb9eedd": {
        "mnm_config": {
            "987": {
                "product_id": 987,
                "quantity": 1
            },
            "1001": {
                "product_id": 1001,
                "quantity": 2
            },
            "1003": {
                "product_id": 1003,
                "quantity": 3
            }
        },
        "mnm_contents": [
            "8e9868e336b9bafe4722c31ade00a8f6",
            "a29fe85f04188ccc5a7106dec6f5ca89",
            "4531239fed8c9875e0cce609b86be85a"
        ],
        "key": "6563b32f054b00c7ed83ce4c9bb9eedd",
        "product_id": 1102,
        "variation_id": 0,
        "variation": [],
        "quantity": 1,
        "data_hash": "d88f7e7e5dfa45731252a34e3e64325c",
        "line_tax_data": {
            "subtotal": [],
            "total": []
        },
        "line_subtotal": 9.7013,
        "line_subtotal_tax": 0,
        "line_total": 9.7013,
        "line_tax": 0,
        "data": {
            "product_type": "mix-and-match",
            "is_nyp": false
        },
        "product_name": "Mix and Match Products",
        "product_title": "Mix and Match Products",
        "product_price": "£2.00"
    },
    "8e9868e336b9bafe4722c31ade00a8f6": {
        "mnm_container": "6563b32f054b00c7ed83ce4c9bb9eedd",
        "mnm_child_id": 1102,
        "product_id": 987,
        "variation_id": 0,
        "variation": [],
        "quantity": 1,
        "line_tax_data": {
            "subtotal": [],
            "total": []
        },
        "line_subtotal": 1.28355,
        "line_subtotal_tax": 0,
        "line_total": 1.28355,
        "line_tax": 0,
        "data": {},
        "product_name": "Cupcake",
        "product_title": "Cupcake",
        "product_price": "£1.28"
    },
    "a29fe85f04188ccc5a7106dec6f5ca89": {
        "mnm_container": "6563b32f054b00c7ed83ce4c9bb9eedd",
        "mnm_child_id": 1102,
        "product_id": 1001,
        "variation_id": 0,
        "variation": [],
        "quantity": 2,
        "line_tax_data": {
            "subtotal": [],
            "total": []
        },
        "line_subtotal": 2.5671,
        "line_subtotal_tax": 0,
        "line_total": 1.28355,
        "line_tax": 0,
        "data": {},
        "product_name": "Turquoise Cupcake",
        "product_title": "Turquoise Cupcake",
        "product_price": "£1.28"
    },
    "4531239fed8c9875e0cce609b86be85a": {
        "mnm_container": "6563b32f054b00c7ed83ce4c9bb9eedd",
        "mnm_child_id": 1102,
        "product_id": 1003,
        "variation_id": 0,
        "variation": [],
        "quantity": 3,
        "line_tax_data": {
            "subtotal": [],
            "total": []
        },
        "line_subtotal": 3.85065,
        "line_subtotal_tax": 0,
        "line_total": 1.28355,
        "line_tax": 0,
        "data": {},
        "product_name": "Buttercup and Bumblebee Cupcakes",
        "product_title": "Buttercup and Bumblebee Cupcakes",
        "product_price": "£1.28"
    }
}

Retrieve a product

If you are using CoCart Products API, any Mix and Match Product will return additional product data under a new array variable mnm_data. View example.

Name Your Price

The Name Your Price plugin extension lets you be flexible in what price you are willing to accept for selected products. You can use this plugin to accept donations or to take a new approach to selling products. You can suggest a price to your customers and optionally enforce a minimum acceptable price, or leave it entirely in the hands of the customer.

Add item to Cart

Once you have setup "Name Your Price" for your particular products. Here is how you can apply the customers requested price for the product when adding to cart.

Parameters

Parameter Type Description
cart_key string Unique identifier for the cart. ? optional

Example of adding an item with a custom price.

curl -X POST https://example.com/wp-json/cocart/v1/add-item \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": "129",
    "quantity": 1,
    "cart_item_data": {
      "nyp": 24
    }
  }'
$.ajax({
  url: "https://example.com/wp-json/cocart/v1/add-item",
  method: "POST",
  data: JSON.stringify({
    "product_id": "129",
    "quantity": 1,
    "cart_item_data": {
      "nyp": 24
    }
  }),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  complete: function (response) {
    console.log(response);
  }
});
<?php
$curl = curl_init();

$args = array(
  'product_id' => '129',
  'quantity' => 1,
  'cart_item_data' => array(
    'nyp' => 24
  )
);

curl_setopt_array( $curl, array(
  CURLOPT_URL => "https://example.com/wp-json/cocart/v1/add-item",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $args,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json',
    'User-Agent: CoCart API/v1',
  )
) );

$response = curl_exec($curl);

curl_close($curl);

echo $response;
<?php
$args = array(
  'headers' => array(
    'Content-Type' => 'application/json; charset=utf-8',
  ),
  'body' => wp_json_encode( [
    'product_id' => '129',
    'quantity' => 1,
    'cart_item_data' => array(
      'nyp' => 24
    )
  ] ),
  'timeout' => 30
);
$response = wp_remote_post( 'https://example.com/wp-json/cocart/v1/add-item', $args );
$body = wp_remote_retrieve_body( $response );
{
    "nyp": "24",
    "key": "1dc5e4af26b22d63451acb95d2b9deba",
    "product_id": 129,
    "variation_id": 0,
    "variation": [],
    "quantity": 1,
    "data": {},
    "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",
    "line_tax_data": {
        "subtotal": [],
        "total": []
    },
    "line_subtotal": 24,
    "line_subtotal_tax": 0,
    "line_total": 24,
    "line_tax": 0,
    "product_name": "Name your price",
    "product_title": "Name your price",
    "product_price": "£24.00"
}

wp-config.php

GitHub Mark Logo Edit on GitHub

For more handling over CoCart for your store or a client, you will find that using the wp-config.php file is the best place to setup certain conditions.

Uninstall

To prevent any data loss when uninstalling CoCart from the backend and to ensure only the site owner can perform this action. You need to enable the ability to remove it.

<?php
/**
 * Allows the full un-installation of CoCart.
 */
define( 'COCART_REMOVE_ALL_DATA', true );

White Labelling

If you are developing a headless store for a client and need to hide CoCart. Enabling white label mode comes in handy if that is something you would want.

Enabling this hides CoCart completely from the backend including the admin menu, plugin row links, plugin notices, WooCommerce inbox notices and WooCommerce System Status information.

<?php
/**
 * Hides CoCart from the WordPress dashboard.
 */
define( 'COCART_WHITE_LABEL', true );

Load Cart for the Web

Introduced filter since v2.8.2

In addition to white labelling, you can filter the parameter name cocart-load-cart `to what ever you like.

<?php
/**
 * Change the parameter name for loading the cart via the web.
 */
add_filter( 'cocart_load_cart_query_name', function() { return 'my-cart'; });

Libraries

GitHub Mark Logo Edit on GitHub

npm install --save @cocart/cocart-rest-api

// import CoCartAPI from "@cocart/cocart-rest-api"; // Supports ESM
const CoCartAPI = require("@cocart/cocart-rest-api").default;

const CoCart = new CoCartAPI({
  url: "https://example.com",
  version: "cocart/v1"
});

Tools

GitHub Mark Logo Edit on GitHub

Some useful tools you can use to access the API include:

If you use Postman you can use these collections made ready for you.

Learn more

Learn more about the REST API by checking the official WordPress REST API documentation.

Changelog

The Documentation is updated only when needed. Below is the changelog for "CoCart Pro" documentation that matches with the version of the plugin release.

Doc Version 0.10

Doc Version 0.9

Doc Version 0.8

Doc Version 0.7

Doc Version 0.6

Doc Version 0.5

Doc Version 0.4

Doc Version 0.3

Doc Version 0.2

Doc Version 0.1 - Plugin Release v1.0.0