-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathreadme_en.txt
303 lines (242 loc) · 7.03 KB
/
readme_en.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Shopping Cart
=============
Provides shpping cart functionality for models.
Cart is a container object that holds items collection and have handy methods to work with it.
It uses user session as a cart data storage.
Installing and configuring
--------------------------
### 1 way: Registration in the config file
Add to `protected/config/main.php`:
```
<?php
'import'=>array(
'ext.yiiext.components.shoppingCart.*'
),
'components' => array(
'shoppingCart' =>
array(
'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart',
),
)
```
### 2 way: Registration by necessity
~~~
[php]
$cart = Yii::createComponent(array(
'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
));
//Important!
$cart->init();
$book = Book::model()->findByPk(1);
$cart->put($book);
~~~
Preparing a model
-----------------
Models that you are planning to put into the cart should implement `IECartPosition` interface:
~~~
[php]
class Book extends CActiveRecord implements IECartPosition {
public static function model($className = __CLASS__) {
return parent::model($className);
}
function getId(){
return 'Book'.$this->id;
}
function getPrice(){
return $this->price;
}
}
~~~
API
---
### EShoppingCart::put($position, $quantity)
Adds $quantity items to the cart.
If item is already in the cart, item data is being updated and item quantity is summed with $quantity.
~~~
[php]
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book); //1 item with id=1, quantity=1.
Yii::app()->shoppingCart->put($book,2); //1 item with id=1, quantity=3.
$book2 = Book::model()->findByPk(2);
Yii::app()->shoppingCart->put($book2); //2 items with id=1 and id=2.
~~~
### EShoppingCart::update($position, $quantity)
Updates cart item.
If item is already in the cart, item data is being updated and item quantity is set to $quantity.
If there is no such item yet it will be added.
If $quantity<1 then item will be deleted.
~~~
[php]
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book); //1 item with id=1, quantity=1.
Yii::app()->shoppingCart->update($book,2); //1 item with id=1, quantity=2.
~~~
### EShoppingCart::remove($key)
Removes item from the cart.
~~~
[php]
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book,2); //1 item with id=1, quantity=2.
Yii::app()->shoppingCart->remove($book->getId()); //no items
~~~
### EShoppingCart::clear()
Clears all cart items.
~~~
[php]
Yii::app()->shoppingCart->clear();
~~~
### EShoppingCart::itemAt($key)
Returns item at key $key.
~~~
[php]
$position = Yii::app()->shoppingCart->itemAt(1);
~~~
### EShoppingCart::contains($key)
Tells if cart contains item with id=$key.
~~~
[php]
$position = Yii::app()->shoppingCart->itemAt();
~~~
### EShoppingCart::isEmpty()
Tells if cart is empty.
~~~
[php]
$position = Yii::app()->shoppingCart->isEmpty(1);
~~~
### EShoppingCart::getCount()
Returns positions count.
~~~
[php]
Yii::app()->shoppingCart->put($book,2);
Yii::app()->shoppingCart->put($book2,3);
Yii::app()->shoppingCart->getCount(); //2
~~~
### EShoppingCart::getItemsCount()
Returns items count.
~~~
[php]
Yii::app()->shoppingCart->put($book,2);
Yii::app()->shoppingCart->put($book2,3);
Yii::app()->shoppingCart->getItemsCount(); //5
~~~
### EShoppingCart::getCost($withDiscount)
Returns cart total.
~~~
[php]
Yii::app()->shoppingCart->put($book,2); //price=100
Yii::app()->shoppingCart->put($book2,1); //price=200
Yii::app()->shoppingCart->getCost(); //400
~~~
### EShoppingCart::getPositions()
Returns an array with all positions.
~~~
[php]
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
...
}
~~~
### IECartPosition::getPrice()
Returns a price for a single item for this position.
~~~
[php]
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
$price = $position->getPrice();
}
~~~
### IECartPosition::getSumPrice($withDiscount)
Returns position price = single item price*items count
~~~
[php]
$book = Book::model()->findByPk(1); //price = 100
Yii::app()->shoppingCart->put($book,2); //putting 2 items
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
$price = $position->getSumPrice(); //200 (2*100)
}
~~~
### IECartPosition::getQuantity()
Returns position items quantity
~~~
[php]
$book = Book::model()->findByPk(1); //price = 100
Yii::app()->shoppingCart->put($book,2); //putting 2 items
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
$price = $position->getQuantity(); //2
}
~~~
Discounts
---------
There is a single discount system built in. It allows to apply a set of rules that
will change cart total or a single position price.
Discount is a class that implements IEDiscount and defines apply() method that describes
how exactly discount is applied.
Discount is calculated by applying Position::addDiscountPrice/Position::setDiscountPrice position's
method or EShoppingCart::addDiscountPrice/EShoppingCart::setDiscountPrice cart-wide method.
This methods are getting one parameter that holds a value of cart total reduction or individual position reduction.
Discount class example:
~~~
[php]
class TestDiscount extends IEDiscount {
/**
* % discount
*/
public $rate = 30;
public function apply() {
foreach ($this->shoppingCart as $position) {
$quantity = $position->getQuantity();
if ($quantity > 1) {
$discountPrice = $this->rate * $position->getPrice() / 100;
$position->addDiscountPrice($discountPrice);
}
}
}
}
~~~
This example dicount works like this:
if there is more than one item for a single position, there will be $rate % discount for
one item and it will be applied to position price.
You can apply unlimited discount rules that will be called one by one:
~~~
[php]
'shoppingCart' =>
array(
'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart',
'discounts' =>
array(
array(
'class' => 'ext.yiiext.components.shoppingCart.discounts.TestDiscount',
'rate' => 40),
array('class' => 'otherDiscount'),
),
),
~~~
Events
------
There are 2 events in ShoppingCart implemented in a standard Yii way:
1) onUpdatePosition - is triggered when position is added or updated.
2) onRemovePosition - is triggered when position is deleted.
Usage:
~~~
[php]
$cN = new CallCenterNotifier();
Yii::app()->shoppingCart->attachEventHandler('onUpdatePosition',array($cN, 'updatePositionInShoppingCart'));
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book);
~~~
When onUpdatePosition event is fired, call center will be notified.
Working with a cart as CMap
---------------------------
ShoppingCart is a child of CMap so you can work with the cart as an array.
~~~
[php]
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart[] = $book; //adding new position.
//iterating over all positions
foreach(Yii::app()->shoppingCart as $position)
{
...
}
~~~