-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathEShoppingCart.php
227 lines (190 loc) · 5.33 KB
/
EShoppingCart.php
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
<?php
/**
* Shopping cart class
*
* @author pirrat <[email protected]>
* @version 0.9
* @package ShoppingCart
*/
class EShoppingCart extends CMap {
/**
* Update the model on session restore?
* @var boolean
*/
public $refresh = true;
public $discounts = array();
public $cartId = __CLASS__;
/**
* Cart-wide discount sum
* @var float
*/
protected $discountPrice = 0.0;
public function init(){
$this->restoreFromSession();
}
/**
* Restores the shopping cart from the session
*/
public function restoreFromSession() {
$data = unserialize(Yii::app()->getUser()->getState($this->cartId));
if (is_array($data) || $data instanceof Traversable)
foreach ($data as $key => $product)
parent::add($key, $product);
}
/**
* Add item to the shopping cart
* If the position was previously added to the cart,
* then information about it is updated, and count increases by $quantity
* @param IECartPosition $position
* @param int count of elements positions
*/
public function put(IECartPosition $position, $quantity = 1) {
$key = $position->getId();
if ($this->itemAt($key) instanceof IECartPosition) {
$position = $this->itemAt($key);
$oldQuantity = $position->getQuantity();
$quantity += $oldQuantity;
}
$this->update($position, $quantity);
}
/**
* Add $value items to position with $key specified
* @return void
* @param mixed $key
* @param mixed $value
*/
public function add($key, $value) {
$this->put($value, 1);
}
/**
* Removes position from the shopping cart of key
* @param mixed $key
*/
public function remove($key) {
parent::remove($key);
$this->applyDiscounts();
$this->onRemovePosition(new CEvent($this));
$this->saveState();
}
/**
* Updates the position in the shopping cart
* If position was previously added, then it will be updated in shopping cart,
* if position was not previously in the cart, it will be added there.
* If count is less than 1, the position will be deleted.
*
* @param IECartPosition $position
* @param int $quantity
*/
public function update(IECartPosition $position, $quantity) {
if (!($position instanceof CComponent))
throw new InvalidArgumentException('invalid argument 1, product must implement CComponent interface');
$key = $position->getId();
$position->detachBehavior("CartPosition");
$position->attachBehavior("CartPosition", new ECartPositionBehaviour());
$position->setRefresh($this->refresh);
$position->setQuantity($quantity);
if ($position->getQuantity() < 1)
$this->remove($key);
else
parent::add($key, $position);
$this->applyDiscounts();
$this->onUpdatePosition(new CEvent($this));
$this->saveState();
}
/**
* Saves the state of the object in the session.
* @return void
*/
protected function saveState() {
Yii::app()->getUser()->setState($this->cartId, serialize($this->toArray()));
}
/**
* Returns count of items in shopping cart
* @return int
*/
public function getItemsCount() {
$count = 0;
foreach ($this as $position)
{
$count += $position->getQuantity();
}
return $count;
}
/**
* Returns total price for all items in the shopping cart.
* @param bool $withDiscount
* @return float
*/
public function getCost($withDiscount = true) {
$price = 0.0;
foreach ($this as $position)
{
$price += $position->getSumPrice($withDiscount);
}
if($withDiscount)
$price -= $this->discountPrice;
return $price;
}
/**
* onRemovePosition event
* @param $event
* @return void
*/
public function onRemovePosition($event) {
$this->raiseEvent('onRemovePosition', $event);
}
/**
* onUpdatePoistion event
* @param $event
* @return void
*/
public function onUpdatePosition($event) {
$this->raiseEvent('onUpdatePosition', $event);
}
/**
* Apply discounts to all positions
* @return void
*/
protected function applyDiscounts() {
foreach ($this->discounts as $discount)
{
$discountObj = Yii::createComponent($discount);
$discountObj->setShoppingCart($this);
$discountObj->apply();
}
}
/**
* Set cart-wide discount sum
*
* @param float $price
* @return void
*/
public function setDiscountPrice($price){
$this->discountPrice = $price;
}
/**
* Add $price to cart-wide discount sum
*
* @param float $price
* @return void
*/
public function addDiscountPrice($price){
$this->discountPrice += $price;
}
/**
* Returns array all positions
* @return array
*/
public function getPositions()
{
return $this->toArray();
}
/**
* Returns if cart is empty
* @return bool
*/
public function isEmpty()
{
return !(bool)$this->getCount();
}
}