overtrue / laravel-shopping-cart
此包已被放弃且不再维护。未建议替代包。
Laravel应用的购物车。
2.0.2
2019-10-12 03:35 UTC
Requires
- php: >=5.5
Requires (Dev)
- illuminate/session: 5.8.* || ~6.0
- mockery/mockery: ~1.2
- phpunit/phpunit: ~7.5
README
Laravel应用的购物车。
安装
$ composer require "overtrue/laravel-shopping-cart:~2.0"
或者将以下行添加到项目的 composer.json
"require": { "overtrue/laravel-shopping-cart": "~2.0" }
然后
$ composer update
在完成上述步骤后,将以下行添加到 config/app.php
文件的 providers
部分
Overtrue\LaravelShoppingCart\ServiceProvider::class,
并添加以下行到 aliases
部分
'ShoppingCart' => Overtrue\LaravelShoppingCart\Facade::class,
用法
添加商品到购物车
添加一个新商品。
Item | null ShoppingCart::add( string | int $id, string $name, int $quantity, int | float $price [, array $attributes = []] );
示例
$row = ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); // Item: // id => 37 // name => 'Item name' // qty => 5 // price => 100.00 // color => 'red' // size => 'M' // total => 500.00 // __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da' $rawId = $row->rawId();// get __raw_id $row->qty; // 5 ...
更新商品
更新指定的商品。
Item ShoppingCart::update(string $rawId, int $quantity); Item ShoppingCart::update(string $rawId, array $arrtibutes);
示例
ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name']); // or only update quantity ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);
获取所有商品
获取所有商品。
Collection ShoppingCart::all();
示例
$items = ShoppingCart::all();
获取商品
获取指定的商品。
Item ShoppingCart::get(string $rawId);
示例
$item = ShoppingCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
删除商品
通过原始ID删除指定的商品。
boolean ShoppingCart::remove(string $rawId);
示例
ShoppingCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');
销毁购物车
清空购物车。
boolean ShoppingCart::destroy(); boolean ShoppingCart::clean(); // alias of destroy();
示例
ShoppingCart::destroy();// or ShoppingCart::clean();
总价
返回所有商品的总价。
int | float ShoppingCart::total(); // alias of totalPrice(); int | float ShoppingCart::totalPrice();
示例
$total = ShoppingCart::total(); // or $total = ShoppingCart::totalPrice();
行数统计
返回行数。
int ShoppingCart::countRows();
示例
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']); ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); ShoppingCart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']); $rows = ShoppingCart::countRows(); // 2
数量统计
返回所有商品的数量
int ShoppingCart::count($totalItems = true);
$totalItems
: 当设置为 false
时,将返回行数。
示例
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']); ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); $count = ShoppingCart::count(); // 11 (5+1+5)
搜索商品
根据属性搜索商品。
Collection ShoppingCart::search(array $conditions);
示例
$items = ShoppingCart::search(['color' => 'red']); $items = ShoppingCart::search(['name' => 'Item name']); $items = ShoppingCart::search(['qty' => 10]);
检查是否为空
bool ShoppingCart::isEmpty();
指定关联模型
在添加商品到购物车之前,指定关联模型。
Cart ShoppingCart::associate(string $modelName);
示例
ShoppingCart::associate('App\Models\Product'); ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); $item = ShoppingCart::get('8a48aa7c8e5202841ddaf767bb4d10da'); $item->product->name; // $item->product is instanceof 'App\Models\Product'
集合与商品
Collection
和 Overtrue\LaravelShoppingCart\Item
都是 Illuminate\Support\Collection
的实例。使用方法请参考:集合 - Laravel 文档。
Overtrue\LaravelShoppingCart\Item
的属性
id
- 您的商品项ID。name
- 商品名称。qty
- 商品数量。price
- 商品单价。total
- 商品总价。__raw_id
- 行的唯一ID。__model
- 关联模型的名称。- ... 自定义属性。
方法
rawId()
- 返回商品的原始ID。
事件
事件名称 | 参数 |
---|---|
shopping_cart.adding |
($attributes, $cart); |
shopping_cart.added |
($attributes, $cart); |
shopping_cart.updating |
($row, $cart); |
shopping_cart.updated |
($row, $cart); |
shopping_cart.removing |
($row, $cart); |
shopping_cart.removed |
($row, $cart); |
shopping_cart.destroying |
($cart); |
shopping_cart.destroyed |
($cart); |
您可以轻松处理这些事件,例如
Event::listen('shopping_cart.adding', function($attributes, $cart){ // code });
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
许可协议
MIT