tomshaw/shopcart

A modern easy to use Laravel shopping cart

v1.5.0 2024-04-19 13:16 UTC

README

GitHub Workflow Status issues forks stars GitHub license

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 and quantity.

$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.