1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Encapsulated manipulation of the current order using a singleton pattern. |
5
|
|
|
* |
6
|
|
|
* Ensures that an order is only started (persisted to DB) when necessary, |
7
|
|
|
* and all future changes are on the same order, until the order has is placed. |
8
|
|
|
* The requirement for starting an order is to adding an item to the cart. |
9
|
|
|
* |
10
|
|
|
* @package shop |
11
|
|
|
*/ |
12
|
|
|
class ShoppingCart |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
private static $cartid_session_name = 'shoppingcartid'; |
15
|
|
|
|
16
|
|
|
private $order; |
17
|
|
|
|
18
|
|
|
private $calculateonce = false; |
19
|
|
|
|
20
|
|
|
private $message; |
21
|
|
|
|
22
|
|
|
private $type; |
23
|
|
|
|
24
|
|
|
private static $instance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Access for only allowing access to one (singleton) ShoppingCart. |
28
|
|
|
* |
29
|
|
|
* @return ShoppingCart |
30
|
|
|
*/ |
31
|
90 |
|
public static function singleton() |
32
|
|
|
{ |
33
|
90 |
|
if (self::$instance === null) { |
34
|
2 |
|
self::$instance = new ShoppingCart(); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
90 |
|
return self::$instance; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Shortened alias for ShoppingCart::singleton()->current() |
42
|
|
|
* |
43
|
|
|
* @return Order |
44
|
|
|
*/ |
45
|
16 |
|
public static function curr() |
46
|
|
|
{ |
47
|
16 |
|
return self::singleton()->current(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Singleton prevents constructing a ShoppingCart any other way. |
52
|
|
|
*/ |
53
|
2 |
|
private function __construct() |
54
|
|
|
{ |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the current order, or return null if it doesn't exist. |
59
|
|
|
* |
60
|
|
|
* @return Order |
61
|
|
|
*/ |
62
|
90 |
|
public function current() |
63
|
|
|
{ |
64
|
|
|
//find order by id saved to session (allows logging out and retaining cart contents) |
65
|
90 |
|
if (!$this->order && $sessionid = Session::get(self::$cartid_session_name)) { |
66
|
|
|
$this->order = Order::get()->filter( |
67
|
|
|
array( |
68
|
|
|
"Status" => "Cart", |
69
|
|
|
"ID" => $sessionid, |
70
|
|
|
) |
71
|
|
|
)->first(); |
72
|
|
|
} |
73
|
90 |
|
if (!$this->calculateonce && $this->order) { |
74
|
2 |
|
$this->order->calculate(); |
75
|
4 |
|
$this->calculateonce = true; |
76
|
18 |
|
} |
77
|
|
|
|
78
|
90 |
|
return $this->order ? $this->order : false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the current cart |
83
|
|
|
* |
84
|
|
|
* @param Order |
85
|
|
|
* |
86
|
|
|
* @return ShoppingCart |
87
|
|
|
*/ |
88
|
7 |
|
public function setCurrent(Order $cart) |
89
|
|
|
{ |
90
|
7 |
|
if (!$cart->IsCart()) { |
91
|
|
|
trigger_error("Passed Order object is not cart status", E_ERROR); |
92
|
|
|
} |
93
|
7 |
|
$this->order = $cart; |
94
|
7 |
|
Session::set(self::$cartid_session_name, $cart->ID); |
95
|
|
|
|
96
|
7 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Helper that only allows orders to be started internally. |
101
|
|
|
* |
102
|
|
|
* @return Order |
103
|
|
|
*/ |
104
|
17 |
|
protected function findOrMake() |
105
|
|
|
{ |
106
|
17 |
|
if ($this->current()) { |
107
|
17 |
|
return $this->current(); |
108
|
|
|
} |
109
|
13 |
|
$this->order = Order::create(); |
110
|
13 |
|
if (Member::config()->login_joins_cart && Member::currentUserID()) { |
111
|
4 |
|
$this->order->MemberID = Member::currentUserID(); |
112
|
4 |
|
} |
113
|
13 |
|
$this->order->write(); |
114
|
13 |
|
$this->order->extend('onStartOrder'); |
115
|
13 |
|
Session::set(self::$cartid_session_name, $this->order->ID); |
116
|
|
|
|
117
|
13 |
|
return $this->order; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Adds an item to the cart |
122
|
|
|
* |
123
|
|
|
* @param Buyable $buyable |
124
|
|
|
* @param number $quantity |
125
|
|
|
* @param unknown $filter |
126
|
|
|
* |
127
|
|
|
* @return boolean|OrderItem false or the new/existing item |
128
|
|
|
*/ |
129
|
16 |
|
public function add(Buyable $buyable, $quantity = 1, $filter = array()) |
130
|
|
|
{ |
131
|
16 |
|
$order = $this->findOrMake(); |
132
|
16 |
|
$order->extend("beforeAdd", $buyable, $quantity, $filter); |
133
|
16 |
|
if (!$buyable) { |
134
|
|
|
|
135
|
|
|
return $this->error(_t("ShoppingCart.ProductNotFound", "Product not found.")); |
136
|
|
|
} |
137
|
16 |
|
$item = $this->findOrMakeItem($buyable, $filter); |
138
|
16 |
|
if (!$item) { |
139
|
|
|
|
140
|
1 |
|
return false; |
141
|
|
|
} |
142
|
16 |
|
if (!$item->_brandnew) { |
|
|
|
|
143
|
3 |
|
$item->Quantity += $quantity; |
|
|
|
|
144
|
3 |
|
} else { |
145
|
16 |
|
$item->Quantity = $quantity; |
|
|
|
|
146
|
|
|
} |
147
|
16 |
|
$item->write(); |
148
|
16 |
|
$order->extend("afterAdd", $item, $buyable, $quantity, $filter); |
149
|
16 |
|
$this->message(_t("ShoppingCart.ItemAdded", "Item has been added successfully.")); |
150
|
|
|
|
151
|
16 |
|
return $item; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Remove an item from the cart. |
156
|
|
|
* |
157
|
|
|
* @param id or Buyable $buyable |
158
|
|
|
* @param $item |
159
|
|
|
* @param int $quantity - number of items to remove, or leave null for all items (default) |
160
|
|
|
* |
161
|
|
|
* @return boolean success/failure |
162
|
|
|
*/ |
163
|
4 |
|
public function remove(Buyable $buyable, $quantity = null, $filter = array()) |
164
|
|
|
{ |
165
|
4 |
|
$order = $this->current(); |
166
|
|
|
|
167
|
4 |
|
if (!$order) { |
168
|
|
|
return $this->error(_t("ShoppingCart.NoOrder", "No current order.")); |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
$order->extend("beforeRemove", $buyable, $quantity, $filter); |
172
|
|
|
|
173
|
4 |
|
$item = $this->get($buyable, $filter); |
174
|
|
|
|
175
|
4 |
|
if (!$item) { |
176
|
1 |
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
//if $quantity will become 0, then remove all |
180
|
4 |
|
if (!$quantity || ($item->Quantity - $quantity) <= 0) { |
181
|
4 |
|
$item->delete(); |
182
|
4 |
|
$item->destroy(); |
183
|
4 |
|
} else { |
184
|
1 |
|
$item->Quantity -= $quantity; |
185
|
1 |
|
$item->write(); |
186
|
|
|
} |
187
|
4 |
|
$order->extend("afterRemove", $item, $buyable, $quantity, $filter); |
188
|
4 |
|
$this->message(_t("ShoppingCart.ItemRemoved", "Item has been successfully removed.")); |
189
|
|
|
|
190
|
4 |
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sets the quantity of an item in the cart. |
195
|
|
|
* Will automatically add or remove item, if necessary. |
196
|
|
|
* |
197
|
|
|
* @param id or Buyable $buyable |
198
|
|
|
* @param $item |
199
|
|
|
* @param int $quantity |
200
|
|
|
* |
201
|
|
|
* @return boolean|OrderItem false or the new/existing item |
202
|
|
|
*/ |
203
|
3 |
|
public function setQuantity(Buyable $buyable, $quantity = 1, $filter = array()) |
204
|
|
|
{ |
205
|
3 |
|
if ($quantity <= 0) { |
206
|
|
|
return $this->remove($buyable, $quantity, $filter); |
207
|
|
|
} |
208
|
3 |
|
$order = $this->findOrMake(); |
209
|
3 |
|
$item = $this->findOrMakeItem($buyable, $filter); |
210
|
3 |
|
if (!$item) { |
211
|
|
|
|
212
|
|
|
return false; |
213
|
|
|
} |
214
|
3 |
|
$order->extend("beforeSetQuantity", $buyable, $quantity, $filter); |
215
|
3 |
|
$item->Quantity = $quantity; |
|
|
|
|
216
|
3 |
|
$item->write(); |
217
|
3 |
|
$order->extend("afterSetQuantity", $item, $buyable, $quantity, $filter); |
218
|
3 |
|
$this->message(_t("ShoppingCart.QuantitySet", "Quantity has been set.")); |
219
|
|
|
|
220
|
3 |
|
return $item; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Finds or makes an order item for a given product + filter. |
225
|
|
|
* |
226
|
|
|
* @param id or Buyable $buyable |
227
|
|
|
* @param string $filter |
228
|
|
|
* |
229
|
|
|
* @return OrderItem the found or created item |
230
|
|
|
*/ |
231
|
17 |
|
private function findOrMakeItem(Buyable $buyable, $filter = array()) |
232
|
|
|
{ |
233
|
17 |
|
$order = $this->findOrMake(); |
234
|
|
|
|
235
|
17 |
|
if (!$buyable || !$order) { |
236
|
|
|
return false; |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
17 |
|
$item = $this->get($buyable, $filter); |
|
|
|
|
240
|
|
|
|
241
|
17 |
|
if (!$item) { |
242
|
17 |
|
$member = Member::currentUser(); |
243
|
|
|
|
244
|
17 |
|
if (!$buyable->canPurchase($member)) { |
|
|
|
|
245
|
1 |
|
return $this->error( |
|
|
|
|
246
|
1 |
|
_t( |
247
|
1 |
|
'ShoppingCart.CannotPurchase', |
248
|
1 |
|
'This {Title} cannot be purchased.', |
249
|
1 |
|
'', |
250
|
1 |
|
array('Title' => $buyable->i18n_singular_name()) |
|
|
|
|
251
|
1 |
|
) |
252
|
1 |
|
); |
253
|
|
|
//TODO: produce a more specific message |
254
|
|
|
} |
255
|
|
|
|
256
|
17 |
|
$item = $buyable->createItem(1, $filter); |
|
|
|
|
257
|
17 |
|
$item->OrderID = $order->ID; |
|
|
|
|
258
|
17 |
|
$item->write(); |
259
|
|
|
|
260
|
17 |
|
$order->Items()->add($item); |
|
|
|
|
261
|
|
|
|
262
|
17 |
|
$item->_brandnew = true; // flag as being new |
|
|
|
|
263
|
17 |
|
} |
264
|
|
|
|
265
|
17 |
|
return $item; |
266
|
3 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Finds an existing order item. |
270
|
|
|
* |
271
|
|
|
* @param Buyable $buyable |
272
|
|
|
* @param string $filter |
|
|
|
|
273
|
|
|
* |
274
|
|
|
* @return the item requested, or false |
275
|
|
|
*/ |
276
|
20 |
|
public function get(Buyable $buyable, $customfilter = array()) |
277
|
|
|
{ |
278
|
20 |
|
$order = $this->current(); |
279
|
20 |
|
if (!$buyable || !$order) { |
280
|
3 |
|
return false; |
|
|
|
|
281
|
14 |
|
} |
282
|
14 |
|
$filter = array( |
283
|
19 |
|
'OrderID' => $order->ID, |
284
|
19 |
|
); |
285
|
19 |
|
$itemclass = Config::inst()->get(get_class($buyable), 'order_item'); |
286
|
19 |
|
$relationship = Config::inst()->get($itemclass, 'buyable_relationship'); |
287
|
19 |
|
$filter[$relationship . "ID"] = $buyable->ID; |
|
|
|
|
288
|
19 |
|
$required = array('Order', $relationship); |
289
|
19 |
|
if (is_array($itemclass::config()->required_fields)) { |
290
|
19 |
|
$required = array_merge($required, $itemclass::config()->required_fields); |
291
|
19 |
|
} |
292
|
19 |
|
$query = new MatchObjectFilter($itemclass, array_merge($customfilter, $filter), $required); |
293
|
19 |
|
$item = $itemclass::get()->where($query->getFilter())->first(); |
294
|
19 |
|
if (!$item) { |
295
|
19 |
|
return $this->error(_t("ShoppingCart.ItemNotFound", "Item not found.")); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
11 |
|
return $item; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Store old cart id in session order history |
303
|
|
|
* @param int|null $requestedOrderId optional parameter that denotes the order that was requested |
304
|
|
|
*/ |
305
|
2 |
|
public function archiveorderid($requestedOrderId = null) |
306
|
|
|
{ |
307
|
1 |
|
$sessionId = Session::get(self::$cartid_session_name); |
308
|
2 |
|
$order = Order::get() |
309
|
|
|
->filter("Status:not", "Cart") |
310
|
|
|
->byId($sessionId); |
311
|
|
|
if ($order && !$order->IsCart()) { |
312
|
|
|
OrderManipulation::add_session_order($order); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
// in case there was no order requested |
315
|
|
|
// OR there was an order requested AND it's the same one as currently in the session, |
316
|
|
|
// then clear the cart. This check is here to prevent clearing of the cart if the user just |
317
|
|
|
// wants to view an old order (via AccountPage). |
318
|
|
|
if (!$requestedOrderId || ($sessionId == $requestedOrderId)) { |
|
|
|
|
319
|
|
|
$this->clear(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Empty / abandon the entire cart. |
325
|
|
|
* |
326
|
|
|
* @return bool - true if successful, false if no cart found |
327
|
|
|
*/ |
328
|
15 |
|
public function clear() |
329
|
|
|
{ |
330
|
10 |
|
Session::clear(self::$cartid_session_name); |
331
|
10 |
|
$order = $this->current(); |
332
|
10 |
|
$this->order = null; |
333
|
10 |
|
if (!$order) { |
334
|
4 |
|
return $this->error(_t("ShoppingCart.NoCartFound", "No cart found.")); |
335
|
|
|
} |
336
|
9 |
|
$order->write(); |
337
|
9 |
|
$this->message(_t("ShoppingCart.Cleared", "Cart was successfully cleared.")); |
338
|
|
|
|
339
|
9 |
|
return true; |
340
|
14 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Store a new error. |
344
|
|
|
*/ |
345
|
20 |
|
protected function error($message) |
346
|
|
|
{ |
347
|
20 |
|
$this->message($message, "bad"); |
348
|
|
|
|
349
|
20 |
|
return false; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Store a message to be fed back to user. |
354
|
|
|
* |
355
|
|
|
* @param string $message |
356
|
|
|
* @param string $type - good, bad, warning |
357
|
|
|
*/ |
358
|
21 |
|
protected function message($message, $type = "good") |
359
|
|
|
{ |
360
|
21 |
|
$this->message = $message; |
361
|
21 |
|
$this->type = $type; |
362
|
21 |
|
} |
363
|
|
|
|
364
|
|
|
public function getMessage() |
365
|
|
|
{ |
366
|
|
|
return $this->message; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function getMessageType() |
370
|
|
|
{ |
371
|
|
|
return $this->type; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function clearMessage() |
375
|
|
|
{ |
376
|
|
|
$this->message = null; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
//singleton protection |
380
|
|
|
public function __clone() |
381
|
|
|
{ |
382
|
|
|
trigger_error('Clone is not allowed.', E_USER_ERROR); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function __wakeup() |
386
|
|
|
{ |
387
|
|
|
trigger_error('Unserializing is not allowed.', E_USER_ERROR); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Manipulate the cart via urls. |
393
|
|
|
*/ |
394
|
|
|
class ShoppingCart_Controller extends Controller |
|
|
|
|
395
|
|
|
{ |
396
|
|
|
private static $url_segment = "shoppingcart"; |
397
|
|
|
|
398
|
|
|
private static $direct_to_cart_page = false; |
399
|
|
|
|
400
|
|
|
protected $cart; |
401
|
|
|
|
402
|
|
|
private static $url_handlers = array( |
|
|
|
|
403
|
|
|
'$Action/$Buyable/$ID' => 'handleAction', |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
private static $allowed_actions = array( |
|
|
|
|
407
|
|
|
'add', |
408
|
|
|
'additem', |
409
|
|
|
'remove', |
410
|
|
|
'removeitem', |
411
|
|
|
'removeall', |
412
|
|
|
'removeallitem', |
413
|
|
|
'setquantity', |
414
|
|
|
'setquantityitem', |
415
|
|
|
'clear', |
416
|
|
|
'debug', |
417
|
|
|
); |
418
|
|
|
|
419
|
5 |
|
public static function add_item_link(Buyable $buyable, $parameters = array()) |
420
|
|
|
{ |
421
|
5 |
|
return self::build_url("add", $buyable, $parameters); |
422
|
|
|
} |
423
|
|
|
|
424
|
2 |
|
public static function remove_item_link(Buyable $buyable, $parameters = array()) |
425
|
|
|
{ |
426
|
2 |
|
return self::build_url("remove", $buyable, $parameters); |
427
|
|
|
} |
428
|
|
|
|
429
|
2 |
|
public static function remove_all_item_link(Buyable $buyable, $parameters = array()) |
430
|
|
|
{ |
431
|
2 |
|
return self::build_url("removeall", $buyable, $parameters); |
432
|
|
|
} |
433
|
|
|
|
434
|
3 |
|
public static function set_quantity_item_link(Buyable $buyable, $parameters = array()) |
435
|
|
|
{ |
436
|
3 |
|
return self::build_url("setquantity", $buyable, $parameters); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Helper for creating a url |
441
|
|
|
*/ |
442
|
5 |
|
protected static function build_url($action, $buyable, $params = array()) |
443
|
|
|
{ |
444
|
5 |
|
if (!$action || !$buyable) { |
445
|
|
|
return false; |
446
|
|
|
} |
447
|
5 |
|
if (SecurityToken::is_enabled()) { |
448
|
|
|
$params[SecurityToken::inst()->getName()] = SecurityToken::inst()->getValue(); |
449
|
|
|
} |
450
|
5 |
|
return self::config()->url_segment . '/' . |
451
|
5 |
|
$action . '/' . |
452
|
5 |
|
$buyable->class . "/" . |
453
|
5 |
|
$buyable->ID . |
454
|
5 |
|
self::params_to_get_string($params); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Creates the appropriate string parameters for links from array |
459
|
|
|
* |
460
|
|
|
* Produces string such as: MyParam%3D11%26OtherParam%3D1 |
461
|
|
|
* ...which decodes to: MyParam=11&OtherParam=1 |
462
|
|
|
* |
463
|
|
|
* you will need to decode the url with javascript before using it. |
464
|
|
|
*/ |
465
|
5 |
|
protected static function params_to_get_string($array) |
466
|
|
|
{ |
467
|
5 |
|
if ($array & count($array > 0)) { |
468
|
2 |
|
array_walk($array, create_function('&$v,$k', '$v = $k."=".$v ;')); |
469
|
2 |
|
return "?" . implode("&", $array); |
470
|
|
|
} |
471
|
5 |
|
return ""; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* This is used here and in VariationForm and AddProductForm |
476
|
|
|
* |
477
|
|
|
* @param bool|string $status |
478
|
|
|
* |
479
|
|
|
* @return bool |
480
|
|
|
*/ |
481
|
3 |
|
public static function direct($status = true) |
482
|
|
|
{ |
483
|
3 |
|
if (Director::is_ajax()) { |
484
|
|
|
return $status; |
485
|
|
|
} |
486
|
3 |
|
if (self::config()->direct_to_cart_page && $cartlink = CartPage::find_link()) { |
487
|
|
|
Controller::curr()->redirect($cartlink); |
488
|
|
|
return; |
489
|
|
|
} else { |
490
|
3 |
|
Controller::curr()->redirectBack(); |
491
|
3 |
|
return; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
3 |
|
public function init() |
496
|
|
|
{ |
497
|
3 |
|
parent::init(); |
498
|
3 |
|
$this->cart = ShoppingCart::singleton(); |
499
|
3 |
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @return Product|ProductVariation|Buyable |
503
|
|
|
*/ |
504
|
3 |
|
protected function buyableFromRequest() |
|
|
|
|
505
|
|
|
{ |
506
|
3 |
|
$request = $this->getRequest(); |
507
|
3 |
|
if (SecurityToken::is_enabled() && !SecurityToken::inst()->checkRequest($request)) { |
508
|
|
|
return $this->httpError( |
509
|
|
|
400, |
510
|
|
|
_t("ShoppingCart.InvalidSecurityToken", "Invalid security token, possible CSRF attack.") |
511
|
|
|
); |
512
|
|
|
} |
513
|
3 |
|
$id = (int)$request->param('ID'); |
514
|
3 |
|
if (empty($id)) { |
515
|
|
|
//TODO: store error message |
516
|
|
|
return null; |
517
|
|
|
} |
518
|
3 |
|
$buyableclass = "Product"; |
519
|
3 |
|
if ($class = $request->param('Buyable')) { |
520
|
3 |
|
$buyableclass = Convert::raw2sql($class); |
521
|
3 |
|
} |
522
|
3 |
|
if (!ClassInfo::exists($buyableclass)) { |
523
|
|
|
//TODO: store error message |
524
|
|
|
return null; |
525
|
|
|
} |
526
|
|
|
//ensure only live products are returned, if they are versioned |
527
|
3 |
|
$buyable = Object::has_extension($buyableclass, 'Versioned') |
|
|
|
|
528
|
|
|
? |
529
|
3 |
|
Versioned::get_by_stage($buyableclass, 'Live')->byID($id) |
|
|
|
|
530
|
3 |
|
: |
531
|
3 |
|
DataObject::get($buyableclass)->byID($id); |
|
|
|
|
532
|
3 |
|
if (!$buyable || !($buyable instanceof Buyable)) { |
533
|
|
|
//TODO: store error message |
534
|
1 |
|
return null; |
535
|
|
|
} |
536
|
3 |
|
return $buyable; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Action: add item to cart |
541
|
|
|
* |
542
|
|
|
* @param SS_HTTPRequest $request |
543
|
|
|
* |
544
|
|
|
* @return SS_HTTPResponse |
545
|
|
|
*/ |
546
|
3 |
|
public function add($request) |
547
|
|
|
{ |
548
|
3 |
|
if ($product = $this->buyableFromRequest()) { |
549
|
3 |
|
$quantity = (int)$request->getVar('quantity'); |
550
|
3 |
|
if (!$quantity) { |
551
|
3 |
|
$quantity = 1; |
552
|
3 |
|
} |
553
|
3 |
|
$this->cart->add($product, $quantity, $request->getVars()); |
554
|
3 |
|
} |
555
|
|
|
|
556
|
3 |
|
$this->updateLocale($request); |
557
|
3 |
|
$this->extend('updateAddResponse', $request, $response, $product, $quantity); |
558
|
3 |
|
return $response ? $response : self::direct(); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Action: remove a certain number of items from the cart |
563
|
|
|
* |
564
|
|
|
* @param SS_HTTPRequest $request |
565
|
|
|
* |
566
|
|
|
* @return SS_HTTPResponse |
567
|
|
|
*/ |
568
|
1 |
|
public function remove($request) |
569
|
|
|
{ |
570
|
1 |
|
if ($product = $this->buyableFromRequest()) { |
571
|
1 |
|
$this->cart->remove($product, $quantity = 1, $request->getVars()); |
572
|
1 |
|
} |
573
|
|
|
|
574
|
1 |
|
$this->updateLocale($request); |
575
|
1 |
|
$this->extend('updateRemoveResponse', $request, $response, $product, $quantity); |
576
|
1 |
|
return $response ? $response : self::direct(); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Action: remove all of an item from the cart |
581
|
|
|
* |
582
|
|
|
* @param SS_HTTPRequest $request |
583
|
|
|
* |
584
|
|
|
* @return SS_HTTPResponse |
585
|
|
|
*/ |
586
|
1 |
|
public function removeall($request) |
587
|
|
|
{ |
588
|
1 |
|
if ($product = $this->buyableFromRequest()) { |
589
|
1 |
|
$this->cart->remove($product, null, $request->getVars()); |
590
|
1 |
|
} |
591
|
|
|
|
592
|
1 |
|
$this->updateLocale($request); |
593
|
1 |
|
$this->extend('updateRemoveAllResponse', $request, $response, $product); |
594
|
1 |
|
return $response ? $response : self::direct(); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Action: update the quantity of an item in the cart |
599
|
|
|
* |
600
|
|
|
* @param $request |
601
|
|
|
* |
602
|
|
|
* @return AjaxHTTPResponse|bool |
603
|
|
|
*/ |
604
|
2 |
|
public function setquantity($request) |
605
|
|
|
{ |
606
|
2 |
|
$product = $this->buyableFromRequest(); |
607
|
2 |
|
$quantity = (int)$request->getVar('quantity'); |
608
|
2 |
|
if ($product) { |
609
|
2 |
|
$this->cart->setQuantity($product, $quantity, $request->getVars()); |
610
|
2 |
|
} |
611
|
|
|
|
612
|
2 |
|
$this->updateLocale($request); |
613
|
2 |
|
$this->extend('updateSetQuantityResponse', $request, $response, $product, $quantity); |
614
|
2 |
|
return $response ? $response : self::direct(); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Action: clear the cart |
619
|
|
|
* |
620
|
|
|
* @param $request |
621
|
|
|
* |
622
|
|
|
* @return AjaxHTTPResponse|bool |
623
|
|
|
*/ |
624
|
|
|
public function clear($request) |
625
|
|
|
{ |
626
|
|
|
$this->updateLocale($request); |
627
|
|
|
$this->cart->clear(); |
628
|
|
|
$this->extend('updateClearResponse', $request, $response); |
629
|
|
|
return $response ? $response : self::direct(); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Handle index requests |
634
|
|
|
*/ |
635
|
|
|
public function index() |
636
|
|
|
{ |
637
|
|
|
if ($cart = $this->Cart()) { |
|
|
|
|
638
|
|
|
$this->redirect($cart->CartLink); |
639
|
|
|
return; |
640
|
|
|
} elseif ($response = ErrorPage::response_for(404)) { |
641
|
|
|
return $response; |
642
|
|
|
} |
643
|
|
|
return $this->httpError(404, _t("ShoppingCart.NoCartInitialised", "no cart initialised")); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Displays order info and cart contents. |
648
|
|
|
*/ |
649
|
|
|
public function debug() |
650
|
|
|
{ |
651
|
|
|
if (Director::isDev() || Permission::check("ADMIN")) { |
652
|
|
|
//TODO: allow specifying a particular id to debug |
653
|
|
|
Requirements::css(SHOP_DIR . "/css/cartdebug.css"); |
654
|
|
|
$order = ShoppingCart::curr(); |
655
|
|
|
$content = ($order) |
656
|
|
|
? |
657
|
|
|
Debug::text($order) |
|
|
|
|
658
|
|
|
: |
659
|
|
|
"Cart has not been created yet. Add a product."; |
660
|
|
|
return array('Content' => $content); |
|
|
|
|
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
|
664
|
3 |
|
protected function updateLocale($request) |
665
|
|
|
{ |
666
|
3 |
|
$order = $this->cart->current(); |
667
|
3 |
|
if ($request && $request->isAjax() && $order) { |
668
|
|
|
ShopTools::install_locale($order->Locale); |
669
|
|
|
} |
670
|
3 |
|
} |
671
|
|
|
} |
672
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.