PHP code example of baka / cart
1. Go to this page and download the library: Download baka/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/ */
baka / cart example snippets
use Cart\Cart;
use Cart\Storage\SessionStore;
$id = 'cart-01';
$cartSessionStore = new SessionStore();
$cart = new Cart($id, $cartSessionStore);
use Cart\CartItem;
$item = new CartItem;
$item->name = 'Macbook Pro';
$item->sku = 'MBP8GB';
$item->price = 1200;
$item->tax = 200;
$cart->add($item);
$cart->remove('e4df90d236966195b49b0f01f5ce360a356bc76b');
$newId = $cart->update('e4df90d236966195b49b0f01f5ce360a356bc76b', 'price', 959.99);
$item = $cart->get('e4df90d236966195b49b0f01f5ce360a356bc76b');
if ($item) {
// ...
}
$cartItems = $cart->all();
if (count($cartItems) > 0) {
foreach ($cartItems as $item) {
// ...
}
}
if ($cart->has('e4df90d236966195b49b0f01f5ce360a356bc76b')) {
// ...
}
$cart->clear();
$cart->save();
$cart->restore();
$cart->totalUniqueItems();
$cart->totalItems();
$cart->total();
$cart->totalExcludingTax();
$cart->tax();
$cartData = $cart->toArray();
[
'id' => 'cart-01', // cart id
'items' => [
// cart items as array
]
]
$cart->getId();
$cart->getStore();
use Cart\CartItem;
$item = new CartItem;
$item->name = 'Macbook Pro';
$item->sku = 'MBP8GB';
$item->price = 1200;
$item->tax = 200;
$item->options = [
'ram' => '8 GB',
'ssd' => '256 GB'
];
$item = new CartItem;
$item['name'] = 'Macbook Pro';
$item['sku'] = 'MBP8GB';
$item['price'] = 1200;
$item['tax'] = 200;
$item['options'] = [
'ram' => '8 GB',
'ssd' => '256 GB'
];
$itemData = [
'name' => 'Macbook Pro';
'sku' => 'MBP8GB';
'price' => 1200;
'tax' => 200;
'options' => [
'ram' => '8 GB',
'ssd' => '256 GB'
]
];
$item = new CartItem($itemData);
$id = $item->getId();
$id = $item->id;
$id = $item['id'];
$name = $item->get('name');
$name = $item['name'];
$name = $item->name;
$item->set('name', 'Macbook Pro');
$item['name'] = 'Macbook Pro';
$item->name = 'Macbook Pro';
$item->quantity = 1; // ok
$item->quantity = '1' // will throw exception
$item->price = 10.00; // ok
$item->price = '10' // ok
$item->price = 'ten' // will throw exception
$item->getTotalPrice();
$item->getTotalPriceExcludingTax();
$item->getSinglePrice();
$item->getSinglePriceExcludingTax();
$item->getTotalTax();
$item->getSingleTax();
$itemArr = $item->toArray();
[
'id' => 'e4df90d236966195b49b0f01f5ce360a356bc76b', // cart item unique id
'data' => [
'name' => 'Macbook Pro',
'sku' => 'MBP8GB',
'price' => 1200,
// ... other cart item properties
]
]
use Cart\Store;
class SessionStore implements Store
{
/**
* {@inheritdoc}
*/
public function get($cartId)
{
return isset($_SESSION[$cartId]) ? $_SESSION[$cartId] : serialize([]);
}
/**
* {@inheritdoc}
*/
public function put($cartId, $data)
{
$_SESSION[$cartId] = $data;
}
/**
* {@inheritdoc}
*/
public function flush($cartId)
{
unset($_SESSION[$cartId]);
}
}