joseph-montanez / phpcartload
独立购物车
v0.1.0
2019-02-25 04:56 UTC
Requires
- php: >=7.0
- symfony/event-dispatcher: 4.2
Requires (Dev)
- codeception/codeception: 2.5.4
- phpmd/phpmd: 2.6.0
- phpmetrics/phpmetrics: 2.4.1
- phpstan/phpstan: 0.11.2
This package is auto-updated.
Last update: 2024-09-28 06:05:56 UTC
README
受 Python 的 Satchless 启发,PHPCartLoad 是一个功能丰富的库,旨在帮助构建购物车和通用电子商务应用。此库旨在提供易于使用的基础类,以管理库存定价和购物车。没有数据库要求,完全由数据驱动。
许可证
该库遵循 MIT 许可,你可以自由使用。
待办事项
这仍然处于 alpha 版本,API 正在改进。以下功能将陆续推出
- 达到 100% 代码覆盖率
- 创建重量测试
- 完成购物车 API
- 为查找、删除添加更多事件
- 提供默认的重复逻辑
- 税收或按项目拆分价格
- 通过定价规则(Mangento 在此方面做得很好)提供折扣、优惠券
- 考虑多货币、多语言支持
- 库存 API
- 库存
- 直销
- 已售罄(包括支持限制已售罄的数量)
- 配送 API
- 对 USPS 的初始支持
- 更多配送提供商的指南
- 基于产品尺寸和重量的包装尺寸计算数学
- 订单 API
- 发票 API
示例
以下是库中当前可用的示例。
简单定价
这是该库的“Hello World”,展示了您需要做多少工作才能使库与现有代码库一起工作。
<?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));
批量定价
您还可以扩展定价以包括批量定价,同时保持尽可能简单。
<?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 变体
SKU 变体或选项是让基础产品作为所有变体平台的方式。所以如果您有一件有多种尺寸和颜色的衬衫,那么您可以针对每个这些变体/选项提供定制定价。
<?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);
组合
组合是自定义结果产品变体的方式。这可能只是说某个产品配置不可用,或者可能需要为特定组合提供特殊价格、SKU 或重量。如果需要跟踪变体的库存,另一种可能性是将这些链接到单个产品以进行更高级的定制。
<?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;
购物车事件
提供了一个带有 PHP Session ($_SESSION) 驱动的通用购物车系统。可以注入事件以提供基本功能,如验证。
<?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;
}
}
如何构建文档
您需要在您的计算机上安装 Python。
cd docs
pip install -r requirements.txt
make