lalalili/laravelshoppingcart
Composer 安装命令:
composer require lalalili/laravelshoppingcart
包简介
Shopping cart package for Laravel 12/13 with PHP 8.3+ support.
README 文档
README
Shopping cart package for Laravel 11/12/13 with PHP 8.3+ support.
Requirements
- PHP
^8.3 - Laravel
^11.0|^12.0|^13.0
Installation
composer require lalalili/laravelshoppingcart:^14.0
Publish config:
php artisan vendor:publish --provider="Lalalili\\ShoppingCart\\ShoppingCartServiceProvider" --tag="config"
This publishes config/shopping_cart.php.
Quick start
use Lalalili\ShoppingCart\Facades\ShoppingCartFacade; ShoppingCartFacade::session('user-1')->add([ 'id' => 'sku-001', 'name' => 'Sample Item', 'price' => 100, 'quantity' => 2, 'attributes' => [], ]); $total = ShoppingCartFacade::session('user-1')->getTotal();
Batch operations
Use batch APIs when copying or rebuilding carts from stored state. They keep the same validation and quantity semantics as add()/remove(), but persist once per batch.
ShoppingCartFacade::session('user-1')->addMany([ ['id' => 'sku-001', 'name' => 'Sample Item', 'price' => 100, 'quantity' => 2], ['id' => 'sku-002', 'name' => 'Second Item', 'price' => 50, 'quantity' => 1], ]); ShoppingCartFacade::session('user-1')->removeMany(['sku-001', 'sku-002']);
Repeated subtotal and total calls are cached for the lifetime of the cart instance and are invalidated automatically when items, conditions, session keys, or formatting options change.
Extension points
v14.0 includes hooks for application-specific cart behavior:
return [ 'item_collection_class' => App\Support\CartItem::class, 'associated_model_resolver' => App\Support\CartModelResolver::class, 'rounding' => [ 'item_price_before_quantity' => 0, 'line_subtotal' => ['precision' => 0, 'mode' => 'half_up'], 'subtotal' => null, 'total' => null, ], ];
item_collection_class must extend Lalalili\ShoppingCart\ItemCollection.
associated_model_resolver may be a callable or implement Lalalili\ShoppingCart\Contracts\AssociatedModelResolverInterface.
Rounding rules may be null, an integer precision, or ['precision' => int, 'mode' => 'half_up|half_down|half_even|half_odd'].
Snapshots and total explanations
Use snapshots when a frontend, checkout review page, or regression test needs a stable cart payload.
$cart = ShoppingCartFacade::session('user-1'); $snapshot = $cart->snapshot(); $explain = $cart->explainTotals();
snapshot() includes items, cart conditions, context, subtotal, total, quantity, version, and hash.
explainTotals() includes the item, subtotal, and total condition steps used to produce the final total.
When pipelines have been run, snapshot() also includes their changed, warnings, and metadata results.
Checkout context and stale cart checks
Context stores checkout-specific inputs without changing item rows.
$cart = ShoppingCartFacade::session('user-1') ->withContext([ 'customer_id' => 123, 'channel' => 'web', 'currency' => 'TWD', 'coupon_codes' => ['WELCOME100'], ]); $hash = $cart->hash(); $cart->assertHash($hash);
assertHash() throws Lalalili\ShoppingCart\Exceptions\StaleCartException when another tab, request, or device has changed the cart.
Cart pipelines
Pipelines let applications attach promotion, gift, validation, or checkout transformations without subclassing the cart.
return [ 'pipelines' => [ 'before_totals' => [ App\Cart\Pipelines\ApplyPromotions::class, ], 'before_checkout' => [ App\Cart\Pipelines\ValidateInventory::class, ], ], ];
Pipeline classes implement Lalalili\ShoppingCart\Contracts\CartPipelineInterface.
before_totals runs automatically before subtotal and total calculations. Other stages may be triggered with runPipelines('before_checkout').
Optional Store API
The package can expose a small JSON cart API, disabled by default:
return [ 'api' => [ 'enabled' => true, 'require_hash' => false, 'prefix' => 'cart', 'middleware' => ['web'], ], ];
Available endpoints include GET /cart, POST /cart/items, POST /cart/items/batch, PATCH /cart/items/{id}, DELETE /cart/items/{id}, DELETE /cart/items, POST /cart/conditions, PUT /cart/context, and DELETE /cart.
Mutating endpoints validate their payloads and accept an optional cart hash precondition through If-Match, X-Cart-Hash, cart_hash, or hash. A stale hash returns HTTP 409; when api.require_hash is enabled, missing hashes return HTTP 428.
Configuration key
v14 uses shopping_cart config key.
config('shopping_cart.decimals');
v14.x compatibility promise
v14.xis non-breaking only.- Existing public API names stay stable in v14:
- Facade:
ShoppingCartFacade - Service provider:
ShoppingCartServiceProvider - Container key:
shopping_cart - Config key:
shopping_cart
- Facade:
- Additive APIs such as
addMany(),removeMany(),snapshot(), andwithContext()may be introduced in v14 minor releases. - Breaking changes are deferred to
v15.
Breaking changes in v14.0.0
- Config key changed from
lalalili_shopping_carttoshopping_cart. - Config publish target changed from
lalalili_shopping_cart.phptoshopping_cart.php. - Environment variables changed from
LALALILI_SHOPPING_*andSHOPPING_*toSHOPPING_CART_*. - The old
lalalili_shopping_cartconfig key is no longer read.
Testing and quality
composer test
composer test:pest
composer analyse
Benchmark
composer bench
Latest local benchmark baseline (v14.x, PHP 8.4, synthetic data):
| Items | Add (ms) | Update (ms) | getTotal x10 (ms) | Peak memory (MB) |
|---|---|---|---|---|
| 100 | 19.37 | 0.41 | 0.46 | 6.00 |
| 1,000 | 90.95 | 3.66 | 2.12 | 8.00 |
| 10,000 | 900.01 | 37.14 | 23.77 | 14.00 |
Release flow
Release checklist is available at .github/ISSUE_TEMPLATE/release-checklist.md.
Standard flow:
milestone close -> tag -> release -> packagist ping -> smoke check
License
MIT
lalalili/laravelshoppingcart 适用场景与选型建议
lalalili/laravelshoppingcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 904 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ecommerce」 「cart」 「laravel」 「shopping cart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lalalili/laravelshoppingcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lalalili/laravelshoppingcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lalalili/laravelshoppingcart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nevogate Payment Gateway SDK
Laravel 11 and above Shopping cart
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
Dealing with payments through the Egyptian payment gateway PayMob
universal shopping basket
A PHP (and Laravel) package to interface with the Snipcart api.
统计信息
- 总下载量: 904
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-13