Download the PHP package scott-lsi/laravelshoppingbasket without Composer
On this page you can find all versions of the php package scott-lsi/laravelshoppingbasket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download scott-lsi/laravelshoppingbasket
More information about scott-lsi/laravelshoppingbasket
Files in scott-lsi/laravelshoppingbasket
Package laravelshoppingbasket
Short Description Laravel Shopping Basket
License MIT
Informations about the package laravelshoppingbasket
Laravel 10 Shopping Basket
Credits
This is a fork of ultrono/laravelshoppingcart-1 which is a fork of darryldecode/laravelshoppingbasket
Installation
composer require scott-lsi/laravelshoppingbasket
Configuration
The service provider and alias are autodiscovered.
You may publish the configuration file using:
php artisan vendor:publish --provider="ScottLsi\Basket\BasketServiceProvider" --tag="config"
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 basket has a default sessionKey that holds the basket data. This
also serves as a basket unique identifier which you can use to bind a basket to a specific user.
To override this default session Key, you will just simply call the \Basket::session($sessionKey)
method
BEFORE ANY OTHER METHODS!!.
Example:
See More Examples below:
Adding Item on Basket: Basket::add()
There are several ways you can add items on your basket, see below:
Updating an item on a basket: Basket::update()
Updating an item on a basket is very simple:
Removing an item on a basket: Basket::remove()
Removing an item on a basket is very easy:
Getting an item on a basket: Basket::get()
Getting basket's contents and count: Basket::getContent()
Check if basket is empty: Basket::isEmpty()
Get basket total quantity: Basket::getTotalQuantity()
Get basket subtotal: Basket::getSubTotal()
Get basket total: Basket::getTotal()
Clearing the Basket: Basket::clear()
Conditions
Laravel Shopping Basket supports basket 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 Basket Value bases
2.) Per-Item Bases
First let's add a condition on a Basket Bases:
There are also several ways of adding a condition on a basket: NOTE:
When adding a condition on a basket 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 BasketCondition.
NOTE: All basket based conditions should be added to basket's conditions before calling Basket::getTotal() and if there are also conditions that are targeted to be applied to subtotal, it should be added to basket's conditions before calling Basket::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 basket 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 basket bases.
Now let's add condition on an item.
NOTE: All basket per-item conditions should be added before calling Basket::getSubTotal()
Then Finally you can call Basket::getSubTotal() to get the Basket sub total with the applied conditions on each of the items.
Add condition to existing Item on the basket: Basket::addItemCondition($productId, $itemCondition)
Adding Condition to an existing Item on the basket 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 Basket Conditions: Basket::clearBasketConditions()
Remove Specific Basket Condition: Basket::removeBasketCondition(\$conditionName)
Remove Specific Item Condition: Basket::removeItemCondition($itemId, $conditionName)
Clear all Item Conditions: Basket::clearItemConditions(\$itemId)
Get conditions by type: Basket::getConditionsByType(\$type)
Remove conditions by type: Basket::removeConditionsByType(\$type)
Items
The method Basket::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. Basket conditions won't be applied to price.
Associating Models
One can associate a basket item to a model. Let's say you have a Product
model in your application. With the associate()
method, you can tell the basket that an item in the basket, 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 basket.
Instances
You may also want multiple basket 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 basket 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 basket item. |
Events
The basket has currently 9 events you can listen and hook some actons.
Event | Fired |
---|---|
basket.created(\$basket) | When a basket is instantiated |
basket.adding($items, $basket) | When an item is attempted to be added |
basket.added($items, $basket) | When an item is added on basket |
basket.updating($items, $basket) | When an item is being updated |
basket.updated($items, $basket) | When an item is updated |
basket.removing($id, $basket) | When an item is being remove |
basket.removed($id, $basket) | When an item is removed |
basket.clearing(\$basket) | When a basket is attempted to be cleared |
basket.cleared(\$basket) | When a basket is cleared |
NOTE: For different basket instance, dealing events is simple. For example you have created another basket instance which you have given an instance name of "wishlist". The Events will be something like: {$instanceName}.created($basket)
So for you wishlist basket instance, events will look like this:
- wishlist.created(\$basket)
- wishlist.adding($items, $basket)
- wishlist.added($items, $basket) 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 baskets items is pretty straight forward. The storage class that is injected to the Basket'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 basket data.
Let's create it by issuing php artisan make:migration create_basket_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 basket 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 basket would be still available for 30 days. Sessions by default persists only 20 minutes.
To make this the basket's default storage, let's update the basket's configuration file.
First, let us publish first the basket config file for us to enable to override it.
php artisan vendor:publish --provider="ScottLsi\Basket\BasketServiceProvider" --tag="config"
after running that command, there should be a new file on your config folder name shopping_basket.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 basket instance (example WishList), you can inject the custom database storage to your basket instance by injecting it to the service provider of your wishlist basket, you replace the storage to use your custom storage. See below:
License
The Laravel Shopping Basket 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 laravelshoppingbasket with dependencies
illuminate/support Version ^10.0
illuminate/validation Version ^10.0
illuminate/translation Version ^10.0