E-commerce is widespread on todayβs internet, and shopping carts are a standard way for users to collect and manage products. This package provides a simple API for working with cart items stored in a session.
Import it first:
<?php
use Krystal\Cart\SessionAdapter;
use Krystal\Cart\ShoppingCart;
$sessionAdapter = new SessionAdapter();
$cart = new ShoppingCart($sessionAdapter);
Only the item ID is required. Attributes, price, and quantity are optional.
// Add product with ID = 9, with default quantity = 1 and no attributes or price
$cart->add(9);
// Add product with ID = 10, with quantity 2 and attributes
$cart->add(10, 2, ['color' => 'red', 'size' => 'XXL']);
// Add product with ID = 11, quantity 3, no attributes, and a fixed price of 99
$cart->add(11, 3, [], 99);
If multiple items share the same ID but have different attributes, you must specify the attributes of the item you want to update.
// Update product (ID = 10) with given attributes
$cart->update(
10,
['color' => 'red', 'size' => 'XXL'],
[
'quantity' => 5, // Optional
'price' => 112, // Optional
'newAttributes' => [ // Optional
'color' => 'blue'
]
]
);
If attributes are not provided, all items with the same ID will be removed.
// Remove all items with ID = 11
$cart->remove(11);
// Remove only the item with matching ID and attributes
$cart->remove(10, ['color' => 'red', 'size' => 'XXL']);
Returns a multi-dimensional array containing all items and a summary block.
$cart->toArray();
The returned structure contains:
items β All cart itemssummary β Aggregated information (totals, counts, etc.)Retrieves a multi-dimensional array describing one specific item stored in the cart.
$item = $cart->get(10, ['color' => 'red', 'size' => 'XXL']);
$cart->isEmpty(); // Returns true or false
Clears all items from the cart.
$cart->clear();