hnooz/laravel-shopping-cart
Composer 安装命令:
composer require hnooz/laravel-shopping-cart
包简介
A lightweight and flexible Laravel package for managing shopping carts with full support for cart items, session storage, and database persistence.
关键字:
README 文档
README
A flexible and easy-to-use Laravel shopping cart package that supports both database and session storage. Perfect for e-commerce applications that need to handle both guest and authenticated user shopping experiences.
Features
- Flexible Storage: Database, session, or hybrid storage options
- Multi-User Support: Seamlessly handles guest and authenticated users
- Easy-to-Use Facade: Simple, intuitive API
- Item Management: Add, remove, increase/decrease quantities
- Calculations: Automatic totals and item counts
- Well Tested: Comprehensive test coverage with Pest
- Laravel 12 Ready: Built for the latest Laravel version
- Clean Code: Uses Rector and Pint for code quality
Installation
You can install the package via composer:
composer require hnooz/laravel-shopping-cart
Publish and Run Migrations
Publish the migration file and run migrations:
php artisan vendor:publish --tag="laravel-cart-migrations"
php artisan migrate
Publish Configuration (Optional)
If you want to customize the configuration:
php artisan vendor:publish --tag="laravel-cart-config"
Configuration
The package comes with sensible defaults, but you can customize the behavior by publishing the config file:
return [ /* |-------------------------------------------------------------------------- | Cart Storage Driver |-------------------------------------------------------------------------- | | This option controls the default cart storage driver that will be used | to store cart items. You may set this to any of the storage options | listed below. | | Supported: "database", "session", "both" | */ 'driver' => env('CART_DRIVER', 'both'), /* |-------------------------------------------------------------------------- | Cart Database Connection |-------------------------------------------------------------------------- | | This is the database connection that will be used to store cart items | when using the "database" or "both" storage driver. | */ 'connection' => env('CART_DB_CONNECTION', null), /* |-------------------------------------------------------------------------- | Cart Items Table |-------------------------------------------------------------------------- | | This is the table that will be used to store cart items when using | the "database" or "both" storage driver. | */ 'table' => 'cart_items', /* |-------------------------------------------------------------------------- | Session Key |-------------------------------------------------------------------------- | | This is the session key that will be used to store cart items when | using the "session" or "both" storage driver. | */ 'session_key' => 'shopping_cart', ];
Usage
Basic Operations
use Hnooz\LaravelCart\Facades\Cart; // Add items to cart Cart::add('product-1', 'iPhone 14', 999.99, 1); Cart::add('product-2', 'MacBook Pro', 1999.99, 2, ['color' => 'Space Gray']); // Remove an item Cart::remove('product-1'); // Increase item quantity Cart::increase('product-2', 1); // Adds 1 more MacBook Pro // Decrease item quantity Cart::decrease('product-2', 1); // Removes 1 MacBook Pro // Get all cart items $items = Cart::all(); // Get item count $count = Cart::count(); // Total quantity of all items // Get cart total $total = Cart::total(); // Total price of all items // Clear entire cart Cart::clear();
Working with Item Options
You can store additional data with each cart item:
Cart::add('shirt-001', 'Cotton T-Shirt', 29.99, 2, [ 'size' => 'L', 'color' => 'Blue', 'customization' => 'Custom text on back' ]); $items = Cart::all(); foreach ($items as $item) { echo $item['name'] . ' - Size: ' . $item['options']['size']; }
Storage Drivers
Session Storage
Best for simple applications or when you don't need persistent carts:
// In config/cart.php or .env 'driver' => 'session' // or CART_DRIVER=session
Database Storage
Best for when you need persistent carts and user account integration:
// In config/cart.php or .env 'driver' => 'database' // or CART_DRIVER=database
Hybrid Storage (Recommended)
Uses session for guests and database for authenticated users:
// In config/cart.php or .env 'driver' => 'both' // or CART_DRIVER=both
Guest to User Cart Migration
When a guest user logs in, their session cart can be easily migrated:
// In your authentication logic use Hnooz\LaravelCart\Facades\Cart; // After user login if (session()->has('shopping_cart')) { // Cart items are automatically available // The package handles the transition seamlessly }
API Reference
Cart::add(string $id, string $name, float $price, int $quantity = 1, array $options = [])
Adds an item to the cart. If the item already exists, the quantity will be increased.
Parameters:
$id- Unique identifier for the item$name- Display name of the item$price- Price per unit$quantity- Quantity to add (default: 1)$options- Additional data array (default: [])
Cart::remove(string $id)
Removes an item completely from the cart.
Cart::increase(string $id, int $quantity = 1)
Increases the quantity of an existing item.
Cart::decrease(string $id, int $quantity = 1)
Decreases the quantity of an existing item. Minimum quantity is 1.
Cart::clear()
Removes all items from the cart.
Cart::all()
Returns all cart items as an array.
Cart::count()
Returns the total quantity of all items in the cart.
Cart::total()
Returns the total price of all items in the cart.
Advanced Usage
Using the Contract
You can type-hint the contract in your classes:
use Hnooz\LaravelCart\Contracts\CartInterface; class CheckoutService { public function __construct( protected CartInterface $cart ) {} public function processOrder() { $items = $this->cart->all(); $total = $this->cart->total(); // Process order... $this->cart->clear(); } }
Custom Cart Implementation
You can create your own cart implementation by implementing the CartInterface:
use Hnooz\LaravelCart\Contracts\CartInterface; class CustomCartManager implements CartInterface { // Implement all required methods... } // In a service provider $this->app->bind(CartInterface::class, CustomCartManager::class);
Testing
The package comes with comprehensive tests. To run the tests:
composer test
Code Quality
The package uses several tools to maintain code quality:
# Run Pint for style fix composer style-fix # Run rector for code improvements composer rector-dry composer rector-fix
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
hnooz/laravel-shopping-cart 适用场景与选型建议
hnooz/laravel-shopping-cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 572 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cart」 「laravel」 「laravel-cart」 「hnooz」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hnooz/laravel-shopping-cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hnooz/laravel-shopping-cart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hnooz/laravel-shopping-cart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications
Laravel 11 and above Shopping cart
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
universal shopping basket
A PHP (and Laravel) package to interface with the Snipcart api.
Shopping cart for Laravel with session, cookie, and database storage, Livewire components, and REST API
统计信息
- 总下载量: 572
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-24