PHPackages                             yzh52521/think-shop-cart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yzh52521/think-shop-cart

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yzh52521/think-shop-cart
========================

Shopping cart for ThinkPHP Application.

v0.63(3y ago)339MITPHPPHP &gt;=7.1.0

Since Apr 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/yzh52521/think-shop-cart)[ Packagist](https://packagist.org/packages/yzh52521/think-shop-cart)[ RSS](/packages/yzh52521-think-shop-cart/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (4)Dependencies (1)Versions (8)Used By (0)

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

1. 购物车数据支持 Database 存储
2. Item 增加 Model 属性返回。因为购物车可能是SPU或者SKU，因此直接通过 model 属性直接返回相关对象。
3. 支持多 Guard. 商城购物车和导购购物车。

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

Installation
------------

[](#installation)

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

```

### 会话初始化

[](#会话初始化)

`app\middleware.php`

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

Usage
-----

[](#usage)

### Select Storage

[](#select-storage)

You can change data Storage in `config/cart.php` file.

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

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

If you use Database Storage, you need to execute

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

```
php think tauthz:publish

```

执行迁移工具（**确保数据库配置信息正确**）：

```
php think migrate:run

```

这将创建名为 `shopping_cart` 的表。

### Add item to cart

[](#add-item-to-cart)

Add a new item.

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

**example:**

```
$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
...
```

### Update item

[](#update-item)

Update the specified item.

```
Item ShopCart::update(string $rawId, int $quantity);
Item ShopCart::update(string $rawId, array $arrtibutes);
```

**example:**

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

### Get all items

[](#get-all-items)

Get all the items.

```
Collection ShopCart::all();
```

**example:**

```
$items = ShopCart::all();
```

### Get item

[](#get-item)

Get the specified item.

```
Item ShopCart::get(string $rawId);
```

**example:**

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

### Remove item

[](#remove-item)

Remove the specified item by raw ID.

```
boolean ShopCart::remove(string $rawId);
```

**example:**

```
ShopCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');
```

### Destroy cart

[](#destroy-cart)

Clean Shopping Cart.

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

**example:**

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

### Total price

[](#total-price)

Returns the total of all items.

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

**example:**

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

### Count rows

[](#count-rows)

Return the number of rows.

```
int ShopCart::countRows();
```

**example:**

```
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
```

### Count quantity

[](#count-quantity)

Returns the quantity of all items

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

`$totalItems` : When `false`,will return the number of rows.

**example:**

```
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)
```

### Search items

[](#search-items)

Search items by property.

```
Collection ShopCart::search(array $conditions);
```

**example:**

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

### Check empty

[](#check-empty)

```
bool ShopCart::isEmpty();
```

### Specifies the associated model

[](#specifies-the-associated-model)

Specifies the associated model of item.

```
Cart ShopCart::associate(string $modelName);
```

session **example:**

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

database **example:**

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

The Collection And Item
=======================

[](#the-collection-and-item)

properties of `yzh52521\ShoppingCart\Item`:

- `id` - your goods item ID.
- `name` - Name of item.
- `qty` - Quantity of item.
- `price` - Unit price of item.
- `total` - Total price of item.
- `__raw_id` - Unique ID of row.
- `__model` - Name of item associated Model.
- ... custom attributes.

And methods:

- `rawId()` - Return the raw ID of item.

License
=======

[](#license)

MIT

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~114 days

Recently: every ~170 days

Total

7

Last Release

1164d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15060466?v=4)[听风吹雨](/maintainers/yuanzhihai)[@yuanzhihai](https://github.com/yuanzhihai)

---

Top Contributors

[![yuanzhihai](https://avatars.githubusercontent.com/u/15060466?v=4)](https://github.com/yuanzhihai "yuanzhihai (23 commits)")

---

Tags

shoppingshopping cartthinkphp

### Embed Badge

![Health badge](/badges/yzh52521-think-shop-cart/health.svg)

```
[![Health](https://phpackages.com/badges/yzh52521-think-shop-cart/health.svg)](https://phpackages.com/packages/yzh52521-think-shop-cart)
```

###  Alternatives

[ibrand/laravel-shopping-cart

Shopping cart for Laravel Application.

385.6k1](/packages/ibrand-laravel-shopping-cart)[extcode/cart

Shopping Cart(s) for TYPO3

57119.3k14](/packages/extcode-cart)[liliuwei/thinkphp-jump

适用于thinkphp6.0的跳转扩展

2874.4k1](/packages/liliuwei-thinkphp-jump)[xiaodi/think-pullword

ThinkPHP 分词/抽词 扩展包

5512.1k](/packages/xiaodi-think-pullword)[isszz/rotate-captcha

Rotate image captcha

801.7k](/packages/isszz-rotate-captcha)[syscover/shopping-cart

Shopping Cart package

299.1k1](/packages/syscover-shopping-cart)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
