rogervila / cart
基于Fowler的Money模式的PHP购物车
dev-master
2017-01-09 20:27 UTC
Requires
- php: ^5.6 || ^7.0
- florianv/swap: ^3.0
- moneyphp/money: ^3.0
- php-http/guzzle6-adapter: ^1.1
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2024-09-04 17:26:15 UTC
README
在1.0.0版本标记之前,请勿在生产环境中使用它
购物车基于会话,并遵循Fowler的Money模式。
主要功能.
- 货币管理
- 面向对象编程
- 自定义会话/缓存管理
- 框架无关
- 易于集成
安装
$ composer require rogervila/cart
设置
购物车有两个基本对象:Cart
和Item
创建购物车
购物车构造函数接受一个ID
use Cart\Cart; $cart = new Cart(); // generates an automatic ID // OR $cart = new Cart('myCustomId');
检索购物车
如果存在,将通过传递其ID从会话中检索购物车
$cart = new Cart('myCustomId'); // If it exists on the session, retrieves it instead of creating a new one
更改购物车ID
$cart = new Cart(); // generates an automatic ID $cart->id('myCustomID'); // Changes the ID
当购物车ID更改时,旧会话不会被删除
添加货币
默认情况下,如果没有设置货币,购物车将使用浮点数
为了添加货币,只需添加以下内容
$cart = new Cart(); $cart->currency('EUR'); // add an ISO4217 currency
创建项目
创建项目时,它必须接收一个唯一的ID
use Cart\Item; $item = new Item('mandatoryUniqueId');
添加项目数据
除了传递ID之外,还可以传递包含数据的数组。
$item = new Item([ 'id' => 'uniqueId', 'name' => 'Banana', 'quantity' => 1, // must be an integer 'price' => '0.99' // it accepts strings and integers ]);
添加项目自定义数据
为了添加自定义字段,提供了一个fields()
方法
$fields = [ 'foo' => 'bar' ] $item->fields($fields);
当项目价格设置为整数时,它将被解析为分,因此
(int) 999
将被解析为(string) '9.99'
此外,项目数据也可以使用流畅的方式添加
$item = new Item(123); $item->quantity(2)->price('0.99')->name('Banana');
将项目添加到购物车
如果项目没有数量,它将被设置为1
$items = [ new Item('id1'), new Item('id2'), ] $cart->add($items); // OR $cart->add($item1)->add($item2); // etc...
获取小计
获取所有购物车项目的总和
var_dump($cart->subtotal());
添加费用
费用可以是百分比或固定值
待办事项
添加条件
待办事项
获取总计
获取应用项目条件、购物车条件和费用后的最终结果
待办事项
待办事项
- 完整文档
- 当货币更改时允许价格转换
- 选择自动转换或手动转换
- 当购物车货币更改时更新购物车项目货币
- 将条件(折扣、优惠券等)与自定义规则集成
- 添加费用(税费、运费等)
- 更多测试
许可
MIT