Batch Requests for the Cart

To use the premium batch request for cart, you need CoCart Plus or higher. Otherwise feel free to use the standard WordPress batch process. Supports API v2 or higher.

Make multiple cart requests and return 1 complete cart response. Sounds to good to be true but it is. The standard batch processing that WordPress supports is too problematic for developers when it comes to using the cart data to present it.

Seriously if you try it yourself, you would not want to use the standard way.

We took away the pain of having to track and merge data together for you, so you can go about your programming with ease.

It supports both authenticated users and guest customers.

How do I use it?

It’s very simple. Post your requests as you would do a standard WordPress batch request only you are using our special batch endpoint wp-json/cocart/batch instead.

{
    "requests": [
        {
            "method": "POST",
            "path": "/cocart/v2/cart/add-item",
            "body": {
                "id": "94",
                "quantity": "2"
            }
        },
        {
            "method": "POST",
            "path": "/cocart/v2/cart/add-item",
            "body": {
                "id": "61",
                "quantity": "1"
            }
        },
        {
            "method": "POST",
            "path": "/cocart/v2/cart/update?namespace=update-customer",
            "body": {
                "first_name": "Sebastien",
                "last_name": "Dumont",
                "email": "thatisnomoon@space.com",
                "phone": "01908672354",
                "company": "Darth Vadar LTD",
                "address_1": "70 The Crescent",
                "address_2": "",
                "city": "BELFAST",
                "state": "",
                "country": "GB",
                "postcode": "BT90 2PU",
                "s_first_name": "Sith",
                "s_last_name": "Lord",
                "s_address_1": "515 Cherry Drive",
                "s_city": "San Pablo",
                "s_state": "CA",
                "s_postcode": "94806",
                "s_country": "US",
                "s_company": "Galaxy Ruler LLC",
                "ship_to_different_address": true
            }
        }
    ]
}

What about guest customers?

Just as you would provide a cart key to identify the cart for the guest customer on a singular cart endpoint, simply provide it the same way. If you used the standard batch process, you would have to apply the cart key to each request, which we don’t recommend.

http://your.store/wp-json/cocart/batch?cart_key=batch101

The results

The results is one complete cart response (if all requests are successful). You should see multiple notices return for each of the requests. Will leave you to decide how you use them.

{
    "cart_hash": "8c5ccafe6a9993e331048677309cccd9",
    "cart_key": "batch101",
    "currency": {
        "currency_code": "USD",
        "currency_symbol": "$",
        "currency_minor_unit": 2,
        "currency_decimal_separator": ".",
        "currency_thousand_separator": ",",
        "currency_prefix": "$",
        "currency_suffix": ""
    },
    "customer": {
        "billing_address": {
            "billing_email": "thatisnomoon@space.com",
            "billing_first_name": "Sebastien",
            "billing_last_name": "Dumont",
            "billing_company": "Darth Vadar LTD",
            "billing_country": "GB",
            "billing_address_1": "70 The Crescent",
            "billing_address_2": "",
            "billing_city": "BELFAST",
            "billing_state": "CA",
            "billing_postcode": "BT90 2PU",
            "billing_phone": "01908672352"
        },
        "shipping_address": {
            "shipping_first_name": "Sith",
            "shipping_last_name": "Lord",
            "shipping_company": "Galaxy Ruler LLC",
            "shipping_country": "US",
            "shipping_address_1": "515 Cherry Drive",
            "shipping_address_2": "",
            "shipping_city": "San Pablo",
            "shipping_state": "CA",
            "shipping_postcode": "94806"
        }
    },
    "items": [
        {
            "item_key": "f4b9ec30ad9f68f89b29639786cb62ef",
            "id": 94,
            "name": "T-Shirt with Logo",
            "title": "T-Shirt with Logo",
            "price": "1800",
            "quantity": {
                "value": 2,
                "min_purchase": 1,
                "max_purchase": -1
            },
            "totals": {
                "subtotal": "3600",
                "subtotal_tax": 0,
                "total": 36,
                "tax": 0
            },
            "slug": "t-shirt-with-logo",
            "meta": {
                "product_type": "simple",
                "sku": "Woo-tshirt-logo",
                "dimensions": {
                    "length": "",
                    "width": "",
                    "height": "",
                    "unit": "cm"
                },
                "weight": 0.40000000000000002,
                "variation": []
            },
            "backorders": "",
            "cart_item_data": [],
            "featured_image": "http://your.store/wp-content/uploads/2024/03/t-shirt-with-logo-1-300x300.jpg"
        },
        {
            "item_key": "e546f9808cfa13a82006e928134ce293",
            "id": 61,
            "name": "Coffee",
            "title": "Coffee",
            "price": "600",
            "quantity": {
                "value": 1,
                "min_purchase": 1,
                "max_purchase": -1
            },
            "totals": {
                "subtotal": "600",
                "subtotal_tax": 0,
                "total": 6,
                "tax": 0
            },
            "slug": "coffee",
            "meta": {
                "product_type": "variation",
                "sku": "",
                "dimensions": {
                    "length": "",
                    "width": "",
                    "height": "",
                    "unit": "cm"
                },
                "weight": 0,
                "variation": []
            },
            "backorders": "",
            "cart_item_data": [],
            "featured_image": "http://your.store/wp-content/uploads/2024/02/adi-goldstein-xKS-1DP4g7A-unsplash-scaled-300x300.jpg"
        }
    ],
    "item_count": 3,
    "items_weight": 0.40000000000000002,
    "coupons": [],
    "needs_payment": true,
    "needs_shipping": true,
    "shipping": {
        "total_packages": 1,
        "show_package_details": false,
        "has_calculated_shipping": false,
        "packages": {
            "default": {
                "package_name": "Shipping",
                "rates": {
                    "flat_rate:1": {
                        "key": "flat_rate:1",
                        "method_id": "flat_rate",
                        "instance_id": 1,
                        "label": "Flat rate",
                        "cost": "1000",
                        "html": "Flat rate: $10.00",
                        "taxes": "",
                        "chosen_method": true,
                        "meta_data": {
                            "items": "T-Shirt with Logo × 2, Coffee - Já - Mokkakanna × 1"
                        }
                    }
                },
                "package_details": "",
                "index": 0,
                "chosen_method": "flat_rate:1",
                "formatted_destination": "515 Cherry Drive, San Pablo, CA 94806"
            }
        }
    },
    "fees": [],
    "taxes": [],
    "totals": {
        "subtotal": "4200",
        "subtotal_tax": "0",
        "fee_total": "0",
        "fee_tax": "0",
        "discount_total": "0",
        "discount_tax": "0",
        "shipping_total": "1000",
        "shipping_tax": "0",
        "total": "5200",
        "total_tax": "0"
    },
    "removed_items": [],
    "cross_sells": [],
    "notices": {
        "success": [
            "“T-Shirt with Logo” has been added to your cart.",
            "“Coffee” has been added to your cart.",
            "Cart updated."
        ]
    }
}

That’s it.

Was this helpful to you?