sky2002/laravel-shopping-cart is a Laravel package for shopping cart package for laravel.
It currently has 0 GitHub stars and 184 downloads on Packagist (latest version 8.2.2.1).
Install it with composer require sky2002/laravel-shopping-cart.
Discover more Laravel packages by sky2002
or browse all Laravel packages to compare alternatives.
Last updated
This Shopping Cart is From melihovv/laravel-shopping-cart.
Because melihovv/laravel-shopping-cart has been archived. I copied the files and update.
Install via composer
composer require sky2002/laravel-shopping-cart
php artisan vendor:publish --provider="sky2002\ShoppingCart\ServiceProvider"
php artisan migrate
Regiser facade in config/app.php
'Cart' => 'sky2002\ShoppingCart\Facades\ShoppingCart',
or
use sky2002\ShoppingCart\Facades\ShoppingCart as Cart;
in the below examples.
The shopping cart gives you the following methods to use:
Add an item to the shopping cart.
$cartItem = Cart::add($id, $name, $price, $quantity);
$cartItem = Cart::add($id, $name, $price, $quantity, [
'color' => 'white',
]);
Remove the item with the specified unique id from the shopping cart. Unique id
is used to store items with the same $id, but different $options.
$cartItem = Cart::add($id, $name, $price, $quantity);
// ...
Cart::remove($cartItem->getUniqueId())
Check if the shopping cart contains the item with the specified unique id.
$cartItem = Cart::add($id, $name, $price, $quantity);
// ...
if (Cart::has($cartItem->getUniqueId())) {
// Do smth.
}
Get an item in the shopping cart by its unique id.
$cartItem = Cart::add($id, $name, $price, $quantity);
// ...
$cartItem = Cart::get($cartItem->getUniqueId());
Get all items in the shopping cart.
Clear the shopping cart.
Return number of items in the shopping cart. This method does not summarize quantities of item. For example, there are 3 books and 1 iPhone in the shopping cart, so this method returns 2.
Return total price of all items in the shopping cart.
Cart::add(1, 'iPhone 7', 500, 1);
Cart::add(1, 'iPad Pro', 700, 2);
Cart::getTotal(); // return 1900
The package supports multiple instances of the cart. Some examples:
Cart::instance('shopping')->add('192ao12', 'Product 1', 100, 10);
// Store and get the content of the 'shopping' cart
Cart::store->content();
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 50, 1, ['size' => 'medium']);
// Store and get the content of the 'wishlist' cart
Cart::store()->content();
// If you want to get the content of the 'shopping' cart again
Cart::instance('shopping')->restore()->content();
The default cart instance is called default, so when you're not using
instances,Cart::content(); is the same as Cart::instance('default')->content().
Set current instance name.
Get current instance name.
Currently there are two possible storage to persist shopping cart:
You can choose one by specifying repository class name in config
// config/shopping-cart.php
'repository' => \sky2002\ShoppingCart\Repositories\ShoppingCartDatabaseRepository::class,
// or
'repository' => \sky2002\ShoppingCart\Repositories\ShoppingCartRedisRepository::class,
In order to use redis storage you also need to install predis/predis package.
Persist current shopping cart instance to storage.
Cart::store($user->id);
Cart::instance('cart')->store($user->id);
Cart::instance('wishlist')->store($user->id);
Restore shopping cart instance to storage.
Cart::restore($user->id);
Cart::instance('cart')->restore($user->id);
Cart::instance('wishlist')->restore($user->id);
Remove shopping cart instance from storage.
Cart::destroy($user->id);
Cart::instance('cart')->destroy($user->id);
Cart::instance('wishlist')->destroy($user->id);
You can easily add discount coupons to shopping cart. Currently there are two types of coupons:
Related methods:
Add coupon to cart.
Cart::addCoupon(new FixedDiscountCoupon($name, $discount));
Cart::addCoupon(new PercentDiscountCoupon($name, 0.1)); // 10% discount
Returns all coupons.
Returns total price with applied coupons.
Cart::add(1, 'iPhone 7', 500, 1);
Cart::add(1, 'iPad Pro', 700, 2);
Cart::addCoupon(new FixedDiscountCoupon($name, 300));
Cart::getTotal(); // return 1900 - 300 = 1600