lombervid/shoppingcart
Composer 安装命令:
composer require lombervid/shoppingcart
包简介
A simple shopping cart class for PHP
关键字:
README 文档
README
ShoppingCart PHP Class
ShoppingCart is a simple PHP package that provides you with a simple shopping cart implementation stored in session.
Installation
Composer
You can install it using composer:
composer require lombervid/shoppingcart
Usage
Create an instance of ShoppingCart class.
use Lombervid\ShoppingCart\ShoppingCart; $shoppingCart = new ShoppingCart();
Add items
You can add items calling the method add() passing an Item instance as parameter.
use Lombervid\ShoppingCart\Item; use Lombervid\ShoppingCart\ShoppingCart; $cart = new ShoppingCart(); $cart->add(new Item('1', 'Cake', 15.56)); $cart->add(new Item('15', 'Frappe', 5)); foreach ($cart->items() as $item) { // $item->id // $item->name }
at this point your $cart->items() will look like this:
array:2 [▼ 1 => Lombervid\ShoppingCart\Item {#5 ▼ -id: "1" -name: "Cake" -price: 15.56 } 15 => Lombervid\ShoppingCart\Item {#6 ▼ -id: "15" -name: "Frappe" -price: 5.0 } ]
Add extra fields to your item
You can also add extra fields (such as price, name, etc) to your item. The Item constructor receives a parameter fields which is an Array with the following structure:
[
'field_name' => 'field_value',
'field_2_name' => 'field_2_value'
]
when you provide the $fields param, each field of the array is added to your item.
$fields = [ 'size' => 'XL', 'color' => 'blue' ]; $item = new Item('23', 'My Shirt', 2.5, fields: $fields); $cart->add($item);
with the above code your $cart->items() will look line:
array:1 [▼ 23 => Lombervid\ShoppingCart\Item {#5 ▼ -id: "23" -name: "My Shirt" -price: 2.5 -qty: 1 -fields: array:2 [▼ "size" => "XL" "color" => "blue" ] } ]
Then you can access any extra field as if they were properties:
foreach ($cart->items() as $item) { // $item->size // $item->color }
Remove items
You can remove an item from the cart calling the method remove($id) which receive item's $id as parameter.
$cart->remove(23);
Clear the cart
You can clear the cart calling the method clear() which removes all the items from the cart.
$shoppingCart->clear();
Advanced options
ShoppingCart
Cart options
It is an array of options. The default value is:
[
'name' => 'shopping_cart',
'autosave' => true,
'tax' => 0,
'shipping' => [
'amount' => 0,
'free' => 0,
],
]
| Option | Type | Default | Description |
|---|---|---|---|
name |
string |
shopping_cart |
Cart's name. Used to save the cart in storage |
autosave |
bool |
true |
If set to true, cart is saved when object is destroyed |
tax |
int|float |
0 |
Porcentaje to be used as tax (0 - 100) |
shipping.amount |
int|float |
0 |
Shipping cost |
shipping.free |
int|float |
0 |
Value after which shipping will be free. 0 to disable |
Constructor
| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
$options |
array |
See Cart options | false |
Cart options |
$storage |
StorageInterface |
NativeSessionStorage |
false |
Storage driver |
Methods
| Name | Description |
|---|---|
add(Item $item, bool $append = true): void |
Add an item to the cart |
remove(string $id): bool |
Remove an item from the cart |
subtotal(): float |
Get subtotal |
shipping(): float |
Get shipping cost |
tax(): float |
Get tax |
total(): float |
Get total |
inCart(string $id): bool |
Check if an item is in the cart |
items(): array |
Return the items in the cart |
clear(): static |
Remove all items from the cart |
isEmpty(): bool |
Check if the cart is empty |
totalItems(): int |
Return the total (distinct) items in the cart |
save(): void |
Save items in the storage |
toArray(): array |
Return items as array |
Item
Constructor
| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
$id |
string |
N/A |
true |
Identifier |
$name |
string |
N/A |
true |
Name / description |
$price |
float |
N/A |
true |
Price (>= 0) |
$qty |
int |
1 |
false |
Quantity (> 0) |
$fields |
array |
[] |
false |
Extra fields |
$discount |
float |
0 |
false |
Discount (>= 0) |
Methods
| Name | Description |
|---|---|
add(int $qty): void |
Increase item's quantity by $qty |
update(int $qty): void |
Update item's quantity to $qty |
get(string $name, mixed $default = null): mixed |
Get $name property/field |
hasDiscount(): bool |
Check if has a discount |
price(): float |
Get item's price |
total(): float |
Get total |
toArray(): array |
Return item as array |
Contributing
Refer to CONTRIBUTING for information.
License
lombervid/shoppingcart 适用场景与选型建议
lombervid/shoppingcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 406 次下载、GitHub Stars 达 11, 最近一次更新时间为 2023 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「shopping cart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lombervid/shoppingcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lombervid/shoppingcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lombervid/shoppingcart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel 11 and above Shopping cart
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
universal shopping basket
Google Shopping Feed API (with ns problem fixed)
A PHP (and Laravel) package to interface with the Snipcart api.
Yii2 extension that adds shopping cart functions
统计信息
- 总下载量: 406
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-01