Download the PHP package bahaaalhagar/shoppingcart without Composer
On this page you can find all versions of the php package bahaaalhagar/shoppingcart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bahaaalhagar/shoppingcart
More information about bahaaalhagar/shoppingcart
Files in bahaaalhagar/shoppingcart
Package shoppingcart
Short Description Laravel simple Shopping Cart package
License MIT
Homepage https://github.com/BahaaAlhagar/ShoppingCart
Informations about the package shoppingcart
ShoppingCart
a Simple ShoppingCart impelementaion for Laravel.
Install
Install the package through Composer.
If you're using Laravel 5.5, this is all there is to do.
if you're using Laravel 5.4 or less version then you have the register the package service provider and the package alias in your app.
open config/app.php
file.
Add a new line to the providers
array:
And add a new line to the aliases
array:
Usage
The ShoppingCart gives you the following methods to use:
Cart::add()
Adding an item to the cart is really simple, you just use the add()
method, which accepts a variety of parameters.
In its most basic form you can specify the product Model, quantity ($qty) you'd like to add to the cart. note that the Product model must have a price column.
if you didnt specify the $qty the cart will assume its 1 item.
As an optional third parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size or color for example.
The add()
method will return CartItem array of the item you just added to the cart.
Cart::modify()
To modify an item in the cart, you'll first need the uinqueIndex of the item.
the uniqueIndex is the item offset in the cart Model items array.
Next you can use the modify()
method to modify it.
If you simply want to modify the quantity, you'll pass the modify method the uinqueIndex and the new quantity:
Cart::reduceOneItem()
to reduce certain Cart item quantity by 1, you'll first need the uinqueIndex of the item.
then
Cart::remove()
To remove an item for the cart, you'll again need the uniqueIndex. This uniqueIndex you simply pass to the remove()
method and it will remove the item from the cart.
Cart::getContent()
Of course you also want to get the carts content. This is where you'll use the getContent
method. This method will return a Cart model which you can iterate over and show the content to your customers.
Cart::destroy()
If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove the cart from Session.
Cart::total()
The total()
method can be used to get the calculated total of all items in the cart, given there price and quantity.
Cart::count()
If you want to know how many items there are in your cart, you can use the count()
method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items.
Exceptions
The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
Exception | Reason |
---|---|
CartIsEmptyException | When trying to modify or reduce a cart item and the cart already empty |
InvalidUniqueIndexException | When the uniqueIndex that got passed doesn't exists in the cart |
InvalidQuantityException | When you try to add negative or float quantity. |
Events
The cart also has events build in. There are five events available for you to listen for.
Event | Fired | Parameter |
---|---|---|
cartItem.added | When an item was added to the cart. | The CartItem that was added. |
cartItem.modified | When an item in the cart was modified or reduced by 1. | The CartItem that was modified. |
cartItem.removed | When an item is removed from the cart. | The CartItem that was removed. |
cart.destroyed | When the content of a cart was destroyed. | return the removed Cart Model. |
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Bahaa Alhagar
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of shoppingcart with dependencies
illuminate/console Version ~5
illuminate/filesystem Version ~5
php Version ~5.6|~7.0