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