dev-master
9999999-devStandalone shopping cart
MIT
The Requires
- php >=5.5.9
- symfony/event-dispatcher 3.4.9
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Standalone shopping cart
Inspired by Satchless for Python, PHPCartLoad is a feature rich library intended to help build shopping carts and general e-commerce applications. This library is to provide a foundation of easy to use classes to manage inventory pricing and carts. There is zero database requirements and is completely data driven., (*2)
The library is MIT, do what you want with it., (*3)
This is still alpha and undergoing changes for a better API. The following is to come:, (*4)
Here are some examples of what is current available in the library., (*5)
This is the Hello World of this library, showing you how little you have to do to start to get the library working with your existing code base., (*6)
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use CartLoad\Product\Product;
$apple = Product::make([
'name' => 'Apple',
'sku' => 'a',
'price' => 19.95
]);
$qty = 10;
//-- This will return the simple price: 19.95
var_dump($apple->getPrice($qty));
You can also expand on pricing to include bulk pricing, again keeping with something as simple as possible., (*7)
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use CartLoad\Product\Product;
$apple = new Product([
'name' => 'Apple',
'sku' => 'a',
'price' => [
['min_qty' => 1, 'max_qty' => 9, 'price' => 19.95],
['min_qty' => 10, 'price' => 14.95],
]
]);
$qty = 10;
//-- This will return the simple price: 14.95
var_dump($apple->getPrice($qty));
SKU Variations, or options are ways to let a base product serve as a platform for all the variations. So if you have a shirt with several sizes and colors, then you can have custom pricing for each of those variants / options., (*8)
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use CartLoad\Product\Product;
use CartLoad\Cart\Item;
$shirt = Product::make([
'id' => 1,
'name' => 'Shirt',
'sku' => 'shirt',
'price' => [
['min_qty' => 1, 'max_qty' => 9, 'price' => 4.95],
['min_qty' => 10, 'max_qty' => 19, 'price' => 3.95],
],
'variations' => [
[
'id' => 1,
'name' => 'Color',
'required' => true,
'items' => [
['id' => 1, 'name' => 'Red', 'price' => 0.5, 'sku' => 'r'],
['id' => 2, 'name' => 'Blue', 'price' => 0.4, 'sku' => 'b'],
['id' => 3, 'name' => 'Green', 'price' => 0.6, 'sku' => 'g'],
]
],
[
'id' => 2,
'name' => 'Size',
'required' => true,
'items' => [
['id' => 4, 'name' => 'Small', 'price' => 1.0, 'sku' => 's'],
['id' => 5, 'name' => 'Medium', 'price' => 1.1, 'sku' => 'm'],
['id' => 6, 'name' => 'Large', 'price' => 1.2, 'sku' => 'l'],
]
],
]
]);
//-- Blue Medium Shirt
$cartItem = Item::make([
'id' => 1,
'product_id' => 1, //Shirt product ID
'qty' => 1,
'variations' => [2, 5] // Blue, Medium
]);
//-- The unit price of a blue medium shirt is 6.45
$unit_price = $cartItem->getPrice($shirt);
//-- The resulting SKU is then "shirt-b-m"
$unit_sku = $cartItem->getSku($shirt);
Combinations are ways to customize the resulting product variants. This could be as simple as saying a certain product configuration is not available, or maybe you need a special price, SKU or weight for a specific combination. Another possibility if you need to track stock for the variations, instead these can be linked to individual products for greater customizations., (*9)
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use CartLoad\Product\Product;
use CartLoad\Cart\Item;
$shirt = Product::make([
'id' => 1,
'name' => 'Shirt',
'sku' => 'shirt',
'price' => [
['min_qty' => 1, 'max_qty' => 9, 'price' => 4.95],
['min_qty' => 10, 'max_qty' => 19, 'price' => 3.95],
],
'variations' => [
[
'id' => 1,
'name' => 'Color',
'required' => true,
'items' => [
['id' => 1, 'name' => 'Red', 'price' => 0.5, 'sku' => 'r'],
['id' => 2, 'name' => 'Blue', 'price' => 0.4, 'sku' => 'b'],
['id' => 3, 'name' => 'Green', 'price' => 0.6, 'sku' => 'g'],
]
],
[
'id' => 2,
'name' => 'Size',
'required' => true,
'items' => [
['id' => 4, 'name' => 'Small', 'price' => 1.0, 'sku' => 's'],
['id' => 5, 'name' => 'Medium', 'price' => 1.1, 'sku' => 'm'],
['id' => 6, 'name' => 'Large', 'price' => 1.2, 'sku' => 'l'],
]
],
],
'combinations' => [
//-- Blue Medium Shirt on Sale with special SKU to track all blue shirt sales in main system
[
'id' => 1,
'variations' => [2, 5],
'price' => 5.00,
'sku' => 'shirt-b-m-sale',
]
]
]);
//-- Blue Medium Shirt
$cartItem = Item::make([
'id' => 1,
'product_id' => 1, //Shirt product ID
'qty' => 1,
'variations' => [2, 5] // Blue, Medium
]);
//-- The unit price of a blue medium shirt is 5.00
$unit_price = $shirt->getCartPrice($cartItem);
//-- The resulting SKU is then "shirt-b-m-sale"
$unit_sku = $shirt->getCartSku($cartItem);
echo $unit_sku, ' - ', $unit_price;
A generic cart system is provided with a PHP Session ($_SESSION) driver. Its possible to interject events to provide basic features like validation., (*10)
<?php
use CartLoad\Cart\Container;
use CartLoad\Cart\Events\CartAddItemBeforeEvent;
use CartLoad\Cart\Events\CartGetItemAfterEvent;
use CartLoad\Cart\Item;
use CartLoad\Cart\Repositories\Session;
use CartLoad\Product\Product;
require_once __DIR__ . '/../vendor/autoload.php';
//--------------------------------------------------------------------------------------------------------------
//-- Products
//--------------------------------------------------------------------------------------------------------------
$products = [
'd97c4f2f-fd06-4e7d-b161-51f4038ee898' => Product::make([
'id' => 'd97c4f2f-fd06-4e7d-b161-51f4038ee898',
'name' => 'Apple',
'sku' => 'a',
'price' => 19.95
]),
'b99cb34e-7edb-45d3-9dfe-5e39f6b71587' => Product::make([
'id' => 'b99cb34e-7edb-45d3-9dfe-5e39f6b71587',
'name' => 'Orange',
'sku' => 'o',
'price' => 21.99
]),
];
//--------------------------------------------------------------------------------------------------------------
//-- Cart
//--------------------------------------------------------------------------------------------------------------
$repository = new Session();
$container = new Container($repository);
//--------------------------------------------------------------------------------------------------------------
//-- Validation
//--------------------------------------------------------------------------------------------------------------
//-- @ before adding to cart
$container->addListener(CartAddItemBeforeEvent::NAME, function (CartAddItemBeforeEvent $event) use ($products) {
/**
* @var $item CartLoad\Cart\Item
*/
$item = $event->getItem();
/** @var CartLoad\Product\Product $product */
$product = isset($products[$item->getProductId()]) ? $products[$item->getProductId()] : null;
if ($product === null) {
$event->addError('Sorry this product "' . $item->getProductId() . '" does not exist', 'does-not-exist');
}
});
//-- @ after getting an item
$container->addListener(CartGetItemAfterEvent::NAME, function (CartGetItemAfterEvent $event) {
/**
* @var $item CartLoad\Cart\Item
*/
$item = $event->getItem();
if ($item->getQty() < 1) {
$event->addError('Please enter a quantity', 'qty');
}
});
//-- Example of a good cart add
$apple = $products['d97c4f2f-fd06-4e7d-b161-51f4038ee898'];
$cartAppleData = [
'id' => uniqid(),
'product_id' => $apple->getId(),
'qty' => 3,
];
$cartAppleItem = Item::make($cartAppleData);
$appleAdded = $container->addItem($cartAppleItem); //-- returns true
//-- Example of a bad cart add
$cartBadAppleData = [
'id' => uniqid(),
'product_id' => 'i do not exist',
'qty' => 3,
];
$cartBadAppleItem = Item::make($cartBadAppleData);
$badAppleAdded = $container->addItem($cartBadAppleItem); //-- returns false
if (!$badAppleAdded) {
$errors = $container->getErrors();
// array(1) {
// ["does-not-exist"]=> string(33) "Sorry this product "i do not exist" does not exist"
// }
foreach ($errors as $key => $error) {
echo '"', $key, '" - ', $error, PHP_EOL;
}
}
You will need python install on your computer., (*11)
cd docs pip install -r requirements.txt make
Standalone shopping cart
MIT