forked from omnilight/yii2-shopping-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingCart.php
297 lines (271 loc) · 8.47 KB
/
ShoppingCart.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
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
<?php
namespace yz\shoppingcart;
use Yii;
use yii\base\Component;
use yii\base\Event;
use yii\di\Instance;
use yii\web\Session;
/**
* Class ShoppingCart
* @property CartPositionInterface[] $positions
* @property int $count Total count of positions in the cart
* @property int $cost Total cost of positions in the cart
* @property bool $isEmpty Returns true if cart is empty
* @property string $hash Returns hash (md5) of the current cart, that is uniq to the current combination
* of positions, quantities and costs
* @property string $serialized Get/set serialized content of the cart
* @package \yz\shoppingcart
*/
class ShoppingCart extends Component
{
/** Triggered on position put */
const EVENT_POSITION_PUT = 'putPosition';
/** Triggered on position update */
const EVENT_POSITION_UPDATE = 'updatePosition';
/** Triggered on after position remove */
const EVENT_BEFORE_POSITION_REMOVE = 'removePosition';
/** Triggered on any cart change: add, update, delete position */
const EVENT_CART_CHANGE = 'cartChange';
/** Triggered on after cart cost calculation */
const EVENT_COST_CALCULATION = 'costCalculation';
/**
* If true (default) cart will be automatically stored in and loaded from session.
* If false - you should do this manually with saveToSession and loadFromSession methods
* @var bool
*/
public $storeInSession = true;
/**
* Session component
* @var string|Session
*/
public $session = 'session';
/**
* Shopping cart ID to support multiple carts
* @var string
*/
public $cartId = __CLASS__;
/**
* @var CartPositionInterface[]
*/
protected $_positions = [];
public function init()
{
if ($this->storeInSession)
$this->loadFromSession();
}
/**
* Loads cart from session
*/
public function loadFromSession()
{
$this->session = Instance::ensure($this->session, Session::className());
if (isset($this->session[$this->cartId]))
$this->setSerialized($this->session[$this->cartId]);
}
/**
* Saves cart to the session
*/
public function saveToSession()
{
$this->session = Instance::ensure($this->session, Session::className());
$this->session[$this->cartId] = $this->getSerialized();
}
/**
* Sets cart from serialized string
* @param string $serialized
*/
public function setSerialized($serialized)
{
$this->_positions = unserialize($serialized);
}
/**
* @param CartPositionInterface $position
* @param int $quantity
*/
public function put($position, $quantity = 1)
{
if (isset($this->_positions[$position->getId()])) {
$this->_positions[$position->getId()]->setQuantity(
$this->_positions[$position->getId()]->getQuantity() + $quantity);
} else {
$position->setQuantity($quantity);
$this->_positions[$position->getId()] = $position;
}
$this->trigger(self::EVENT_POSITION_PUT, new CartActionEvent([
'action' => CartActionEvent::ACTION_POSITION_PUT,
'position' => $this->_positions[$position->getId()],
]));
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_POSITION_PUT,
'position' => $this->_positions[$position->getId()],
]));
if ($this->storeInSession)
$this->saveToSession();
}
/**
* Returns cart positions as serialized items
* @return string
*/
public function getSerialized()
{
return serialize($this->_positions);
}
/**
* @param CartPositionInterface $position
* @param int $quantity
*/
public function update($position, $quantity)
{
if ($quantity <= 0) {
$this->remove($position);
return;
}
if (isset($this->_positions[$position->getId()])) {
$this->_positions[$position->getId()]->setQuantity($quantity);
} else {
$position->setQuantity($quantity);
$this->_positions[$position->getId()] = $position;
}
$this->trigger(self::EVENT_POSITION_UPDATE, new CartActionEvent([
'action' => CartActionEvent::ACTION_UPDATE,
'position' => $this->_positions[$position->getId()],
]));
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_UPDATE,
'position' => $this->_positions[$position->getId()],
]));
if ($this->storeInSession)
$this->saveToSession();
}
/**
* Removes position from the cart
* @param CartPositionInterface $position
*/
public function remove($position)
{
$this->removeById($position->getId());
}
/**
* Removes position from the cart by ID
* @param string $id
*/
public function removeById($id)
{
$this->trigger(self::EVENT_BEFORE_POSITION_REMOVE, new CartActionEvent([
'action' => CartActionEvent::ACTION_BEFORE_REMOVE,
'position' => $this->_positions[$id],
]));
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_BEFORE_REMOVE,
'position' => $this->_positions[$id],
]));
unset($this->_positions[$id]);
if ($this->storeInSession)
$this->saveToSession();
}
/**
* Remove all positions
*/
public function removeAll()
{
$this->_positions = [];
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_REMOVE_ALL,
]));
if ($this->storeInSession)
$this->saveToSession();
}
/**
* Returns position by it's id. Null is returned if position was not found
* @param string $id
* @return CartPositionInterface|null
*/
public function getPositionById($id)
{
if ($this->hasPosition($id))
return $this->_positions[$id];
else
return null;
}
/**
* Checks whether cart position exists or not
* @param string $id
* @return bool
*/
public function hasPosition($id)
{
return isset($this->_positions[$id]);
}
/**
* @return CartPositionInterface[]
*/
public function getPositions()
{
return $this->_positions;
}
/**
* @param CartPositionInterface[] $positions
*/
public function setPositions($positions)
{
$this->_positions = array_filter($positions, function (CartPositionInterface $position) {
return $position->quantity > 0;
});
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_SET_POSITIONS,
]));
if ($this->storeInSession)
$this->saveToSession();
}
/**
* Returns true if cart is empty
* @return bool
*/
public function getIsEmpty()
{
return count($this->_positions) == 0;
}
/**
* @return int
*/
public function getCount()
{
$count = 0;
foreach ($this->_positions as $position)
$count += $position->getQuantity();
return $count;
}
/**
* Return full cart cost as a sum of the individual positions costs
* @param $withDiscount
* @return int
*/
public function getCost($withDiscount = false)
{
$cost = 0;
foreach ($this->_positions as $position) {
$cost += $position->getCost($withDiscount);
}
$costEvent = new CostCalculationEvent([
'baseCost' => $cost,
]);
$this->trigger(self::EVENT_COST_CALCULATION, $costEvent);
if ($withDiscount)
$cost = max(0, $cost - $costEvent->discountValue);
return $cost;
}
/**
* Returns hash (md5) of the current cart, that is unique to the current combination
* of positions, quantities and costs. This helps us fast compare if two carts are the same, or not, also
* we can detect if cart is changed (comparing hash to the one's saved somewhere)
* @return string
*/
public function getHash()
{
$data = [];
foreach ($this->positions as $position) {
$data[] = [$position->getId(), $position->getQuantity(), $position->getPrice()];
}
return md5(serialize($data));
}
}