shunhua / laravel-shoppingcarts

Laravel应用中的购物车。

v1.0 2021-06-22 05:46 UTC

This package is auto-updated.

Last update: 2024-09-22 15:01:04 UTC


README

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

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

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

已经完成了Session和Database模式下的单元测试,线上使用中。可放心使用。

安装

composer require shunhua/laravel-shoppingcarts:^1.0
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/cart.php 文件中更改数据存储。

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

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

向购物车添加项目

添加一个新项目。

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

示例

$row = Cart::add(37, 'Item name', 5, 100.00, 0, ['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\Item 都是 Illuminate\Support\Collection 的实例。用法请参考:集合 - Laravel文档。

Overtrue\LaravelShoppingCart\Item 的属性

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

和方法的属性

  • rawId() - 返回项目的原始ID。

事件

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

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