Download the PHP package treestoneit/shopping-cart without Composer
On this page you can find all versions of the php package treestoneit/shopping-cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download treestoneit/shopping-cart
More information about treestoneit/shopping-cart
Files in treestoneit/shopping-cart
Package shopping-cart
Short Description An easy-to-use shopping cart for Laravel
License MIT
Homepage https://github.com/treestoneit/shopping-cart
Informations about the package shopping-cart
A Database Driven Shopping Cart for Laravel
This is a simple shopping cart implementation for Laravel 6/7/8. It automatically serializes your cart to the database and loads the related product models.
Usage
To get started, add the Buyable
interface to your model.
Make sure you implement the getBuyableDescription
and getBuyablePrice
methods with the respective product description and product price.
Now you can add products to the cart.
To retrieve the cart contents:
To retrieve the total:
You can update the quantity of an item in the cart. The first argument is the primary id of the related CartItem
.
Or remove the item completely.
Options
To add item-specific options (such as size or color) to an item in the cart, first register available options in your Buyable
instance.
Then you just pass an associative array as the third parameter of Cart::add
.
Any invalid options will be silently removed from the array.
You can also add or change options of an item currently in the cart by calling Cart::updateOption
.
The options array will be available on the CartItem
instance as $item->options
.
Attaching to Users
You can attach a cart instance to a user, so that their cart from a previous session can be retrieved. Attaching a cart to a user is acheived by calling the attachTo
method, passing in an instance of Illuminate\Contracts\Auth\Authenticatable
.
Then when the user logs in, you can call the loadUserCart
method, again passing the user instance.
Dependency Injection
If you're not a facade person, you can use the container to inject the shopping cart instance by type-hinting the Treestoneit\ShoppingCart\CartManager
class, or the Treestoneit\ShoppingCart\CartContract
interface.
Tax
The shopping cart can calculate the total tax of the items in the cart. Just call
You can also set a default tax rate in the included config file.
Then just call Cart::tax
without a parameter.
If some of your items have different tax rates applicable to them, or are tax-free, no problem. First modify the config file:
Then, set the tax rate per item by implementing the Taxable
interface and defining a getTaxRate
method.
Now your items will have their custom tax rate applied to them when calling Cart::tax
.
Installation
You can install the package via composer:
To publish the config file and migrations, run
And run the included database migrations.
Testing
Starter (demo) Repository
If you would like to see a starter/demo implementation using this shopping cart please check out our laravel-commerce repository
Roadmap
Some things I didn't get around to yet:
- Clear cart instance which has not been attached to a user when session is destroyed.
- Add an Artisan command that will clear any unattached carts (these two might be mutually exclusive)
- Add ability to configure cart merging strategy when
loadUserCart
is called
Credits
- Created by Avraham Appel
- Initial development sponsored by Bomshteyn Consulting
- Inspired by LaravelShoppingcart package by @Crisane
License
The MIT License (MIT). Please see the License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
All versions of shopping-cart with dependencies
ext-json Version *
illuminate/config Version ^6.0|^7.0|^8.0|^9.0
illuminate/database Version ^6.20.26|^7.30.5|^8.40.0|^9.0
illuminate/support Version ^6.0|^7.0|^8.0|^9.0