A flexible shopping cart package for Laravel with support for session or database storage, conditional pricing, cart merging, rules engine, and custom product attributes.
composer require daikazu/flexicart
php artisan vendor:publish --tag="flexicart-config"
php artisan vendor:publish --tag="flexicart-migrations"
php artisan migrate
Then update your .env file:
CART_STORAGE=database
use Daikazu\Flexicart\Facades\Cart;
// Add item as array
Cart::addItem([
'id' => 1,
'name' => 'Product Name',
'price' => 29.99,
'quantity' => 2,
'attributes' => [
'color' => 'red',
'size' => 'large'
]
]);
// Add multiple items at once
Cart::addItem([
[
'id' => 2,
'name' => 'Another Product',
'price' => 15.50,
'quantity' => 1
],
[
'id' => 3,
'name' => 'Third Product',
'price' => 45.00,
'quantity' => 3
]
]);
// Update quantity
Cart::updateItem('item_id', ['quantity' => 5]);
// Update attributes
Cart::updateItem('item_id', [
'attributes' => [
'color' => 'blue',
'size' => 'medium'
]
]);
// Update multiple properties
Cart::updateItem('item_id', [
'quantity' => 3,
'price' => 25.99,
'attributes' => ['color' => 'green']
]);
// Remove a specific item
Cart::removeItem('item_id');
// Clear all items from the cart
Cart::clear();
// Clear all items and conditions from cart
Cart::reset();
// Get all items
$items = Cart::items();
// Get a specific item
$item = Cart::item('item_id');
// Get cart counts
$totalItems = Cart::count(); // Total quantity of all items
$uniqueItems = Cart::uniqueCount(); // Number of unique items
// Check if cart is empty
$isEmpty = Cart::isEmpty();
// Get cart totals
$subtotal = Cart::subtotal(); // Subtotal before conditions
$total = Cart::total(); // Final total after all conditions
$taxableSubtotal = Cart::getTaxableSubtotal(); // Subtotal of taxable items only
Conditions are adjustments that can be applied to cart items or the entire cart:
Conditions can target:
use Daikazu\Flexicart\Conditions\Types\PercentageCondition;
use Daikazu\Flexicart\Conditions\Types\FixedCondition;
use Daikazu\Flexicart\Enums\ConditionTarget;
// Add a 10% discount to the cart
$discount = new PercentageCondition(
name: '10% Off Sale',
value: -10, // Negative for discount
target: ConditionTarget::SUBTOTAL
);
Cart::addCondition($discount);
// Add a $5 shipping fee
$shipping = new FixedCondition(
name: 'Shipping Fee',
value: 5.00,
target: ConditionTarget::SUBTOTAL
);
Cart::addCondition($shipping);
// Add condition to a specific item
$itemDiscount = new PercentageCondition(
name: 'Item Discount',
value: -20,
target: ConditionTarget::ITEM
);
Cart::addItemCondition('item_id', $itemDiscount);
// Remove a specific condition from the cart
Cart::removeCondition('10% Off Sale');
// Remove a condition from a specific item
Cart::removeItemCondition('item_id', 'Item Discount');
// Clear all cart conditions
Cart::clearConditions();
// Add non-taxable item
Cart::addItem([
'id' => 4,
'name' => 'Non-taxable Service',
'price' => 100.00,
'quantity' => 1,
'taxable' => false
]);
// Update existing item to be non-taxable
Cart::updateItem('item_id', ['taxable' => false]);
taxable FlagUse PercentageTaxCondition or FixedTaxCondition to apply tax. Tax is calculated on a tax base composed of:
taxable attribute is true, plustaxable flag is true.Conditions default to taxable: false, which means they do not affect the tax base. Set taxable: true on a condition when its amount should be taxed (e.g. a taxable shipping fee) or when a discount should reduce the tax base.
use Daikazu\Flexicart\Conditions\Types\FixedCondition;
use Daikazu\Flexicart\Conditions\Types\PercentageCondition;
use Daikazu\Flexicart\Conditions\Types\PercentageTaxCondition;
// Non-taxable shipping — added to total, NOT added to the tax base.
$shipping = new FixedCondition(
name: 'Shipping',
value: 10.00,
taxable: false // default
);
// Taxable shipping — added to total AND added to the tax base.
$taxableShipping = new FixedCondition(
name: 'Shipping',
value: 10.00,
taxable: true
);
// Discount that does NOT reduce the tax base (tax is calculated on the pre-discount amount).
$discount = new PercentageCondition(
name: 'Discount',
value: -10,
taxable: false // default
);
// Discount that DOES reduce the tax base by its full amount (tax is calculated on the post-discount amount).
$discountReducingTax = new PercentageCondition(
name: 'Discount',
value: -10,
taxable: true
);
// Apply sales tax. The tax is computed against the tax base described above.
$tax = new PercentageTaxCondition(name: 'Sales Tax', value: 8.25);
Cart::addCondition($shipping);
Cart::addCondition($discount);
Cart::addCondition($tax);
| Condition | Effect on total | Effect on tax base |
|---|---|---|
Fee with taxable: false (default) |
Adds to total | No change |
Fee with taxable: true |
Adds to total | Adds full fee amount |
Discount with taxable: false |
Reduces total | No change |
Discount with taxable: true |
Reduces total | Reduces by full amount |
The same rule applies to item-level conditions: an item condition with taxable: false is included in the item's subtotal but excluded from the tax base.
For detailed documentation on specific features, see the following guides:
| Guide | Description |
|---|---|
| Configuration | Storage options, currency settings, cleanup, and all config options |
| Rules Engine | Advanced promotional rules: Buy X Get Y, thresholds, tiered discounts |
| Cart Merging | Merge strategies for guest-to-user carts, wishlists, and cart recovery |
| Events | Cart lifecycle events for analytics, inventory, and integrations |
| Working with Prices | Price object API, arithmetic operations, and formatting |
| Blade Templates | Examples for displaying cart data in Blade views |
| Extending FlexiCart | Custom conditions, storage drivers, models, and merge strategies |
composer test
Cart data not persisting between requests
CART_STORAGE environment variable is set correctlyPrice calculation errors
Condition not applying correctly
Memory issues with large carts
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?