PHPackages                             yzh52521/webman-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. yzh52521/webman-shop-cart

ActiveLibrary

yzh52521/webman-shop-cart
=========================

Shopping cart for webman plugin.

v0.1.2(4y ago)116MITPHPPHP &gt;=7.4

Since Apr 24Pushed 4y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

webman-shop-cart
================

[](#webman-shop-cart)

购物车在电商场景中基本是必须的一个模块

安装
--

[](#安装)

```
composer require yzh52521/webman-shop-cart

```

用法
--

[](#用法)

### 选择 Storage

[](#选择-storage)

您可以更改数据存储在 `config/plugin/yzh52521/shop-cart/app.php` 配置文件.

```
session

'storage' => \yzh52521\ShopCart\storage\SessionStorage::class,

datadate

'storage' => \yzh52521\ShopCart\storage\DatabaseStorage::class,  //tp-orm
or
'storage' => \yzh52521\ShopCart\storage\LaravelDatabaseStorage::class, // laravel
```

如果更改数据存储如果使用数据库存储，则需要创建数据表：

```
CREATE TABLE `shopping_cart`
(
    `key`         varchar(255) CHARACTER SET utf8 NOT NULL,
    `__raw_id`    varchar(255) CHARACTER SET utf8 NOT NULL,
    `guard`       varchar(255) CHARACTER SET utf8          DEFAULT NULL,
    `user_id`     int                                      DEFAULT NULL,
    `id`          int                             NOT NULL,
    `name`        varchar(255) CHARACTER SET utf8 NOT NULL,
    `qty`         int                             NOT NULL,
    `price`       decimal(8, 2)                   NOT NULL,
    `total`       decimal(8, 2)                   NOT NULL,
    `__model`     varchar(255) CHARACTER SET utf8          DEFAULT NULL,
    `type`        varchar(255) CHARACTER SET utf8          DEFAULT NULL,
    `status`      varchar(255) CHARACTER SET utf8          DEFAULT NULL,
    `attributes`  text CHARACTER SET utf8,
    `create_time` timestamp                       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` timestamp NULL DEFAULT NULL,
    PRIMARY KEY (`key`, `__raw_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

### 添加到购物车

[](#添加到购物车)

add 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 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 the items.

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

**example:**

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

### 获取一个商品

[](#获取一个商品)

Get the specified item.

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

**example:**

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

### 删除商品

[](#删除商品)

Remove the specified item by raw ID.

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

**example:**

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

### 清理购物车

[](#清理购物车)

Clean Shopping Cart.

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

**example:**

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

### 购物车总价格

[](#购物车总价格)

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

### 购物车商品个数

[](#购物车商品个数)

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

### 购物车商品数量

[](#购物车商品数量)

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 by property.

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

**example:**

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

### 检查购物车是否为空

[](#检查购物车是否为空)

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

### 指定关联的商品模型

[](#指定关联的商品模型)

Specifies the associated model of item.

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

**example:**

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.1'); //The cart name like cart.{guard}.{user_id}： cart.api.1
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'
```

购物车商品
=====

[](#购物车商品)

properties of `yzh52521\ShopCart\Item`:

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

方法：

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

事件
--

[](#事件)

 事件名参数`cart.adding`($attributes, $cart);`cart.added`($attributes, $cart);`cart.updating`($row, $cart);`cart.updated`($row, $cart);`cart.removing`($row, $cart);`cart.removed`($row, $cart);`cart.destroying`($cart);`cart.destroyed`($cart);License
=======

[](#license)

MIT

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

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 ~1 days

Total

3

Last Release

1478d 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 (8 commits)")

---

Tags

shoppingshopping cartwebman

### Embed Badge

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

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

###  Alternatives

[lukepolo/laracart

A simple cart for Laravel

583135.4k1](/packages/lukepolo-laracart)[amsgames/laravel-shop

Package set to provide shop or e-commerce functionality (such as CART, ORDERS, TRANSACTIONS and ITEMS) to Laravel for customizable builds.

4845.9k](/packages/amsgames-laravel-shop)[realrashid/cart

Meet Cart — your ultimate solution for seamless shopping cart functionality in Laravel applications. Cart simplifies the complexities of shopping cart operations, from product additions to total calculations, ensuring a frictionless user experience.

1093.6k](/packages/realrashid-cart)[ibrand/laravel-shopping-cart

Shopping cart for Laravel Application.

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

Simple yet customizable Laravel shopping cart

213.0k](/packages/yabhq-laravel-cart)[tinywan/rpc

simple rpc service for webman plugin

193.5k](/packages/tinywan-rpc)

PHPackages © 2026

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