An open source, simple in syntax, and ready to use package for Shopping Carts managing with Sessions from Backend, with Laravel.
- Laravel 8 or above
composer require sebacarrasco93/mycart
Adding item(s):
$itemOne = [
'uuid' => '111AAA',
'name' => "Lemon Waffle by SoloWaffles",
'price' => '8.99'
];
$itemTwo = [
'uuid' => '222BBB',
'name' => "Mixed Waffle by SoloWaffles",
'price' => '9.95'
];
MyCart::add($itemOne);
MyCart::add($itemTwo);
Getting item(s):
return MyCart::get(); // returns an array [$itemOne, $itemTwo]
Counting item(s):
return MyCart::count(); // returns 2
Calculating total:
return MyCart::total(); // returns 18.94
Finding by UUID:
return MyCart::findByUuid('222BBB'); // returns $itemOne
You can find a Laravel implementation here
MyCart uses 3 names:
Name | Default value | What does it |
---|---|---|
SESSION_NAME | mycart | Name of the mycart's session |
ITEMS_NAME | items | Name of the key's session |
PRICE_NAME | price | Name of an item's price |
Depending on what you need, you can prefer one or another solution. However, there are two ways to do this:
You need to copy and change these values:
MYCART_SESSION_NAME=your_custom_session_name
MYCART_ITEMS_NAME=your_custom_items_name
MYCART_PRICE_NAME=your_custom_price_field_name
Simply run:
php artisan vendor:publish --provider="SebaCarrasco93\MyCart\MyCartServiceProvider"
Laravel will publish a configuration file in config/mycart.php
, so you can change the values by opening it.
'session_name' => env('MYCART_SESSION_NAME', 'your_custom_session_name'),
'items_name' => env('MYCART_ITEMS_NAME', 'your_custom_items_name'),
'price_name' => env('MYCART_PRICE_NAME', 'your_custom_price_field_name'),
Since behind the scenes it works with Laravel's sessions, MyCart will work as another Laravel session.
So, for example, if you want to increase the maximum lifetime from default, 2 hours (120 minutes) to 24 hours (1440 minutes), the only thing you need to change is the SESSION_LIFETIME
value in your project's .env
file
Before (2 hours):
SESSION_LIFETIME=120
After (one day):
SESSION_LIFETIME=1440
Easy Peasy!