ibrand/laravel-shopping-cart

Laravel 应用程序的购物车。

v1.3.4 2020-09-24 03:07 UTC

This package is auto-updated.

Last update: 2024-09-16 15:03:06 UTC


README

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

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

  1. 购物车数据支持 Database 存储
  2. Item 增加模型属性返回。因为购物车可能是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 的实例,使用方法请参考:集合 - Laravel 文档。

Overtrue\LaravelShoppingCart\Item 的属性

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

方法

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

事件

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

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

果酱云社区

点击跳转

  • 全网真正免费的IT课程平台

  • 专注于综合IT技术的在线课程,致力于打造优质、高效的IT在线教育平台

  • 课程方向包含Python、Java、前端、大数据、数据分析、人工智能等热门IT课程

  • 300+免费课程任你选择

点击跳转