1. Go to this page and download the library: Download tandrezone/cart-officer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tandrezone / cart-officer example snippets
CartOfficer\Cart;
use CartOfficer\CartController;
session_start(); // must be called before new Cart()
$cart = new Cart();
$controller = new CartController($cart, '/orders'); // pass your order route
$controller->handle(); // reads $_POST / JSON body, writes JSON response
$orderRoute = '/orders'; // shown as a hint; JS reads window.CartOfficer.orderRoute
// orders.php
$payload = json_decode($_POST['cart_payload'] ?? '{}', true);
$items = $payload['items']; // array of cart lines
$total = $payload['total']; // float grand total
$itemCount = $payload['item_count']; // int
// … persist to DB, generate invoice, etc.
use CartOfficer\Cart;
$cart = new Cart();
$cart->add('id', 'variant', 'Name', 19.99, 2); // returns CartItem
$cart->update('id_variant', 3); // set quantity; 0 removes
$cart->remove('id_variant'); // remove one line
$cart->clear(); // empty cart
$cart->items(); // CartItem[] keyed by "productId_variant"
$cart->count(); // total units
$cart->total(); // float grand total
$cart->isEmpty(); // bool
$cart->toArray(); // raw session array