tomshaw / shopcart
A modern easy to use Laravel shopping cart
Installs: 33
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tomshaw/shopcart
Requires
- php: ^8.4|^8.5
- illuminate/events: ^11.0|^12.0
- illuminate/session: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.20
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^3.0|^4.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0|^12.0
- dev-master
- v2.0.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- dev-release-please--branches--master
- dev-dependabot/github_actions/actions/checkout-6
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-7
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.6
- dev-tomshaw-patch-1
This package is auto-updated.
Last update: 2026-01-23 01:54:27 UTC
README
ShopCart is a modern easy to use Laravel shopping cart...
Installation
You can install the package via composer:
composer require tomshaw/shopcart
Publish configuration file
php artisan vendor:publish --provider="TomShaw\ShopCart\Providers\ShopCartServiceProvider" --tag=config
Requirements
- PHP 8.4+
- Laravel 12
Basic Usage
Adding an item to the shopping cart.
Note: Cart item constructor properties are validated when creating or updating cart items.
Note: A unique random integer
rowIdis created and used to identify cart items.
use TomShaw\ShopCart\{Cart, CartItem}; $cartItem = CartItem::make(id: $product->id, name: $product->name, quantity: 1, price: $product->price); Cart::add($cartItem);
Adding an item with product options to the shopping cart.
$cartItem = CartItem::make($product->id, $product->name, 1, $product->price); $cartItem->options['size'] = 'XL'; $cartItem->options['logo'] = 'Laravel Rocks'; Cart::add($cartItem);
Accessing product options.
// Array access $size = $cartItem->options['size']; // Or using Collection methods $size = $cartItem->options->get('size'); $cartItem->options->put('color', 'blue'); $hasSize = $cartItem->options->has('size');
Updating an item and product options in the shopping cart.
$cartItem = Cart::all()->where('id', '===', $id)->first(); $cartItem->quantity = 5; $cartItem->options['size'] = '2XL'; Cart::update($cartItem);
Removing an item from the shopping cart.
Cart::remove(Cart::get($rowId));
Deleting the shopping cart after checkout.
Cart::forget();
Cart Totals
Sums the properties:
tax,price,subtotalandquantity.
$totalPrice = Cart::total('price');
$totalQuantity = Cart::total(property: 'quantity', numberFormat: false);
$subTotal = Cart::total('subtotal');
$totalTax = Cart::total('tax');
Computed Properties
Cart items automatically calculate subTotal, totalTax, and totalPrice. These values are computed on-demand and are always accurate.
$cartItem = CartItem::make(id: 1, name: 'Product', quantity: 2, price: 100.00, tax: 8.5); // Automatically computed properties echo $cartItem->subTotal; // 200.00 (quantity * price) echo $cartItem->totalTax; // 17.00 (subTotal * tax / 100) echo $cartItem->totalPrice; // 217.00 (subTotal + totalTax) // Values update automatically when properties change $cartItem->quantity = 3; echo $cartItem->subTotal; // 300.00 (automatically recalculated)
Tax Rates
To set a default tax rate add the following environment variable in your application .env.
SHOPCART_DEFAULT_TAXRATE=9.547
You can easily apply item specific tax rates at run time.
use TomShaw\ShopCart\{Cart, CartItem}; Cart::add(CartItem::make(tax: 6.250, ...));
Number Formatting
Number formating is handled by adding the following environment variables to your application .env.
SHOPCART_DECIMALS=2 SHOPCART_DECIMAL_SEPARATOR="." SHOPCART_THOUSANDS_SEPARATOR=","
Cart Methods
Get item from collection by rowId.
$cartItem = Cart::get($rowId);
Check if cart item exists by rowId.
$boolean = Cart::has($rowId);
Get cart as collection or array.
$cartItems = Cart::all(bool $toArray = false);
Use Laravel Collection methods for advanced operations.
// Search for specific cart items $cartItems = Cart::all()->where('id', '===', $productId); // Check if cart is empty $isEmpty = Cart::all()->isEmpty(); $isNotEmpty = Cart::all()->isNotEmpty(); // Filter, map, or use any Collection method $total = Cart::all()->sum('totalPrice'); $firstItem = Cart::all()->first();
Casting the cart as an array or json;
Cart::toArray();
Cart::toJson();
Changelog
For changes made to the project, see the Changelog.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). See License File for more information.