PHP code example of jamesdb / cart

1. Go to this page and download the library: Download jamesdb/cart 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/ */

    

jamesdb / cart example snippets


use jamesdb\Cart\CartItem;

$item = new CartItem([
    ...
    'price' => 1099, // Instead of £10.99 or $10.99 etc.
    ...
]);

use jamesdb\Cart\Cart;
use jamesdb\Cart\Storage\NativeSessionDriver;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;

$cart = new Cart('cart', new NativeSessionDriver);
$cart->setCurrency(new Currency('GBP'));

$cart->setFormatterCallback(function ($money) {
    $currencies = new ISOCurrencies();
    $moneyFormatter = new DecimalMoneyFormatter($currencies);

    return $moneyFormatter->format($money); // outputs in decimal format.
});

use jamesdb\Cart\Cart;
use jamesdb\Cart\CartItem;

$cart = new Cart(...);

/**
 * Assign properties via __set.
 */
$item1 = new CartItem();
$item1->id = 2731;
$item1->name = 'Product';
$item1->price = 1099;
$item1->quantity = 1;

$cart->add($item1);

/**
 * ----------
 */

/**
 * Assign properties via ArrayAccess.
 */
$item2 = new CartItem([
    'id' => 2731,
    'name' => 'Product',
    'price' => 1099,
    'quantity' => 1
]);

$cart->add($item2)

$cart->update('rowid', ['name' => 'Renamed Product']);

/**
 * ----------
 */

$cartItem = $cart->find('rowid');
$cartItem->name = 'Renamed Product';

$cart->remove('rowid');

$cart->clear();

$cart->getItem('rowid');

$cart->getItems();

// Return all items with a quantity of 2.
$cart->filter('quantity', 2);

// Return all items with a price of 1000.
$cart->filter('price', 1000);

$cart->getTotalUniqueItems();

$cart->getTotalItems();

$cart->isEmpty();

$cart->getTotalPrice();

$cart->getTotalPriceExcludingTax();

$cart->getTax();

$cart->addEventListener('cart.add', function ($event) {
    $item = $event->getItem();
    $cart = $event->getCart();

    ...
});

$cart->addEventListener('cart.add', function ($event) {});

$cart->addEventListener('cart.update', function ($event) {});

$cart->addEventListener('cart.remove', function ($event) {});