ingenius / shopcart
Ingenius ShopCart Package
Requires
- php: ^8.2
- ingenius/auth: *
- ingenius/core: *
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^11.0
README
A Laravel package for implementing shopping cart functionality with modifiers support.
Installation
composer require ingenius/shopcart
Features
- Add products to cart (supports polymorphic relationships)
- Remove products from cart
- Delete cart items
- Get cart items
- Cart modifiers system for extending functionality
- Session-based cart for guests
- User-based cart for authenticated users
Configuration
Environment Variables
PRODUCT_MODEL=Ingenius\Products\Models\Product
Note: For backward compatibility,
SHOPCART_PRODUCT_MODEL
is still supported butPRODUCT_MODEL
is preferred as it's used across all packages.
ShopCart Modifier System
The ShopCart package includes a modifier system that allows other packages to extend the cart's functionality without the ShopCart package needing to know about them.
Features
- Add custom calculations to the cart total (shipping, discounts, taxes, etc.)
- Extend the cart's JSON/array output with additional data
- Control the execution order of modifiers using priorities
- Easy to implement in other packages
How It Works
The ShopCart uses a pipeline approach with modifiers that:
- Calculate subtotals in priority order (lower numbers run first)
- Extend the
toArray()
output with custom values from each modifier - Allow precise control over execution order
Creating Your Own Modifier
To create a new cart modifier in your package:
- Create a class that extends
BaseCartModifier
or implementsCartModifierInterface
- Set its priority to control when it runs (lower = earlier)
- Register it with the
CartModifierManager
in your package's service provider
Example Modifier
<?php namespace YourPackage\CartModifiers; use Ingenius\ShopCart\Modifiers\BaseCartModifier; use Ingenius\ShopCart\Services\ShopCart; class YourModifier extends BaseCartModifier { /** * Set priority - lower numbers run first */ public function getPriority(): int { return 50; // Run after the base item subtotal calculations } /** * Modify the subtotal */ public function calculateSubtotal(ShopCart $cart, float $currentSubtotal): float { // Your custom calculation logic here $yourModification = 10.00; // Store data for use in extendCartArray $this->yourData = $yourModification; // Return modified subtotal return $currentSubtotal + $yourModification; } /** * Add data to the cart array */ public function extendCartArray(ShopCart $cart, array $cartArray): array { $cartArray['your_data'] = [ 'amount' => $this->yourData, 'other_info' => 'Custom information' ]; return $cartArray; } protected $yourData; }
Registering Your Modifier
In your package's service provider:
<?php namespace YourPackage\Providers; use Illuminate\Support\ServiceProvider; use Ingenius\ShopCart\Services\CartModifierManager; use YourPackage\CartModifiers\YourModifier; class YourPackageServiceProvider extends ServiceProvider { public function boot(): void { // Register your cart modifier $this->app->afterResolving(CartModifierManager::class, function (CartModifierManager $manager) { $manager->register(new YourModifier()); }); } }
Command Line Tools
The ShopCart package includes command line tools to help you manage and debug cart modifiers.
Listing Registered Cart Modifiers
To view all registered cart modifiers and their priorities, use the following command:
php artisan shopcart:modifiers
Usage
// Usage examples will go here
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.