yzh52521/think-shop-cart

适用于 ThinkPHP 应用的购物车。

v0.63 2023-03-07 08:39 UTC

This package is auto-updated.

Last update: 2024-09-07 12:35:29 UTC


README

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

  1. 购物车数据支持数据库存储
  2. Item 增加模型属性返回。因为购物车可能是SPU或者SKU,所以可以直接通过模型属性返回相关对象。
  3. 支持多Guard。包括商城购物车和导购购物车。

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

安装

composer require yzh52521/think-shop-cart:~0.5

会话初始化

app\middleware.php

return [
    // Session初始化
    \think\middleware\SessionInit::class,
];

使用方法

选择存储

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

'storage' => \yzh52521\ShoppingCart\storage\DatabaseStorage::class,
  
'storage' => \yzh52521\ShoppingCart\storage\SessionStorage::class,

如果您使用数据库存储,需要执行以下操作:

发布配置文件和数据库迁移文件:

php think tauthz:publish

执行迁移工具(确保数据库配置信息正确):

php think migrate:run

这将创建名为 shopping_cart 的表。

向购物车添加项目

添加一个新项目。

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

示例

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

示例

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

获取所有项目

获取所有项目。

Collection ShopCart::all();

示例

$items = ShopCart::all();

获取项目

获取指定的项目。

Item ShopCart::get(string $rawId);

示例

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

删除项目

通过原始 ID 删除指定的项目。

boolean ShopCart::remove(string $rawId);

示例

ShopCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

销毁购物车

清理购物车。

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

示例

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

总价

返回所有项目的总价。

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

示例

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

行数统计

返回行数。

int ShopCart::countRows();

示例

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

数量统计

返回所有项目的数量

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

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

示例

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

搜索项目

通过属性搜索项目。

Collection ShopCart::search(array $conditions);

示例

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

检查空

bool ShopCart::isEmpty();

指定关联模型

指定项目关联的模型。

Cart ShopCart::associate(string $modelName);

session 示例:

ShopCart::associate('app\model\Goods');
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'

database 示例:

ShopCart::associate('app\model\Goods');
ShopCart::name('web.12');
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'

集合与项目

yzh52521\ShoppingCart\Item 的属性

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

以及方法

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

许可

MIT