Skip to content

cocart-headless/cocart-php

Repository files navigation

CoCart PHP SDK

Tests Packagist Downloads Packagist Version License


The Official PHP SDK for the CoCart REST API.

Supports API v2 for both CoCart Basic and the legacy CoCart plugin (cart-rest-api-for-woocommerce v4.x).

Important

This SDK is still in development and not yet ready for production use. Provide feedback if you experience a bug.

TODO to complete the SDK

  • Add SDK docs to documentation site
  • Add support for Cart API extras
  • Add Checkout API support
  • Add Customers Account API support

Requirements

  • PHP 8.2 or higher
  • CoCart Basic plugin installed on your WooCommerce store
  • One of: Guzzle (recommended), cURL, or PHP Streams

Support Policy

See SUPPORT.md for our versioning policy, supported PHP versions, and support lifecycle.

Features

  • Full cart management (add, update, remove, clear items)
  • Guest customer support with automatic cart key tracking
  • Authenticated user support (Basic Auth & JWT)
  • JWT token lifecycle (login, refresh, validate, auto-refresh)
  • Session management and cart transfer on login
  • Fetch products easy, search and filter results
  • Batch requests — multiple operations in a single HTTP call
  • Sessions management (admin)
  • Multiple storage adapters for cart key and token persistence
  • Multiple HTTP adapters (Guzzle, cURL, WordPress HTTP API, PHP Streams)
  • CoCartInterface for easy mocking in tests
  • ETag conditional requests for reduced bandwidth (enabled by default)
  • Legacy CoCart plugin support with version-aware endpoint guards
  • Comprehensive error handling
  • PSR-4 autoloading

Installation

composer require cocart/sdk

# Recommended: install Guzzle for best performance
composer require guzzlehttp/guzzle

See Installation Guide for manual install, HTTP adapter options, and full configuration reference.

Quick Start

// Guest customer — cart key is persisted to PHP session automatically
$client = new CoCart('https://your-store.com');
$client->cart()->addItem(123, 2);
$cart = $client->cart()->get();

Documentation

Guide Description
Installation Requirements, Composer/manual install, HTTP adapters, configuration options
Authentication Guest sessions, Basic Auth, JWT (login/refresh/validate/auto-refresh), consumer keys, white-labelling
Cart Add/update/remove items, coupons, customer details, shipping, payment, fees, totals
Products List/search/filter products, pagination, variations, categories, tags, attributes, reviews
Sessions Admin sessions API, SessionManager, storage adapters, cart transfer on login
Error Handling Exception hierarchy, catching errors, HTTP status mapping, response error helpers
Utilities Currency and timezone utilities helpers that operate on data already returned by the API.

Configuration

$client = new CoCart('https://your-store.com', [
    'cart_key'          => 'existing_cart_key',      // Guest session
    'username'          => 'customer@email.com',     // Basic Auth
    'password'          => 'password',
    'jwt_token'         => 'your-jwt-token',         // JWT Auth
    'jwt_refresh_token' => 'your-refresh-token',
    'consumer_key'      => 'ck_xxxxx',               // Admin (Sessions API)
    'consumer_secret'   => 'cs_xxxxx',
    'auth_header'       => 'Authorization',          // Custom auth header for proxies
    'timeout'           => 30,                       // HTTP settings
    'verify_ssl'        => true,
    'rest_prefix'       => 'wp-json',                // Custom REST prefix
    'namespace'         => 'cocart',                 // Custom namespace - Only supported if you have the WhiteLabel add-on
    'main_plugin'       => 'basic',                   // 'basic' (default) or 'legacy' for legacy CoCart plugin
    'etag'              => true,                     // ETag conditional requests (default true)
    'auto_storage'      => true,                     // Auto-persist cart key to $_SESSION
    'session_key'       => 'cocart_cart_key',        // Session key name
]);

CoCart Channels

We have different channels at your disposal where you can find information about the CoCart project, discuss it and get involved:

Twitter: cocartapi CoCart GitHub Stars

  • 📖 Documentation: this is the place to learn how to use CoCart API. Get started!
  • 👪 Community: use our Discord chat room to share any doubts, feedback and meet great people. This is your place too to share how are you planning to use CoCart!
  • 🐞 GitHub: we use GitHub for bugs and pull requests, doubts are solved with the community.
  • 🐦 Social media: a more informal place to interact with CoCart users, reach out to us on X/Twitter.

Credits

Website cocartapi.com  ·  GitHub @cocart-headless  ·  X/Twitter @cocartapi  ·  Facebook  ·  Instagram

License

MIT

About

CoCart's Official PHP SDK

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages