Download the PHP package asyou99/yii2-cart without Composer
On this page you can find all versions of the php package asyou99/yii2-cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download asyou99/yii2-cart
More information about asyou99/yii2-cart
Files in asyou99/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
- asyou99\cart\CookieStorage
- asyou99\cart\SessionStorage
- asyou99\cart\LocalStorage
- asyou99\cart\DatabaseStorage
- asyou99\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 asyou99\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 asyou99\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: