itmosoft / laravel-cart
A reusable Laravel shopping cart package with persistent carts, items, quantity management, and price snapshots. Supports Laravel 12 and 13.
v1.0.1
2026-11-04 13:14 UTC
Requires
- php: ^8.3
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/database: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
Requires (Dev)
- orchestra/testbench: ^10.0 || ^11.0
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Persistent shopping carts for Laravel 12 and 13 / PHP 8.3+.
Features
- Persistent user and guest/session carts
- Polymorphic cart items: any Eloquent model can be added
- Quantity updates, item removal, and cart clearing
- Item option variants kept as separate cart lines
- Unit-price and line-total snapshots
- Configurable currency, table prefix, models, and limits
- Transactional cart writes and row-level locking
Install
composer require itmosoft/laravel-cart php artisan vendor:publish --tag=itmosoft-cart-config php artisan migrate
Create a cart
use ItmoSoft\Cart\Services\CartManager; $manager = app(CartManager::class); $cart = $manager->create( userId: auth()->id(), currency: 'USD', );
For a guest cart, pass a session identifier instead:
$cart = $manager->create(sessionId: session()->getId());
Add an item
$item = $manager->add( cart: $cart, item: $product, quantity: 2, unitPrice: $resolvedPrice, options: ['size' => 'M', 'color' => 'navy'], priceSnapshot: ['source' => 'catalog', 'unit_price' => $resolvedPrice], );
If unitPrice is omitted, the manager reads the model's price attribute. In production, pass the already-resolved price from your pricing layer. The package stores a price snapshot; it does not reprice or reserve inventory.
Manage cart
$manager->setQuantity($item, 3); $manager->remove($item); $manager->clear($cart); $subtotal = $manager->subtotal($cart);
Integration boundaries
- Product Catalog supplies product models; the cart accepts any Eloquent model.
- Pricing should resolve the price before adding/updating a line.
- Promotions and Tax are applied by the checkout/pricing orchestration layer; this package intentionally does not calculate discounts or tax.
- Inventory is not reserved by adding an item. Reservation belongs to checkout/order orchestration.
- This package does not create orders, process payments, or calculate shipping.
Testing
composer install vendor/bin/phpunit
License
MIT.