How to Use Laravel Cart Packages

Introduction
Laravel cart packages are tools that help you create and manage shopping carts in your Laravel applications. They provide features such as adding, updating, removing, and retrieving items, applying taxes and discounts, associating models, and storing cart data in sessions or databases. In this article, we will explore some of the popular Laravel cart packages and how to use them.
Laravel Shopping Cart by Darryldecode
This package is a shopping cart implementation for Laravel 5, 6, 7, and 9. It supports multiple cart instances, session-based storage, model association, and taxation. It also allows you to apply conditions on the cart or item level, such as discounts, shipping costs, or service charges. You can find the documentation here.
To install this package, run the following command:
composer require "darryldecode/cart:~4.0"Then, add the service provider and the alias to your config/app.php file:
'providers' => [
// ...
Darryldecode\Cart\CartServiceProvider::class,
],
'aliases' => [
// ...
'Cart' => Darryldecode\Cart\Facades\CartFacade::class,
],Optionally, you can publish the configuration file using this command:
php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --tag="config"To use this package, you need to call the Cart::session($sessionKey) method before any other methods. The $sessionKey is a unique identifier for your cart, which you can use to bind it to a specific user.
For example:
$userId = 2; // the user ID to bind the cart contents
Cart::session($userId)->add(array(
'id' => 456, // generate a unique() row ID
'name' => 'Sample Item',
'price' => 67.99,
'quantity' => 4,
'attributes' => array(),
'associatedModel' => 'Product'
));This will add an item to the cart with the session key of 2. You can also update, remove, or get the items using the same session key.
For example:
Cart::session($userId)->update(456, [
'quantity' => 2, // new quantity
]);
Cart::session($userId)->remove(456);
$items = Cart::session($userId)->getContent();To apply conditions on the cart or item level, you need to create a CartCondition object and pass it to the Cart::condition() or Cart::addItemCondition() methods.
For example:
// create a 10% tax condition
$taxCondition = new \Darryldecode\Cart\CartCondition(array(
'name' => 'TAX 10%',
'type' => 'tax',
'target' => 'total', // this condition will be applied to cart's total when getTotal() is called.
'value' => '10%',
));
// apply condition to cart
Cart::session($userId)->condition($taxCondition);
// create a 5% discount condition
$discountCondition = new \Darryldecode\Cart\CartCondition(array(
'name' => 'DISCOUNT 5%',
'type' => 'discount',
'target' => 'item', // this condition will be applied to each item when getSubTotal() is called.
'value' => '-5%',
));
// apply condition to an item with ID of 456
Cart::session($userId)->addItemCondition(456, $discountCondition);To get the cart’s total or subtotal, you can use the Cart::getTotal() or Cart::getSubTotal() methods.
For example:
$total = Cart::session($userId)->getTotal();
$subtotal = Cart::session($userId)->getSubTotal();Laravel Cart by JackieDo
This package is a tool to create and manage carts in Laravel applications. It supports commercial and non-commercial carts, multiple cart instances, grouping carts, model association, and taxation. It also allows you to apply actions on the cart and item level, such as discounts, service charges, shipping costs, or coupons. You can find the documentation here.
To install this package, run the following command:
composer require jackiedo/cart
Then, add the service provider and the alias to your config/app.php file:
'providers' => [
// ...
JackieDo\Cart\CartServiceProvider::class,
],
'aliases' => [
// ...
'Cart' => JackieDo\Cart\Facades\Cart::class,
],Optionally, you can publish the configuration file using this command:
php artisan vendor:publish --provider="JackieDo\Cart\CartServiceProvider" --tag="config"To use this package, you need to create a cart instance using the Cart::make($instanceName, $options) method. The $instanceName is a unique identifier for your cart, and the $options is an array of configuration options.
For example:
$cart = Cart::make('shopping', [
'isCommercial' => true, // this is a commercial cart
'group' => 'default', // this cart belongs to the default group
'tax' => 10, // this cart has a 10% tax rate
]);This will create a cart instance with the name of shopping. You can also get an existing cart instance using the Cart::get($instanceName) method. For example:
$cart = Cart::get('shopping');To add an item to the cart, you need to use the Cart::addItem($itemData) method. The $itemData is an array of item information, such as id, name, price, quantity, attributes, and model. For example:
$cart->addItem([
'id' => 456,
'name' => 'Sample Item',
'price' => 67.99,
'quantity' => 4,
'attributes' => [],
'model' => 'Product'
]);This will add an item to the cart. You can also update, remove, or get the items using the Cart::updateItem($itemId, $itemData), Cart::removeItem($itemId), or Cart::getItems() methods.
For example:
$cart->updateItem(456, [
'quantity' => 2, // new quantity
]);
$cart->removeItem(456);
$items = $cart->getItems();To apply actions on the cart or item level, you need to use the Cart::addAction($actionData) or Cart::addItemAction($itemId, $actionData) methods. The $actionData is an array of action information, such as name, type, target, value, and attributes. For example:
// create a 10% tax action
$cart->addAction([
'name' => 'TAX 10%',
'type' => 'tax',
'target' => 'total', // this action will be applied to cart's total when getTotal() is called.
'value' => '10%',
'attributes' => []
]);
// create a 5% discount action
$cart->addItemAction(456, [
'name' => 'DISCOUNT 5%',
'type' => 'discount',
'target' => 'item', // this action will be applied to the item when getSubTotal() is called.
'value' => '-5%',
'attributes' => []
]);To get the cart’s total or subtotal, you can use the Cart::getTotal() or Cart::getSubTotal() methods.
For example:
$total = $cart->getTotal();
$subtotal = $cart->getSubTotal();Conclusion
Laravel cart packages are useful tools to create and manage shopping carts in your Laravel applications. They offer various features and options to suit your needs. You can choose the package that best fits your project and follow the documentation to get started. Happy coding! 😊
Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some claps 👏. And if you have any questions feel free to comment. Thank you.
Thanks a lot for reading till the end. Follow or contact me via: Email: [email protected] LinkedIn: https://www.linkedin.com/in/reuben-shumba-a72aaa157/ BuyMeCoffee: https://www.buymeacoffee.com/reubenshumba REUBEN SHUMBA






