Download the PHP package osaris-uk/cart without Composer
On this page you can find all versions of the php package osaris-uk/cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download osaris-uk/cart
More information about osaris-uk/cart
Files in osaris-uk/cart
Package cart
Short Description Shopping Cart library for Laravel 6 that uses Database instead of Sessions
License MIT
Homepage https://github.com/osaris-uk/cart
Informations about the package cart
Cart
Shopping Cart library for Laravel 6 that uses database instead of sessions to store carts.
Features
- Cart for guest users
- Cart for logged in users
- Guest Cart is merged with User Cart when logged in
- Singleton Cart instance to avoid unnecessary database queries. But also possible to avoid signleton cart if needed.
- Built on top of Eloquent Model, so easily extendable and all eloquent methods can be used.
- Multiple instances of cart
- Schedule expired carts for deletion
Installation
-
Install with composer command:
-
Next, add a new provider to the providers array in config/app.php:
- Then, publish database migrations and run migration:
Configuration
Optionally, you can publish package config file:
Now, update config/cart.php
if required
Usage
Get Cart Instance:
Get the current cart instance. It returns a singleton cart instance:
or,
alternatively, you can avoid singleton instance and use the model class to load the cart instance from database everytime:
The idea of using singleton cart is that the cart object will be available globally throughout your app (e.g. controllers/models/views/view composers etc) for a single request. Also as you manipulate cart items, $cart->item_count
and $cart->total_price
would get updated.
Add an Item: $cart->addItem($attributes)
which is equivalent to $cart->items()->create($attributes)
Get Items: $cart->items
Since $cart
is eloquent model instance, you can use any of the eloquent methods to get items
Update an Item: $cart->updateItem($where, $attributes)
which is equivalent to $cart->items()->where($where)->first()->update($attributes)
Remove an Item: $cart->removeItem($where)
which is equivalent to $cart->items()->where($where)->first()->delete()
Clear Cart Items: $cart->clear()
Remove all items from the cart
Refresh Cart Totals: $cart->refreshCartTotals()
Refresh the cart based on all items in the cart
Checkout cart: $cart->checkout()
This method only updates status
and placed_at
column values. status
is set to pending
Move item(s) between carts:
To move all items from one cart to another cart instance:
To move a single item between carts:
Get Cart Attributes
Cart Statuses
Supports several cart statuses:
active
: currently adding items to the cartexpired
: cart is expired, meaningful for session cartspending
: checked out cartscompleted
: completed carts
Working with Multiple Cart Instances
By default, cart instances are named as default
. You can load other instances by providing a name:
or, without singleton carts:
To get carts other than default
:
Delete Expired Carts:
The guest carts depend on the session lifetime. They are valid as long as the session is not expired. You can increase session lifetime in config/session.php
to increase cart lifetime. When a session expires, a new cart instance will be created in database and the old one will no longer be used. Over time, these expired carts could pile-up in database.
Laravel Task Scheduler comes to the rescue. To enable scheduler just add following to the crontab in your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
That's it. The module will now check for expired carts in every hour and delete them. This won't affect the carts for loggedin users.
Other features:
Get Cart User: $cart->user
Get Item Product: $item->product
Is Cart Empty: $cart->isEmpty()
If an item exists in cart: $cart->hasItem(['id' => 10])
Expire the cart: cart->expire();
Set to completed
status: $cart->complete();
Extending Cart Model
It's easy to extend Cart. You can extend base Cart model and add your own methods or columns. Follow these steps to extend the cart model:
-
Create a model by extending
OsarisUk\Cart\Models\Cart
: -
Update
cart_model
inconfig/cart.php
with the fully qualified class name of the extended model. - That's it, you can now load the cart as usual:
You can also follow the above steps and create your own CartLine
model by extending OsarisUk\Cart\Models\CartLine
. Be sure to update config/cart.php
to reflect your changes.
Credits
This package is a modified version of hassansin/dbcart. The license for this package remains unchanged from the original.