1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ShoppingCart\Control; |
4
|
|
|
|
5
|
|
|
use SilverStripe\i18n\i18n; |
6
|
|
|
use SilverStripe\Forms\Form; |
7
|
|
|
use SilverStripe\View\SSViewer; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\TextField; |
10
|
|
|
use SilverStripe\Control\Director; |
11
|
|
|
use SilverStripe\Forms\FormAction; |
12
|
|
|
use SilverStripe\Control\Controller; |
13
|
|
|
use SilverStripe\ORM\ValidationResult; |
14
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
15
|
|
|
use SilverStripe\Core\Injector\Injector; |
16
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
|
|
|
|
17
|
|
|
use SilverStripe\ORM\ValidationException; |
18
|
|
|
use SilverCommerce\Discounts\Model\Discount; |
19
|
|
|
use SilverCommerce\Checkout\Control\Checkout; |
|
|
|
|
20
|
|
|
use SilverCommerce\Discounts\DiscountFactory; |
21
|
|
|
use SilverCommerce\Postage\Forms\PostageForm; |
22
|
|
|
use SilverCommerce\OrdersAdmin\Model\Estimate; |
23
|
|
|
use SilverCommerce\Discounts\Model\AppliedDiscount; |
24
|
|
|
use SilverStripe\CMS\Controllers\ContentController; |
|
|
|
|
25
|
|
|
use SilverCommerce\Discounts\Forms\DiscountCodeForm; |
26
|
|
|
use SilverCommerce\ShoppingCart\ShoppingCartFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Holder for items in the shopping cart and interacting with them, as |
30
|
|
|
* well as rendering these items into an interface that allows editing |
31
|
|
|
* of items, |
32
|
|
|
* |
33
|
|
|
* @author ilateral (http://www.ilateral.co.uk) |
34
|
|
|
* @package shoppingcart |
35
|
|
|
*/ |
36
|
|
|
class ShoppingCart extends Controller |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* URL Used to access this controller |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
* @config |
44
|
|
|
*/ |
45
|
|
|
private static $url_segment = 'shoppingcart'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Setup default templates for this controller |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $templates = [ |
53
|
|
|
"index" => [ShoppingCart::class, "Page"], |
54
|
|
|
"usediscount" => [ShoppingCart::class . "_usediscount", ShoppingCart::class, "Page"] |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Overwrite the default title for this controller which is taken |
59
|
|
|
* from the translation files. This is used for Title and MetaTitle |
60
|
|
|
* variables in templates. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
* @config |
64
|
|
|
*/ |
65
|
|
|
private static $title; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Class Name of object we use as an assotiated estimate. |
69
|
|
|
* This defaults to Estimate |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
* @config |
73
|
|
|
*/ |
74
|
|
|
private static $checkout_class = Checkout::class; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Redirect the user to the cart when an item is added? |
78
|
|
|
* |
79
|
|
|
* @var boolean |
80
|
|
|
*/ |
81
|
|
|
private static $redirect_on_add = false; |
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The associated dataRecord |
85
|
|
|
* |
86
|
|
|
* @var ShoppingCartModel |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
protected $dataRecord; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* These methods are mapped to sub URLs of this |
92
|
|
|
* controller. |
93
|
|
|
* |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
private static $allowed_actions = [ |
|
|
|
|
97
|
|
|
"remove", |
98
|
|
|
"emptycart", |
99
|
|
|
"usediscount", |
100
|
|
|
"setdeliverytype", |
101
|
|
|
"checkout", |
102
|
|
|
'removediscount', |
103
|
|
|
"CartForm", |
104
|
|
|
"PostageForm", |
105
|
|
|
"DiscountForm" |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Overwrite default init to support subsites (if installed) |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
protected function init() |
114
|
|
|
{ |
115
|
|
|
parent::init(); |
116
|
|
|
|
117
|
|
|
// Setup current controller on init (as Security::getMember() |
118
|
|
|
// is not available on construction) |
119
|
|
|
$dataRecord = ShoppingCartFactory::create()->getCurrent(); |
120
|
|
|
$this->setDataRecord($dataRecord); |
121
|
|
|
$this->setFailover($this->dataRecord); |
122
|
|
|
|
123
|
|
|
# Check for subsites and add support |
124
|
|
|
if (class_exists(Subsite::class)) { |
125
|
|
|
$subsite = Subsite::currentSubsite(); |
126
|
|
|
|
127
|
|
|
if ($subsite && $subsite->Theme) { |
128
|
|
|
SSViewer::add_themes([$subsite->Theme]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($subsite && i18n::getData()->validate($subsite->Language)) { |
132
|
|
|
i18n::set_locale($subsite->Language); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getTitle() |
138
|
|
|
{ |
139
|
|
|
if ($this->config()->title) { |
140
|
|
|
return $this->config()->title; |
141
|
|
|
} else { |
142
|
|
|
_t("SilverCommerce\ShoppingCart.CartName", "Shopping Cart"); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getMetaTitle() |
147
|
|
|
{ |
148
|
|
|
return $this->getTitle(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getDataRecord() |
152
|
|
|
{ |
153
|
|
|
return $this->dataRecord; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setDataRecord($record) |
157
|
|
|
{ |
158
|
|
|
$this->dataRecord = $record; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the link to this controller |
164
|
|
|
* |
165
|
|
|
* @param string $action The action you want to add to the link |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function Link($action = null) |
169
|
|
|
{ |
170
|
|
|
return Controller::join_links( |
171
|
|
|
$this->config()->url_segment, |
172
|
|
|
$action |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get an absolute link to this controller |
178
|
|
|
* |
179
|
|
|
* @param string $action The action you want to add to the link |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function AbsoluteLink($action = null) |
183
|
|
|
{ |
184
|
|
|
return Director::absoluteURL($this->Link($action)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get a relative (to the root url of the site) link to this |
189
|
|
|
* controller |
190
|
|
|
* |
191
|
|
|
* @param string $action The action you want to add to the link |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function RelativeLink($action = null) |
195
|
|
|
{ |
196
|
|
|
return Controller::join_links( |
197
|
|
|
Director::baseURL(), |
198
|
|
|
$this->Link($action) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* If content controller exists, return it's menu function |
204
|
|
|
* @param int $level Menu level to return. |
205
|
|
|
* @return ArrayList |
|
|
|
|
206
|
|
|
*/ |
207
|
|
|
public function getMenu($level = 1) |
208
|
|
|
{ |
209
|
|
|
if (class_exists(ContentController::class)) { |
210
|
|
|
$controller = ContentController::singleton(); |
211
|
|
|
return $controller->getMenu($level); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function Menu($level) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
return $this->getMenu(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Default acton for the shopping cart |
222
|
|
|
*/ |
223
|
|
|
public function index() |
224
|
|
|
{ |
225
|
|
|
$this->extend("onBeforeIndex"); |
226
|
|
|
|
227
|
|
|
return $this->render(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Remove a product from ShoppingCart Via its ID. This action |
232
|
|
|
* expects an ID to be sent through the URL that matches a specific |
233
|
|
|
* key added to an item in the cart |
234
|
|
|
* |
235
|
|
|
* @return Redirect |
|
|
|
|
236
|
|
|
*/ |
237
|
|
|
public function remove() |
238
|
|
|
{ |
239
|
|
|
$key = $this->request->param('ID'); |
240
|
|
|
$item = null; |
241
|
|
|
$title = null; |
|
|
|
|
242
|
|
|
|
243
|
|
|
if (isset($key)) { |
244
|
|
|
$item = $this |
245
|
|
|
->Items() |
|
|
|
|
246
|
|
|
->find("Key", $key); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
if (isset($item)) { |
250
|
|
|
$title = $item->Title; |
251
|
|
|
ShoppingCartFactory::create()->removeItem($item); |
252
|
|
|
|
253
|
|
|
$form = $this->CartForm(); |
254
|
|
|
$form->sessionMessage(_t( |
255
|
|
|
"ShoppingCart.RemovedItem", |
256
|
|
|
"Removed '{title}' from your cart", |
257
|
|
|
["title" => $title] |
258
|
|
|
)); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this->redirectBack(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Action that will clear shopping cart and associated items |
266
|
|
|
* |
267
|
|
|
*/ |
268
|
|
|
public function emptycart() |
269
|
|
|
{ |
270
|
|
|
ShoppingCartFactory::create()->delete(); |
271
|
|
|
|
272
|
|
|
$form = $this->CartForm(); |
273
|
|
|
$form->sessionMessage(_t( |
274
|
|
|
"ShoppingCart.EmptiedCart", |
275
|
|
|
"Shopping cart emptied" |
276
|
|
|
)); |
277
|
|
|
|
278
|
|
|
return $this->redirectBack(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Action used to add a discount to the users session via a URL. |
284
|
|
|
* This is preferable to using the dicount form as disount code |
285
|
|
|
* forms seem to provide a less than perfect user experience |
286
|
|
|
* |
287
|
|
|
*/ |
288
|
|
|
public function usediscount() |
289
|
|
|
{ |
290
|
|
|
$code_to_search = $this->request->param("ID"); |
291
|
|
|
$code = false; |
292
|
|
|
$curr = $this->getDiscount(); |
|
|
|
|
293
|
|
|
|
294
|
|
|
if (!$code_to_search) { |
295
|
|
|
return $this->httpError(404, "Page not found"); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// First check if the discount is already added (so we don't |
299
|
|
|
// query the DB if we don't have to). |
300
|
|
|
if (!$curr || ($curr && $curr->Code != $code_to_search)) { |
301
|
|
|
$this->setDiscount($code_to_search); |
|
|
|
|
302
|
|
|
ShoppingCartFactory::create()->save(); |
303
|
|
|
} elseif ($curr && $code->Code == $code_to_search) { |
|
|
|
|
304
|
|
|
$code = $this->getDiscount(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$this->extend("onBeforeUseDiscount"); |
308
|
|
|
|
309
|
|
|
return $this |
310
|
|
|
->customise([ |
311
|
|
|
"Discount" => $code |
312
|
|
|
])->render(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Setup the checkout and redirect to it |
317
|
|
|
* |
318
|
|
|
* @return Redirect |
319
|
|
|
*/ |
320
|
|
|
public function checkout() |
321
|
|
|
{ |
322
|
|
|
if (!class_exists($this->config()->checkout_class)) { |
323
|
|
|
return $this->httpError(404); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$checkout = Injector::inst() |
327
|
|
|
->get($this->config()->checkout_class); |
328
|
|
|
$checkout->setEstimate($this->dataRecord); |
329
|
|
|
|
330
|
|
|
$this->extend("onBeforeCheckout"); |
331
|
|
|
|
332
|
|
|
$this->redirect($checkout->Link()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function removediscount() |
336
|
|
|
{ |
337
|
|
|
$id = $this->request->param('ID'); |
338
|
|
|
|
339
|
|
|
$discount = AppliedDiscount::get()->byID($id); |
|
|
|
|
340
|
|
|
|
341
|
|
|
if ($discount->exists()) { |
342
|
|
|
$discount->delete(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$this->redirectBack(); |
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Shortcut to checkout config, to allow us to access it via |
351
|
|
|
* templates |
352
|
|
|
* |
353
|
|
|
* @return boolean |
354
|
|
|
*/ |
355
|
|
|
public function ShowTax() |
356
|
|
|
{ |
357
|
|
|
$config = SiteConfig::current_site_config(); |
358
|
|
|
return $config->ShowPriceAndTax; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Form responsible for listing items in the shopping cart and |
363
|
|
|
* allowing management (such as addition, removal, etc) |
364
|
|
|
* |
365
|
|
|
* @return Form |
366
|
|
|
*/ |
367
|
|
|
public function CartForm() |
368
|
|
|
{ |
369
|
|
|
$form = Form::create( |
370
|
|
|
$this, |
371
|
|
|
"CartForm", |
372
|
|
|
FieldList::create(), |
373
|
|
|
FieldList::create( |
374
|
|
|
FormAction::create( |
375
|
|
|
'doUpdate', |
376
|
|
|
_t('ShoppingCart.UpdateCart', 'Update Cart') |
377
|
|
|
)->addExtraClass('btn btn-info') |
378
|
|
|
) |
379
|
|
|
)->setTemplate("SilverCommerce\ShoppingCart\Forms\Includes\ShoppingCartForm"); |
380
|
|
|
|
381
|
|
|
$this->extend("updateCartForm", $form); |
382
|
|
|
|
383
|
|
|
return $form; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Form that allows you to add a discount code which then gets added |
388
|
|
|
* to the cart's list of discounts. |
389
|
|
|
* |
390
|
|
|
* @return Form |
391
|
|
|
*/ |
392
|
|
|
public function DiscountForm() |
393
|
|
|
{ |
394
|
|
|
$form = DiscountCodeForm::create( |
395
|
|
|
$this, |
396
|
|
|
"DiscountForm", |
397
|
|
|
ShoppingCartFactory::create()->getCurrent() |
398
|
|
|
); |
399
|
|
|
|
400
|
|
|
$this->extend("updateDiscountForm", $form); |
401
|
|
|
|
402
|
|
|
return $form; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Form responsible for estimating shipping based on location and |
407
|
|
|
* postal code |
408
|
|
|
* |
409
|
|
|
* @return Form |
410
|
|
|
*/ |
411
|
|
|
public function PostageForm() |
412
|
|
|
{ |
413
|
|
|
if ($this->isDeliverable()) { |
|
|
|
|
414
|
|
|
$form = PostageForm::create( |
415
|
|
|
$this, |
416
|
|
|
"PostageForm", |
417
|
|
|
$this->dataRecord, |
418
|
|
|
$this->dataRecord->SubTotal, |
419
|
|
|
$this->dataRecord->TotalWeight, |
420
|
|
|
$this->dataRecord->TotalItems |
421
|
|
|
); |
422
|
|
|
|
423
|
|
|
$form->setLegend(_t( |
424
|
|
|
"SilverCommerce\ShoppingCart.EstimatePostage", |
425
|
|
|
"Estimate Postage" |
426
|
|
|
)); |
427
|
|
|
|
428
|
|
|
// Extension call |
429
|
|
|
$this->extend("updatePostageForm", $form); |
430
|
|
|
|
431
|
|
|
return $form; |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Action that will update cart |
437
|
|
|
* |
438
|
|
|
* @param type $data |
439
|
|
|
* @param type $form |
|
|
|
|
440
|
|
|
*/ |
441
|
|
|
public function doUpdate($data, $form) |
442
|
|
|
{ |
443
|
|
|
try { |
444
|
|
|
foreach ($this->Items() as $item) { |
445
|
|
|
foreach ($data as $key => $value) { |
446
|
|
|
$sliced_key = explode("_", $key); |
447
|
|
|
if ($sliced_key[0] == "Quantity") { |
448
|
|
|
if (isset($item) && ($item->Key == $sliced_key[1])) { |
449
|
|
|
if ($value > 0) { |
450
|
|
|
ShoppingCartFactory::create()->updateItem( |
451
|
|
|
$item, |
452
|
|
|
$value |
453
|
|
|
); |
454
|
|
|
} else { |
455
|
|
|
$item->delete(); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$form->sessionMessage( |
459
|
|
|
_t("ShoppingCart.UpdatedShoppingCart", "Shopping cart updated"), |
460
|
|
|
ValidationResult::TYPE_GOOD |
461
|
|
|
); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
ShoppingCartFactory::create()->save(); |
467
|
|
|
} catch (ValidationException $e) { |
468
|
|
|
$form->sessionMessage( |
469
|
|
|
$e->getMessage() |
470
|
|
|
); |
471
|
|
|
} catch (Exception $e) { |
|
|
|
|
472
|
|
|
$form->sessionMessage( |
473
|
|
|
$e->getMessage() |
474
|
|
|
); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return $this->redirectBack(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
} |
481
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths