1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopJsonResponse; |
4
|
|
|
|
5
|
|
|
use SilverShop\Cart\ShoppingCart; |
6
|
|
|
use SilverShop\Page\Product; |
7
|
|
|
use SilverShop\Extension\ProductImageExtension; |
8
|
|
|
use SilverShop\Extension\ProductVariationsExtension; |
9
|
|
|
use SilverShop\Forms\AddProductForm; |
10
|
|
|
use SilverShop\Forms\VariationForm; |
11
|
|
|
use SilverShop\Model\Buyable; |
12
|
|
|
use SilverShop\Model\Variation\Variation; |
13
|
|
|
use SilverStripe\Core\Extension; |
14
|
|
|
use SilverStripe\Control\HTTPRequest; |
15
|
|
|
use SilverStripe\Control\HTTPResponse; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ShopJsonResponse |
19
|
|
|
* |
20
|
|
|
* Json Response for shopping cart of Silverstripe Shop |
21
|
|
|
* @package shop |
22
|
|
|
*/ |
23
|
|
|
class SilvershopJsonResponse extends Extension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Allow get action to obtain a copy of the shopping cart |
27
|
|
|
*/ |
28
|
|
|
private static $allowed_actions = array( |
|
|
|
|
29
|
|
|
'get' |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* get the shopping cart |
34
|
|
|
* |
35
|
|
|
* @param SS_HTTPRequest $request |
|
|
|
|
36
|
|
|
* @return SS_HTTPResponse $response with JSON body |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function get(HTTPRequest $request) |
39
|
|
|
{ |
40
|
|
|
if (!$request->isAjax()) { |
41
|
|
|
return $this->owner->httpError(404, _t(ShoppingCart::class . 'GetCartAjaxOnly', 'Ajax request only Bo')); |
42
|
|
|
} |
43
|
|
|
$response = $this->owner->getResponse(); |
44
|
|
|
$response->removeHeader('Content-Type'); |
45
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
46
|
|
|
|
47
|
|
|
$data = $this->getCurrentShoppingCart(); |
48
|
|
|
|
49
|
|
|
$this->owner->extend('updateGet', $data, $request, $response); |
50
|
|
|
return $response->setBody(json_encode($data)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add one of an item to a cart (Category Page) |
55
|
|
|
* |
56
|
|
|
* @see 'add' function of ShoppingCart_Controller ($this->owner) |
57
|
|
|
* @param SS_HTTPRequest $request |
58
|
|
|
* @param SS_HTTPResponse $response |
59
|
|
|
* @param Buyable $product [optional] |
60
|
|
|
* @param int $quantity [optional] |
61
|
|
|
*/ |
62
|
|
|
public function updateAddResponse(&$request, &$response, $product = null, $quantity = 1) |
63
|
|
|
{ |
64
|
|
|
if ($request->isAjax()) { |
65
|
|
|
if (!$response) { |
|
|
|
|
66
|
|
|
$response = $this->owner->getResponse(); |
67
|
|
|
} |
68
|
|
|
$response->removeHeader('Content-Type'); |
69
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
70
|
|
|
$shoppingcart = ShoppingCart::curr(); |
71
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
72
|
|
|
|
73
|
|
|
$data = $this->getCurrentShoppingCart(); |
74
|
|
|
$data['message'] = [ |
75
|
|
|
'content' => $this->owner->cart->getMessage(), |
76
|
|
|
'type' => $this->owner->cart->getMessageType() |
77
|
|
|
]; |
78
|
|
|
$this->owner->cart->clearMessage(); |
79
|
|
|
|
80
|
|
|
$this->owner->extend('updateAddResponseShopJsonResponse', $data, $request, $response, $product, $quantity); |
81
|
|
|
$response->setBody(json_encode($data)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Remove one of an item from a cart (Cart Page) |
87
|
|
|
* |
88
|
|
|
* @see 'remove' function of ShoppingCart_Controller ($this->owner) |
89
|
|
|
* @param SS_HTTPRequest $request |
90
|
|
|
* @param SS_HTTPResponse $response |
91
|
|
|
* @param Buyable $product [optional] |
92
|
|
|
* @param int $quantity [optional] |
93
|
|
|
*/ |
94
|
|
|
public function updateRemoveResponse(&$request, &$response, $product = null, $quantity = 1) |
95
|
|
|
{ |
96
|
|
|
if ($request->isAjax()) { |
97
|
|
|
if (!$response) { |
|
|
|
|
98
|
|
|
$response = $this->owner->getResponse(); |
99
|
|
|
} |
100
|
|
|
$response->removeHeader('Content-Type'); |
101
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
102
|
|
|
$shoppingcart = ShoppingCart::curr(); |
103
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
104
|
|
|
|
105
|
|
|
$data = $this->getCurrentShoppingCart(); |
106
|
|
|
$data['message'] = [ |
107
|
|
|
'content' => $this->owner->cart->getMessage(), |
108
|
|
|
'type' => $this->owner->cart->getMessageType() |
109
|
|
|
]; |
110
|
|
|
$this->owner->cart->clearMessage(); |
111
|
|
|
|
112
|
|
|
$this->owner->extend('updateRemoveResponseShopJsonResponse', $data, $request, $response, $product, $quantity); |
113
|
|
|
$response->setBody(json_encode($data)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove all of an item from a cart (Cart Page) |
119
|
|
|
* Quantity is NIL |
120
|
|
|
* |
121
|
|
|
* @see 'removeall' function of ShoppingCart_Controller ($this->owner) |
122
|
|
|
* @param SS_HTTPRequest $request |
123
|
|
|
* @param SS_HTTPResponse $response |
124
|
|
|
* @param Buyable $product [optional] |
125
|
|
|
*/ |
126
|
|
|
public function updateRemoveAllResponse(&$request, &$response, $product = null) |
127
|
|
|
{ |
128
|
|
|
if ($request->isAjax()) { |
129
|
|
|
if (!$response) { |
|
|
|
|
130
|
|
|
$response = $this->owner->getResponse(); |
131
|
|
|
} |
132
|
|
|
$response->removeHeader('Content-Type'); |
133
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
134
|
|
|
$shoppingcart = ShoppingCart::curr(); |
135
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
136
|
|
|
|
137
|
|
|
$data = $this->getCurrentShoppingCart(); |
138
|
|
|
$data['message'] = [ |
139
|
|
|
'content' => $this->owner->cart->getMessage(), |
140
|
|
|
'type' => $this->owner->cart->getMessageType() |
141
|
|
|
]; |
142
|
|
|
$this->owner->cart->clearMessage(); |
143
|
|
|
|
144
|
|
|
$this->owner->extend('updateRemoveAllResponseShopJsonResponse', $data, $request, $response, $product); |
145
|
|
|
$response->setBody(json_encode($data)); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Update the quantity of an item in a cart (Cart Page) |
151
|
|
|
* |
152
|
|
|
* @see 'setquantity' function of ShoppingCart_Controller ($this->owner) |
153
|
|
|
* @param SS_HTTPRequest $request |
154
|
|
|
* @param SS_HTTPResponse $response |
155
|
|
|
* @param Buyable $product [optional] |
156
|
|
|
* @param int $quantity [optional] |
157
|
|
|
*/ |
158
|
|
|
public function updateSetQuantityResponse(&$request, &$response, $product = null, $quantity = 1) |
159
|
|
|
{ |
160
|
|
|
if ($request->isAjax()) { |
161
|
|
|
if (!$response) { |
|
|
|
|
162
|
|
|
$response = $this->owner->getResponse(); |
163
|
|
|
} |
164
|
|
|
$response->removeHeader('Content-Type'); |
165
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
166
|
|
|
$shoppingcart = ShoppingCart::curr(); |
167
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
168
|
|
|
|
169
|
|
|
$currentquantity = (int) $product->Item()->Quantity; // quantity of the order item left now in the cart |
|
|
|
|
170
|
|
|
|
171
|
|
|
$data = $this->getCurrentShoppingCart(); |
172
|
|
|
$data['message'] = [ |
173
|
|
|
'content' => $this->owner->cart->getMessage(), |
174
|
|
|
'type' => $this->owner->cart->getMessageType() |
175
|
|
|
]; |
176
|
|
|
$this->owner->cart->clearMessage(); |
177
|
|
|
|
178
|
|
|
$this->owner->extend('updateSetQuantityResponseShopJsonResponse', $data, $request, $response, $product, $quantity); |
179
|
|
|
$response->setBody(json_encode($data)); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Clear all items from the cart (Cart Page) |
185
|
|
|
* |
186
|
|
|
* @see 'clear' function of ShoppingCart_Controller ($this->owner) |
187
|
|
|
* @param SS_HTTPRequest $request |
188
|
|
|
* @param SS_HTTPResponse $response |
189
|
|
|
*/ |
190
|
|
|
public function updateClearResponse(&$request, &$response) |
191
|
|
|
{ |
192
|
|
|
if ($request->isAjax()) { |
193
|
|
|
if (!$response) { |
|
|
|
|
194
|
|
|
$response = $this->owner->getResponse(); |
195
|
|
|
} |
196
|
|
|
$response->removeHeader('Content-Type'); |
197
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
198
|
|
|
|
199
|
|
|
$data = $this->getCurrentShoppingCart(); |
200
|
|
|
$data['message'] = [ |
201
|
|
|
'content' => $this->owner->cart->getMessage(), |
202
|
|
|
'type' => $this->owner->cart->getMessageType() |
203
|
|
|
]; |
204
|
|
|
$this->owner->cart->clearMessage(); |
205
|
|
|
|
206
|
|
|
$this->owner->extend('updateClearResponseShopJsonResponse', $data, $request, $response); |
207
|
|
|
$response->setBody(json_encode($data)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Update the variations of a product (Cart Page) |
213
|
|
|
* |
214
|
|
|
* @see 'addtocart' function of VariationForm ($this->owner) |
215
|
|
|
* @param SS_HTTPRequest $request |
216
|
|
|
* @param SS_HTTPResponse $response |
217
|
|
|
* @param Buyable $variation [optional] |
218
|
|
|
* @param int $quantity [optional] |
219
|
|
|
* @param VariationForm $form [optional] |
220
|
|
|
*/ |
221
|
|
|
public function updateVariationFormResponse(&$request, &$response, $variation = null, $quantity = 1, $form = null) |
222
|
|
|
{ |
223
|
|
|
if ($request->isAjax()) { |
224
|
|
|
if (!$response) { |
|
|
|
|
225
|
|
|
$response = $this->owner->getResponse(); |
226
|
|
|
} |
227
|
|
|
$response->removeHeader('Content-Type'); |
228
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
229
|
|
|
$shoppingcart = ShoppingCart::curr(); |
230
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
231
|
|
|
|
232
|
|
|
$data = $this->getCurrentShoppingCart(); |
233
|
|
|
if ($form) { |
234
|
|
|
$data['message'] = [ |
235
|
|
|
'content' => $form->getMessage(), |
236
|
|
|
'type' => $form->getMessageType() |
237
|
|
|
]; |
238
|
|
|
$form->clearMessage(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
$this->owner->extend('updateVariationFormResponseShopJsonResponse', $data, $request, $response, $variation, $quantity, $form); |
243
|
|
|
$response->setBody(json_encode($data)); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Add one of an item to a cart (Product Page) |
249
|
|
|
* |
250
|
|
|
* @see the addtocart function within AddProductForm class |
251
|
|
|
* @param SS_HTTPRequest $request |
252
|
|
|
* @param SS_HTTPResponse $response |
253
|
|
|
* @param Buyable $buyable [optional] |
254
|
|
|
* @param int $quantity [optional] |
255
|
|
|
* @param AddProductForm $form [optional] |
256
|
|
|
*/ |
257
|
|
|
public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) |
258
|
|
|
{ |
259
|
|
|
if ($request->isAjax()) { |
260
|
|
|
if (!$response) { |
|
|
|
|
261
|
|
|
$response = $this->owner->getController()->getResponse(); |
262
|
|
|
} |
263
|
|
|
$response->removeHeader('Content-Type'); |
264
|
|
|
$response->addHeader('Content-Type', 'application/json; charset=utf-8'); |
265
|
|
|
$shoppingcart = ShoppingCart::curr(); |
266
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
267
|
|
|
|
268
|
|
|
$data = $this->getCurrentShoppingCart(); |
269
|
|
|
if ($form) { |
|
|
|
|
270
|
|
|
$data['message'] = [ |
271
|
|
|
'content' => $form->getMessage(), |
272
|
|
|
'type' => $form->getMessageType() |
273
|
|
|
]; |
274
|
|
|
$form->clearMessage(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); |
278
|
|
|
$response->setBody(json_encode($data)); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Provide a copy of the current order in the required format |
284
|
|
|
* Note the id is the cart's id |
285
|
|
|
* @return array of product id, subTotal, grandTotal, and items & modifiers |
286
|
|
|
*/ |
287
|
|
|
public function getCurrentShoppingCart() |
288
|
|
|
{ |
289
|
|
|
$result = []; |
290
|
|
|
|
291
|
|
|
if ($shoppingcart = ShoppingCart::curr()) { |
292
|
|
|
$result['id'] = (string) $shoppingcart->getReference(); |
293
|
|
|
|
294
|
|
|
if ($items = $this->getCurrentShoppingCartItems()) { |
295
|
|
|
$result['items'] = $items; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($modifiers = $this->getCurrentShoppingCartModifiers()) { |
299
|
|
|
$result['modifiers'] = $modifiers; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if ($shoppingcart->SubTotal()) { |
303
|
|
|
$result['subTotal'] = $shoppingcart->SubTotal(); |
304
|
|
|
$result['grandTotal'] = $shoppingcart->GrandTotal(); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
return $result; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Provide a copy of the current order's items, including image details and variations |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
protected function getCurrentShoppingCartItems() |
315
|
|
|
{ |
316
|
|
|
$result = []; |
317
|
|
|
$shoppingcart = ShoppingCart::curr(); |
318
|
|
|
|
319
|
|
|
if ($shoppingcart->Items()->exists()) { |
320
|
|
|
foreach ($shoppingcart->Items()->getIterator() as $item) { |
321
|
|
|
// Definitions |
322
|
|
|
$data = []; |
323
|
|
|
$product = $item->Product(); |
324
|
|
|
|
325
|
|
|
$data["id"] = (string) $item->ProductID; |
326
|
|
|
$data["internalItemID"] = $product->InternalItemID; |
327
|
|
|
$data["title"] = $product->getTitle(); |
328
|
|
|
$data["quantity"] = (int) $item->Quantity; |
329
|
|
|
$data["unitPrice"] = $product->getPrice(); |
330
|
|
|
$data["href"] = $item->Link(); |
331
|
|
|
$data['categories'] = $product->getCategories()->column('Title'); |
332
|
|
|
$data["addLink"] = $item->addLink(); |
333
|
|
|
$data["removeLink"] = $item->removeLink(); |
334
|
|
|
$data["removeallLink"] = $item->removeallLink(); |
335
|
|
|
$data["setquantityLink"] = $item->setquantityLink(); |
336
|
|
|
|
337
|
|
|
// Image |
338
|
|
|
if ($item->Image()) { |
339
|
|
|
$image = $item->Image()->ScaleWidth((int) ProductImageExtension::config()->cart_image_width); |
340
|
|
|
$data["image"] = array( |
341
|
|
|
'alt' => $image->getTitle(), |
342
|
|
|
'src' => $image->getAbsoluteURL(), |
343
|
|
|
'width' => $image->getWidth(), |
344
|
|
|
'height' => $image->getHeight(), |
345
|
|
|
); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Variations |
349
|
|
|
if (Variation::get()->filter('ProductID', $item->ID)->first()) { |
350
|
|
|
$variations = $product->Variations(); |
351
|
|
|
if ($variations->exists()) { |
352
|
|
|
$data['variations'] = []; |
353
|
|
|
foreach ($variations as $variation) { |
354
|
|
|
$data['variations'][] = array( |
355
|
|
|
'id' => (string) $variation->ID, |
356
|
|
|
'title' => $variation->getTitle(), |
357
|
|
|
); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
$result[] = $data; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
return $result; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Provide a copy of the current order's modifiers |
369
|
|
|
* @return array of modifiers (note: this excludes subtotal and grandtotal) |
370
|
|
|
*/ |
371
|
|
|
protected function getCurrentShoppingCartModifiers() |
372
|
|
|
{ |
373
|
|
|
$result = []; |
374
|
|
|
$shoppingcart = ShoppingCart::curr(); |
375
|
|
|
|
376
|
|
|
if ($shoppingcart->Modifiers()->exists()) { |
377
|
|
|
$modifiers = $shoppingcart->Modifiers(); |
378
|
|
|
foreach ($modifiers->sort('Sort')->getIterator() as $modifier) { |
379
|
|
|
if ($modifier->ShowInTable()) { |
380
|
|
|
$data = array( |
381
|
|
|
'id' => (string) $modifier->ID, |
382
|
|
|
'tableTitle' => $modifier->getTableTitle(), |
383
|
|
|
'tableValue' => (float) $modifier->TableValue(), |
384
|
|
|
); |
385
|
|
|
|
386
|
|
|
if (method_exists($modifier, 'Link')) { |
387
|
|
|
// add if there is a link |
388
|
|
|
$data["href"] = $modifier->Link(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (method_exists($modifier, 'removeLink')) { |
392
|
|
|
// add if there is a canRemove method |
393
|
|
|
$data["removeLink"] = $modifier->removeLink(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$result[] = $data; |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
return $result; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|