Download the PHP package omnilight/yii2-shopping-cart without Composer
On this page you can find all versions of the php package omnilight/yii2-shopping-cart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download omnilight/yii2-shopping-cart
More information about omnilight/yii2-shopping-cart
Files in omnilight/yii2-shopping-cart
Package yii2-shopping-cart
Short Description Yii2 extension that adds shopping cart functions
License
Informations about the package yii2-shopping-cart
Shopping cart for Yii 2
This extension adds shopping cart for Yii framework 2.0
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require
section of your composer.json.
How to use
In your model:
In your controller:
Also you can use cart as global application component:
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 the original model that you want to use as cart position is too heavy to be stored in the session, you can create a separate class implementing CartPositionInterface, and original model can implement CartPositionProviderInterface:
This way gives us ability to create separate cart positions for the same product, that differs only on some property, for example price or color:
Using discounts
Discounts are implemented as behaviors that could attached to the cart or it's positions. To use them, follow this steps:
-
Define discount class as a subclass of yz\shoppingcart\DiscountBehavior
- Add this behavior to the cart:
If discount is suitable not for the whole cart, but for the individual positions, than it is possible to attach discount to the cart position itself:
Note, that the same behavior could be used for both cart and position classes.
-
To get total cost with discount applied:
- During the calculation the following events are triggered:
ShoppingCart::EVENT_COST_CALCULATION
once per calculation.CartPositionInterface::EVENT_COST_CALCULATION
for each position in the cart.
You can also subscribe on this events to perform discount calculation: