tomshaw / shopcart
A modern easy to use Laravel shopping cart
Requires
- php: ^8.1|^8.2|^8.3|^8.4
- illuminate/events: ^10.14|^11.0
- illuminate/session: ^10.14|^11.0
- illuminate/support: ^10.14|^11.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.10|^2.0
- mockery/mockery: ^1.4.4
- orchestra/testbench: ^8.5|^9.0
- pestphp/pest: ^2.8|^3.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1.1|^11.0
This package is auto-updated.
Last update: 2025-03-22 23:58:54 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
The package is compatible with Laravel 10 and 11.
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
rowId
is 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->size = 'XL'; $cartItem->logo = 'Laravel Rocks'; Cart::add($cartItem);
Updating an item and product options in the shoping cart.
$cartItem = Cart::where('id', '===', $id)->first(); $cartItem->quantity = 5; $cartItem->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
,subtotal
andquantity
.
$totalPrice = Cart::total('price');
$totalQuantity = Cart::total(property: 'quantity', numberFormat: false);
$subTotal = Cart::total('subtotal');
$totalTax = Cart::total('tax');
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=","
Proxy 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);
Searching for specific cart items.
$cartItems = Cart::where('id', '===', $productId);
Check if the cart is empty or not.
Cart::isEmpty();
Cart::isNotEmpty();
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.