|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BahaaAlhagar\ShoppingCart; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Session; |
|
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
7
|
|
|
use BahaaAlhagar\ShoppingCart\Exceptions\CartIsEmptyException; |
|
8
|
|
|
use BahaaAlhagar\ShoppingCart\Exceptions\InvalidQuantityException; |
|
9
|
|
|
use BahaaAlhagar\ShoppingCart\Exceptions\UnknownUniqueIndexException; |
|
10
|
|
|
|
|
11
|
|
|
class ShoppingCart |
|
12
|
|
|
{ |
|
13
|
|
|
public $items = null; |
|
14
|
|
|
public $totalQty; |
|
15
|
|
|
public $totalPrice; |
|
16
|
|
|
|
|
17
|
|
|
protected $events; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new Skeleton Instance |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Dispatcher $events) |
|
23
|
|
|
{ |
|
24
|
|
|
$oldCart = Session::has('cart') ? Session::get('cart') : null; |
|
25
|
|
|
|
|
26
|
|
|
if ($oldCart) { |
|
27
|
|
|
$this->items = $oldCart->items; |
|
28
|
|
|
$this->totalQty = $oldCart->totalQty; |
|
29
|
|
|
$this->totalPrice = $oldCart->totalPrice; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->events = $events; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* get the cart from the Session |
|
37
|
|
|
* |
|
38
|
|
|
* @return get the session cart array and the cart object or null |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getContent() |
|
41
|
|
|
{ |
|
42
|
|
|
// Get the Cart from the Session if there is one |
|
43
|
|
|
$cart = Session::has('cart') ? Session::get('cart') : null; |
|
44
|
|
|
|
|
45
|
|
|
// return the Cart |
|
46
|
|
|
return $cart; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* update the cart in the Session |
|
51
|
|
|
* |
|
52
|
|
|
* @return update the session cart array and the cart object |
|
53
|
|
|
*/ |
|
54
|
|
|
public function update() |
|
55
|
|
|
{ |
|
56
|
|
|
// unset the events add the Cart to the Session |
|
57
|
|
|
$this->events = null; |
|
58
|
|
|
Session::put('cart', $this); |
|
59
|
|
|
|
|
60
|
|
|
// return the Cart if needed |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* empty the cart in the Session |
|
66
|
|
|
* |
|
67
|
|
|
* @return empty the session cart array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function destroy() |
|
70
|
|
|
{ |
|
71
|
|
|
// Cart destroyed event |
|
72
|
|
|
$this->events->fire('cart.destroyed', $this); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
// remove the Cart from the Session |
|
75
|
|
|
Session::forget('cart'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add 1 item to the Cart |
|
80
|
|
|
* |
|
81
|
|
|
* @param object $item item to add |
|
82
|
|
|
* |
|
83
|
|
|
* @return update the session cart array and the cart object |
|
84
|
|
|
*/ |
|
85
|
|
|
public function add($item, $qty = null, array $options = null) |
|
86
|
|
|
{ |
|
87
|
|
|
// if it is a mutated item by using options then give it another offset |
|
88
|
|
|
// else use the item model id |
|
89
|
|
|
$uniqueIndex = $options ? $this->createUniqueIndex($item->id, $options) : $item->id; |
|
90
|
|
|
|
|
91
|
|
|
$this->validateQty($qty); |
|
92
|
|
|
|
|
93
|
|
|
// if there is no certain qty provided by the method then set it to 1 |
|
94
|
|
|
$Qty = $qty ? $qty : 1; |
|
95
|
|
|
|
|
96
|
|
|
// Cart item structure |
|
97
|
|
|
$storedItem = ['qty' => 0, 'price' => $item->price, 'options' => $options, 'item' => $item->toArray()]; |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
// check if the item exists in the cart before |
|
101
|
|
|
// and if it exists then get it from the cart |
|
102
|
|
|
if ($this->items) { |
|
103
|
|
|
if (array_key_exists($uniqueIndex, $this->items)) { |
|
104
|
|
|
$storedItem = $this->items[$uniqueIndex]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// update the cart increase qty and price |
|
109
|
|
|
$storedItem['qty'] += $Qty; |
|
110
|
|
|
$storedItem['price'] = $item->price * $storedItem['qty']; |
|
111
|
|
|
$this->items[$uniqueIndex] = $storedItem; |
|
112
|
|
|
$this->totalQty += $Qty; |
|
113
|
|
|
$this->totalPrice += ($Qty * $item->price); |
|
114
|
|
|
|
|
115
|
|
|
// cartItem added event |
|
116
|
|
|
$this->events->fire('cartItem.added', $this->items[$uniqueIndex]); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
// add the cart to the Session |
|
119
|
|
|
$this->update(); |
|
120
|
|
|
|
|
121
|
|
|
// return the cart item that has been added |
|
122
|
|
|
return $this->items[$uniqueIndex]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Reduce 1 item from the Cart |
|
127
|
|
|
* |
|
128
|
|
|
* @param object $item item to reduce by 1 |
|
|
|
|
|
|
129
|
|
|
* |
|
130
|
|
|
* @return update the session cart array and the cart object |
|
131
|
|
|
*/ |
|
132
|
|
|
public function reduceOneItem($uniqueIndex) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->validateIndex($uniqueIndex); |
|
135
|
|
|
|
|
136
|
|
|
// decrease the qty and the price in the cart |
|
137
|
|
|
$this->items[$uniqueIndex]['qty']--; |
|
138
|
|
|
$this->items[$uniqueIndex]['price'] -= $this->items[$uniqueIndex]['item']['price']; |
|
139
|
|
|
$this->totalQty--; |
|
140
|
|
|
$this->totalPrice -= $this->items[$uniqueIndex]['item']['price']; |
|
141
|
|
|
|
|
142
|
|
|
// cartItem modified event |
|
143
|
|
|
$this->events->fire('cartItem.modified', $this->items[$uniqueIndex]); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
// if the item qty is 0 or less remove the item from Cart |
|
146
|
|
|
$this->itemQtyStatusCheck($uniqueIndex); |
|
147
|
|
|
|
|
148
|
|
|
// update the Cart in the Session |
|
149
|
|
|
$this->update(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Remove item from the Cart |
|
154
|
|
|
* |
|
155
|
|
|
* @param object $item item to remove |
|
|
|
|
|
|
156
|
|
|
* |
|
157
|
|
|
* @return update the session cart array and the cart object |
|
158
|
|
|
*/ |
|
159
|
|
|
public function remove($uniqueIndex) |
|
160
|
|
|
{ |
|
161
|
|
|
// remove item qty and price from cart |
|
162
|
|
|
$this->totalQty -= $this->items[$uniqueIndex]['qty']; |
|
163
|
|
|
$this->totalPrice -= $this->items[$uniqueIndex]['price']; |
|
164
|
|
|
|
|
165
|
|
|
// cartItem removed event |
|
166
|
|
|
$this->events->fire('cartItem.removed', $this->items[$uniqueIndex]); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
// remove the cart item |
|
169
|
|
|
unset($this->items[$uniqueIndex]); |
|
170
|
|
|
|
|
171
|
|
|
// update the Cart in the Session |
|
172
|
|
|
$this->update(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* get the cart items count |
|
177
|
|
|
* |
|
178
|
|
|
* @return the cart total quantity |
|
179
|
|
|
*/ |
|
180
|
|
|
public function count() |
|
181
|
|
|
{ |
|
182
|
|
|
// we need id for the array index |
|
183
|
|
|
return $this->totalQty ? $this->totalQty : null; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* get the cart items total price |
|
188
|
|
|
* |
|
189
|
|
|
* @return the cart total price |
|
190
|
|
|
*/ |
|
191
|
|
|
public function total() |
|
192
|
|
|
{ |
|
193
|
|
|
// we need id for the array index |
|
194
|
|
|
return $this->totalPrice ? $this->totalPrice : null; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* create unique index |
|
199
|
|
|
* |
|
200
|
|
|
* @param item |
|
201
|
|
|
* |
|
202
|
|
|
* @param options array |
|
203
|
|
|
* |
|
204
|
|
|
* @return uniqueIndex for the shopping cart |
|
205
|
|
|
*/ |
|
206
|
|
|
public function createUniqueIndex($id, $options) |
|
207
|
|
|
{ |
|
208
|
|
|
$uniqueIndex = md5($id.serialize($options)); |
|
209
|
|
|
return $uniqueIndex; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* update item in the Cart |
|
214
|
|
|
* |
|
215
|
|
|
* @param unique item index to modify |
|
216
|
|
|
* |
|
217
|
|
|
* @param amount to edit |
|
218
|
|
|
* |
|
219
|
|
|
* @return update the session cart array and the cart object |
|
220
|
|
|
*/ |
|
221
|
|
|
public function modify($uniqueIndex, $qty) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->validateQty($qty); |
|
224
|
|
|
|
|
225
|
|
|
$this->validateIndex($uniqueIndex); |
|
226
|
|
|
|
|
227
|
|
|
// update the cart item |
|
228
|
|
|
$this->updateItem($uniqueIndex, $qty); |
|
229
|
|
|
|
|
230
|
|
|
// cartItem modified event |
|
231
|
|
|
$this->events->fire('cartItem.modified', $this->items[$uniqueIndex]); |
|
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
// if the item qty is 0 or less remove the item from Cart |
|
234
|
|
|
$this->itemQtyStatusCheck($uniqueIndex); |
|
235
|
|
|
|
|
236
|
|
|
// add the cart to the Session |
|
237
|
|
|
$this->update(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* get item price |
|
242
|
|
|
* |
|
243
|
|
|
* @param unique index |
|
244
|
|
|
* |
|
245
|
|
|
* @return the item price |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getItemPrice($uniqueIndex) |
|
248
|
|
|
{ |
|
249
|
|
|
if (!$this->items) { |
|
|
|
|
|
|
250
|
|
|
throw new CartIsEmptyException('cart is empty!'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$item = $this->items[$uniqueIndex]; |
|
254
|
|
|
|
|
255
|
|
|
return $itemPrice = $item['price']/$item['qty']; |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* update the item depends on the qty |
|
260
|
|
|
* |
|
261
|
|
|
* @param unique index |
|
262
|
|
|
* |
|
263
|
|
|
* @param integer $qty |
|
264
|
|
|
* |
|
265
|
|
|
* @return void |
|
266
|
|
|
*/ |
|
267
|
|
|
public function updateItem($uniqueIndex, $qty) |
|
268
|
|
|
{ |
|
269
|
|
|
// get the current item from the cart |
|
270
|
|
|
$currentItem = $this->items[$uniqueIndex]; |
|
271
|
|
|
|
|
272
|
|
|
// if its the same qty do nothing |
|
273
|
|
|
if ($currentItem['qty'] == $qty) { |
|
274
|
|
|
return false; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$currentItemPrice = $this->getItemPrice($uniqueIndex); |
|
278
|
|
|
|
|
279
|
|
|
$this->totalPrice -= $currentItem['price']; |
|
280
|
|
|
$this->totalPrice += ($qty * $currentItemPrice); |
|
281
|
|
|
|
|
282
|
|
|
$this->totalQty -= $currentItem['qty']; |
|
283
|
|
|
$this->totalQty += $qty; |
|
284
|
|
|
|
|
285
|
|
|
$this->items[$uniqueIndex]['qty'] = $qty; |
|
286
|
|
|
|
|
287
|
|
|
$this->items[$uniqueIndex]['price'] = $qty * $currentItemPrice; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* validate quantity integer and not negative |
|
292
|
|
|
* |
|
293
|
|
|
* @param $qty |
|
294
|
|
|
* |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
|
|
public function validateQty($qty) |
|
298
|
|
|
{ |
|
299
|
|
|
if (is_float($qty + 0) || $qty < 0) { |
|
300
|
|
|
throw new InvalidQuantityException('Invalid quantity!'); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* check if the cart contain certain offset or not |
|
306
|
|
|
* |
|
307
|
|
|
* @param $uniqueIndex |
|
308
|
|
|
* |
|
309
|
|
|
* @return void |
|
310
|
|
|
*/ |
|
311
|
|
|
public function validateIndex($uniqueIndex) |
|
312
|
|
|
{ |
|
313
|
|
|
// check if the item exists in the cart before |
|
314
|
|
|
if (!$this->items) { |
|
|
|
|
|
|
315
|
|
|
throw new CartIsEmptyException('Cart is empty!'); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
if (!array_key_exists($uniqueIndex, $this->items)) { |
|
319
|
|
|
throw new UnknownUniqueIndexException("The cart does not contain this index {$uniqueIndex}."); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* check if item qty is <= 0 and remove item if true |
|
325
|
|
|
* |
|
326
|
|
|
* @param $uniqueIndex |
|
327
|
|
|
* |
|
328
|
|
|
* @return void |
|
329
|
|
|
*/ |
|
330
|
|
|
public function itemQtyStatusCheck($uniqueIndex) |
|
331
|
|
|
{ |
|
332
|
|
|
// if the qty is 0 or less remove the item from Cart |
|
333
|
|
|
if ($this->items[$uniqueIndex]['qty'] <= 0) { |
|
334
|
|
|
unset($this->items[$uniqueIndex]); |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.