Download the PHP package alexedwoods/laravelshoppingcart without Composer
On this page you can find all versions of the php package alexedwoods/laravelshoppingcart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexedwoods/laravelshoppingcart
More information about alexedwoods/laravelshoppingcart
Files in alexedwoods/laravelshoppingcart
Package laravelshoppingcart
Short Description Laravel 11 Shopping cart
License MIT
Informations about the package laravelshoppingcart
Laravel 11 Shopping Cart
A Shopping Cart Implementation for Laravel Framework
QUICK PARTIAL DEMO
Demo: https://shoppingcart-demo.darrylfernandez.com/cart
Git repo of the demo: https://github.com/darryldecode/laravelshoppingcart-demo
INSTALLATION
Install the package through Composer.
For Laravel 11.0:
CONFIGURATION
-
Open config/app.php and add this line to your Service Providers Array.
-
Open config/app.php and add this line to your Aliases
- Optional configuration file (useful if you plan to have full control)
HOW TO USE
- Quick Usage
- Usage
- Conditions
- Items
- Associating Models
- Instances
- Exceptions
- Events
- Format Response
- Examples
- Using Different Storage
- License
Quick Usage Example
Usage
IMPORTANT NOTE!
By default, the cart has a default sessionKey that holds the cart data. This
also serves as a cart unique identifier which you can use to bind a cart to a specific user.
To override this default session Key, you will just simply call the \Cart::session($sessionKey)
method
BEFORE ANY OTHER METHODS!!.
Example:
See More Examples below:
Adding Item on Cart: Cart::add()
There are several ways you can add items on your cart, see below:
Updating an item on a cart: Cart::update()
Updating an item on a cart is very simple:
Removing an item on a cart: Cart::remove()
Removing an item on a cart is very easy:
Getting an item on a cart: Cart::get()
Getting cart's contents and count: Cart::getContent()
Check if cart is empty: Cart::isEmpty()
Get cart total quantity: Cart::getTotalQuantity()
Get cart subtotal: Cart::getSubTotal()
Get cart subtotal with out conditions: Cart::getSubTotalWithoutConditions()
Get cart total: Cart::getTotal()
Clearing the Cart: Cart::clear()
Conditions
Laravel Shopping Cart supports cart conditions. Conditions are very useful in terms of (coupons,discounts,sale,per-item sale and discounts etc.) See below carefully on how to use conditions.
Conditions can be added on:
1.) Whole Cart Value bases
2.) Per-Item Bases
First let's add a condition on a Cart Bases:
There are also several ways of adding a condition on a cart: NOTE:
When adding a condition on a cart bases, the 'target' should have value of 'subtotal' or 'total'. If the target is "subtotal" then this condition will be applied to subtotal. If the target is "total" then this condition will be applied to total. The order of operation also during calculation will vary on the order you have added the conditions.
Also, when adding conditions, the 'value' field will be the bases of calculation. You can change this order by adding 'order' parameter in CartCondition.
NOTE: All cart based conditions should be added to cart's conditions before calling Cart::getTotal() and if there are also conditions that are targeted to be applied to subtotal, it should be added to cart's conditions before calling Cart::getSubTotal()
Next is the Condition on Per-Item Bases.
This is very useful if you have coupons to be applied specifically on an item and not on the whole cart value.
NOTE: When adding a condition on a per-item bases, the 'target' parameter is not needed or can be omitted. unlike when adding conditions or per cart bases.
Now let's add condition on an item.
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 condition to existing Item on the cart: Cart::addItemCondition($productId, $itemCondition)
Adding Condition to an existing Item on the cart is simple as well.
This is very useful when adding new conditions on an item during checkout process like coupons and promo codes. Let's see the example how to do it:
Clearing Cart Conditions: Cart::clearCartConditions()
Remove Specific Cart Condition: Cart::removeCartCondition(\$conditionName)
Remove Specific Item Condition: Cart::removeItemCondition($itemId, $conditionName)
Clear all Item Conditions: Cart::clearItemConditions(\$itemId)
Get conditions by type: Cart::getConditionsByType(\$type)
Remove conditions by type: Cart::removeConditionsByType(\$type)
Items
The method Cart::getContent() returns a collection of items.
To get the id of an item, use the property \$item->id.
To get the name of an item, use the property \$item->name.
To get the quantity of an item, use the property \$item->quantity.
To get the attributes of an item, use the property \$item->attributes.
To get the price of a single item without the conditions applied, use the property \$item->price.
To get the subtotal of an item without the conditions applied, use the method \$item->getPriceSum().
To get the price of a single item without the conditions applied, use the method
\$item->getPriceWithConditions().
To get the subtotal of an item with the conditions applied, use the method
\$item->getPriceSumWithConditions()
NOTE: When you get price with conditions applied, only the conditions assigned to the current item will be calculated. Cart conditions won't be applied to price.
Associating Models
One can associate a cart item to a model. Let's say you have a Product
model in your application. With the associate()
method, you can tell the cart that an item in the cart, is associated to the Product
model.
That way you can access your model using the property \$item->model.
Here is an example:
NOTE: This only works when adding an item to cart.
Instances
You may also want multiple cart instances on the same page without conflicts. To do that,
Create a new Service Provider and then on register() method, you can put this like so:
IF you are having problem with multiple cart instance, please see the codes on this demo repo here: DEMO
Exceptions
There are currently only two exceptions.
Exception | Description |
---|---|
InvalidConditionException | When there is an invalid field value during instantiating a new Condition |
InvalidItemException | When a new product has invalid field values (id,name,price,quantity) |
UnknownModelException | When you try to associate a none existing model to a cart item. |
Events
The cart has currently 9 events you can listen and hook some actons.
Event | Fired |
---|---|
cart.created(\$cart) | When a cart is instantiated |
cart.adding($items, $cart) | When an item is attempted to be added |
cart.added($items, $cart) | When an item is added on cart |
cart.updating($items, $cart) | When an item is being updated |
cart.updated($items, $cart) | When an item is updated |
cart.removing($id, $cart) | When an item is being remove |
cart.removed($id, $cart) | When an item is removed |
cart.clearing(\$cart) | When a cart is attempted to be cleared |
cart.cleared(\$cart) | When a cart is cleared |
NOTE: For different cart instance, dealing events is simple. For example you have created another cart instance which you have given an instance name of "wishlist". The Events will be something like: {$instanceName}.created($cart)
So for you wishlist cart instance, events will look like this:
- wishlist.created(\$cart)
- wishlist.adding($items, $cart)
- wishlist.added($items, $cart) and so on..
Format Response
Now you can format all the responses. You can publish the config file from the package or use env vars to set the configuration. The options you have are:
- format_numbers or env('SHOPPING_FORMAT_VALUES', false) => Activate or deactivate this feature. Default to false,
- decimals or env('SHOPPING_DECIMALS', 0) => Number of decimals you want to show. Defaults to 0.
- dec_point or env('SHOPPING_DEC_POINT', '.') => Decimal point type. Defaults to a '.'.
- thousands_sep or env('SHOPPING_THOUSANDS_SEP', ',') => Thousands separator for value. Defaults to ','.
Examples
Storage
Using different storage for the carts items is pretty straight forward. The storage class that is injected to the Cart's instance will only need methods.
Example we will need a wishlist, and we want to store its key value pair in database instead of the default session.
To do this, we will need first a database table that will hold our cart data.
Let's create it by issuing php artisan make:migration create_cart_storage_table
Example Code:
Next, lets create an eloquent Model on this table so we can easily deal with the data. It is up to you where you want to store this model. For this example, lets just assume to store it in our App namespace.
Code:
Next, Create a new class for your storage to be injected to our cart instance:
Eg.
For example you can also leverage Laravel's Caching (redis, memcached, file, dynamo, etc) using the example below. Example also includes cookie persistance, so that cart would be still available for 30 days. Sessions by default persists only 20 minutes.
To make this the cart's default storage, let's update the cart's configuration file.
First, let us publish first the cart config file for us to enable to override it.
php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --tag="config"
after running that command, there should be a new file on your config folder name shopping_cart.php
Open this file and let's update the storage use. Find the key which says 'storage' => null,
And update it to your newly created DBStorage Class, which on our example,
'storage' => \App\DBStorage::class,
OR If you have multiple cart instance (example WishList), you can inject the custom database storage to your cart instance by injecting it to the service provider of your wishlist cart, you replace the storage to use your custom storage. See below:
Still feeling confuse on how to do custom database storage? Or maybe doing multiple cart instances? See the demo repo to see the codes and how you can possibly do it and expand base on your needs or make it as a guide & reference. See links below:
OR
License
The Laravel Shopping Cart is open-sourced software licensed under the MIT license
Disclaimer
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, OR ANY OF THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
All versions of laravelshoppingcart with dependencies
illuminate/support Version 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/validation Version 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/translation Version 5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0