foridom/laravel-shopping-cart

Laravel应用购物车。

1.3.4 2020-03-26 04:19 UTC

This package is auto-updated.

Last update: 2024-09-26 13:58:39 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Latest Unstable Version License

在电商场景中,购物车是一个基本且必需的模块。本包是基于overtrue/laravel-shopping-cart进行扩展开发的,主要实现了以下扩展:

  1. 购物车数据支持数据库存储
  2. Item 增加 Model 属性返回。因为购物车可能是SPU或者SKU,所以直接通过model属性返回相关对象。
  3. 支持多 Guard。因为在果酱小店中有商城购物车和导购购物车。

已经完成了 Session 和 Database 模式下的单元测试,而且正在果酱小店产品线上使用中。可放心使用。

安装

composer require ibrand/laravel-shopping-cart:~1.0 -vvv
php artisan vendor:publish --provider="iBrand\Shoppingcart\ServiceProvider"

低于 Laravel5.5 版本

config/app.php 文件中 'providers' 添加

iBrand\Shoppingcart\ServiceProvider::class

config/app.php 文件中 'aliases' 添加

'Cart'=> iBrand\Shoppingcart\Facade::class

用法

选择存储

您可以在 config/ibrand/cart.php 文件中更改数据存储。

'storage' => \iBrand\Shoppingcart\Storage\DatabaseStorage::class,
  
'storage' => \iBrand\Shoppingcart\Storage\SessionStorage::class,

如果使用数据库存储,需要执行 php artisan migrate

添加商品到购物车

添加一个新商品。

Item | null Cart::add(
                    string | int $id,
                    string $name,
                    int $quantity,
                    int | float $price
                    [, array $attributes = []]
                 );

示例

$row = Cart::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 Cart::update(string $rawId, int $quantity);
Item Cart::update(string $rawId, array $arrtibutes);

示例

Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name');
// or only update quantity
Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);

获取所有商品

获取所有商品。

Collection Cart::all();

示例

$items = Cart::all();

获取商品

获取指定的商品。

Item Cart::get(string $rawId);

示例

$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');

移除商品

通过原始ID移除指定的商品。

boolean Cart::remove(string $rawId);

示例

Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

清空购物车

清理购物车。

boolean Cart::destroy();
boolean Cart::clean(); // alias of destroy();

示例

Cart::destroy();// or Cart::clean();

总价

返回所有商品的总价。

int | float Cart::total(); // alias of totalPrice();
int | float Cart::totalPrice();

示例

$total = Cart::total();
// or
$total = Cart::totalPrice();

行数计数

返回行数。

int Cart::countRows();

示例

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = Cart::countRows(); // 2

数量计数

返回所有商品的数量

int Cart::count($totalItems = true);

$totalItems : 当 false 时,将返回行数。

示例

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = Cart::count(); // 11 (5+1+5)

搜索商品

按属性搜索商品。

Collection Cart::search(array $conditions);

示例

$items = Cart::search(['color' => 'red']);
$items = Cart::search(['name' => 'Item name']);
$items = Cart::search(['qty' => 10]);

检查是否为空

bool Cart::isEmpty();

指定关联模型

指定商品关联的模型。

Cart Cart::associate(string $modelName);

示例

Cart::associate('App\Models\Product');
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->product->name; // $item->product is instanceof 'App\Models\Product'

集合与商品

CollectionOvertrue\LaravelShoppingCart\ItemIlluminate\Support\Collection 的实例,用法参考:Collections - Laravel文档。

Overtrue\LaravelShoppingCart\Item 的属性

  • id - 你的商品项ID。
  • name - 商品名称。
  • qty - 商品数量。
  • price - 商品单价。
  • total - 商品总价。
  • __raw_id - 行的唯一ID。
  • __model - 商品关联模型的名称。
  • ... 自定义属性。

以及方法

  • rawId() - 返回商品的原始ID。

事件

你可以轻松处理这些事件,例如

Event::on('cart.adding', function($attributes, $cart){
    // code
});