Download the PHP package dvoiced/yii2-cart without Composer
On this page you can find all versions of the php package dvoiced/yii2-cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dvoiced/yii2-cart
More information about dvoiced/yii2-cart
Files in dvoiced/yii2-cart
Package yii2-cart
Short Description Yii2 extension that adds shopping cart functions
License MIT
Informations about the package yii2-cart
Shopping cart for Yii 2
This extension is improvisation of omnilight/yii2-shopping-cart. It's add shopping cart systems for Yii framework 2.0. It have feature for save to some medium, they are session (default), cookie, localStorage, database, and multiple storage.
What's is the meaning of the multiple storage? It's feature that can handle two storage where it will save cart data to storage 1 if user is guest, and save to storage 2 if user is logged user.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require
section of your composer.json.
If You plan to save cart data into database, so You should create table cart.
or use migration
How to use
In your model:
In your controller:
Also you can use cart as global application component:
Possible values of storage are
- hscstudio\cart\CookieStorage
- hscstudio\cart\SessionStorage
- hscstudio\cart\LocalStorage
- hscstudio\cart\DatabaseStorage
- hscstudio\cart\MultipleStorage
Example configuration for MultipleStorage.
If You use Multiple Storage, so You should add bootstrap in configuration file:
Or You can create and use Your own storageClass, it's should extends abstract class of hscstudio\cart\Storage. It is look like :
And use it in the following way:
In order to get number of items in the cart:
In order to get total cost of items in the cart:
If user have finished, and do checkout, so wen use following code
Using discounts
Discounts are implemented as behaviors that could attached to the cart or it's items. To use them, follow this steps:
-
Define discount class as a subclass of hscstudio\cart\DiscountBehavior
- Add this behavior to the cart:
If discount is suitable not for the whole cart, but for the individual item, than it is possible to attach discount to the cart position itself:
Note, that the same behavior could be used for both cart and item classes.
-
To get total cost with discount applied:
- During the calculation the following events are triggered:
Cart::EVENT_COST_CALCULATION
once per calculation.ItemInterface::EVENT_COST_CALCULATION
for each item in the cart.
You can also subscribe on this events to perform discount calculation: