Download the PHP package joelwmale/laravel-cart without Composer
On this page you can find all versions of the php package joelwmale/laravel-cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joelwmale/laravel-cart
More information about joelwmale/laravel-cart
Files in joelwmale/laravel-cart
Package laravel-cart
Short Description A cart implementation for Laravel
License MIT
Informations about the package laravel-cart
A Cart Implementation for Laravel.
Supported Laravel Versions: 10, 11, and 12.
For Laravel 9.0 and below, please use version 1.0
Table of Contents
- Getting Started
- Demo
- Documentation
- Configuration
- Basic Usage
- Conditions
- Cart Items
- Database Support
- Events
- Credits
- License
🚀 Getting Started
🔥 Installing
Install the package through Composer.
composer require joelwmale/laravel-cart
🧑🍳 Demo
📚 Documentation
⚙️ Configuration
You can publish the configuration file to customize various options.
Formatting Numbers
The package by default does not use round to format numbers, and instead returns the number using floatval().
If you'd prefer this number to be rounded, you can customize the formatting in the configuration file.
Defaults to false.
Decimals
You can customize the number of decimals in the configuration file.
Defaults to 2.
Round Mode
The package uses the round
function to round the prices. You can customize the rounding mode in the configuration file.
Defaults to down
.
Basic Usage
The cart has a default sessionKey that holds the cart data and stores it in the session, so you can have multiple carts for multiple users.
This also serves as a cart unique identifier which you can use to bind a cart to a specific user if you want to.
Make sure to call \Cart::setSessionKey($sessionKey)
before calling any other cart methods.
Usually this is not required.
Adding to the cart: Cart::add()
There are a few ways to add items to the cart.
Updating an item on a cart: Cart::update()
Removing an item on a cart: Cart::remove()
Getting an item on a cart: Cart::get()
Getting the cart content: Cart::getContent()
Get cart total quantity: Cart::getTotalQuantity()
Cart subtotal: Cart::getSubTotal()
Cart subtotal without conditions: Cart::getSubTotalWithoutConditions()
Cart Total: Cart::getTotal()
Check if cart is empty: Cart::isEmpty()
Clearing the Cart: Cart::clear()
This clears all items and conditions from the cart.
Clearing the cart items only: Cart::clearItems()
This clears all items, but keeps the conditions (useful for when you want to keep the conditions but remove the items)
Conditions
Conditions are very useful for adding things like discounts, taxes, shipping, etc.
Conditions can be added on the entire cart or on individual items, and can even be applied only at certain cart values.
Conditions on a cart level should always have a target
of subtotal
or total
. This tells the cart which value to apply the condition to.
You can provide an optional minimum
value which should be the dollar value in which the target (subtotal or total) needs to be for the condition to be active and impact the cart.
You can also provide an order
to cart conditions which tells the cart in what order to apply the conditions. Item level conditions do not support the order
parameter.
Conditions on the cart
Adding a condition to the cart: Cart::condition()
Getting conditions on the cart: Cart::getConditions()
Getting conditions on the cart as an array: Cart::getConditions(array: true)
You can get all cart conditions in array format by passing "array: true". This is useful if you want to store the carts conditions on a Livewire component since by default we have collections inside collections for conditions which Livewire does not support.
Getting conditions by name: Cart::getCondition($conditionName)
Getting active conditions on the cart: Cart::getConditions(active: true)
You can get only active conditions by passsing active: true
to the getConditions()
method.
This will return conditions that are actively being applied to the cart (i.e if they meet their minimum or maximum value)
Calculating condition value
There are 2 ways to calculate the value of a condition:
- Using the
getCalculatedValue
method on the condition instance - Using the
getCalculatedValueForCondition
method on the cart instance and passing the condition name
Using the getCalculatedValue
method on the condition instance
Using the getCalculatedValueForCondition
method on the cart instance
This method automatically calculates the value of a condition by it's name based on the order of the conditions.
Adding conditions that activate once a minimum value is met
You can add a minimum
amount required for a condition to activate.
This is useful for applying discounts only after certain cart values, i.e: 10% off for any purchases over $120.
Adding conditions that only activate up to a maximum value
You can add a maximum
amount required for a condition to activate.
This is useful for applying discounts up until amounts, i.e shipping for anything below $200, and free shipping above.
Conditions on items
Item conditions are useful if you have discounts to be applied specifically on an item and not on the whole cart value.
NOTE: All cart per-item conditions should be added before calling Cart::getSubTotal()
Then Finally you can call Cart::getSubTotal() to get the Cart sub total with the applied conditions on each of the items.
Add a condition to an existing item on the cart: Cart::addItemCondition($productId, $itemCondition)
Adding Condition to an existing Item on the cart is simple as well.
Clearing Cart Conditions: Cart::clearCartConditions()
This clears all cart level conditions, and does not affect item level conditions.
If you wish to clear all conditions from all items and the cart, use Cart::clearAllConditions()
Remove a specific cart condtion: Cart::removeCartCondition($conditionName)
Remove a specific item condition: Cart::removeItemCondition($itemId, $conditionName)
Clear all item conditions: Cart::clearItemConditions($itemId)
Get conditions by type: Cart::getConditionsByType($type)
This returns all conditions that has been added to the cart by the type specified.
Remove conditions by type: Cart::removeConditionsByType($type)
Cart Items
The method Cart::getContent() returns a collection of items.
Apart from the above methods, you can also get the price of an item with or without item level conditions applied.
These methods do not apply cart level conditions.
Storage Options
By default the cart is stored in the session, but there are times you may want to store the cart in the database.
For instance, you may want to store the cart in the database so that the cart can be retrieved even after the user logs out or closes the browser, or you may want to add cart timeouts and support for multiple computers.
Session
The cart is stored in the session by default, using Laravel's in-built SessionManager.
Database Support
To get started, you'll need to create a new model for your Cart, the package requires only 3 columns, but you're free to extend this as you wish and add more columns.
You can utilise the events provided by the package to store additional information alongside the cart.
Then add some json casts to your model and fillable columns:
Then update the configuration file to use the database driver:
Events
The package provides a few events that you can listen to in order to manipulate the cart or take actions based on cart events.
LaravelCart.Created
This fires every time a cart is instantiated (i.e every time \Cart::add() is called)
LaravelCart.Adding
This fires every time an item is being added to the cart
LaravelCart.Added
This fires every time an item is successfully added to the cart
LaravelCart.Updating
This fires every time an item is being updated
LaravelCart.Updated
This fires every time an item is successfully updated
LaravelCart.Removing
This fires every time an item is being removed
LaravelCart.Removed
This fires every time an item is successfully removed
LaravelCart.Clearing
This fires every time the cart is being cleared
LaravelCart.Cleared
This fires every time the cart is successfully cleared
🫡 Credits
This package was orignally created by darryldecode but has since seen almost no updates. I have decided to take the old package and transform it into a new package with new features and updates.
📓 License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-cart with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/auth Version ^10.0|^11.0|^12.0
illuminate/container Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/validation Version ^10.0|^11.0|^12.0