1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Form; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Model\Setting; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Forms\NumericField; |
9
|
|
|
use SilverStripe\View\Requirements; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class QuantityField |
13
|
|
|
* @package Dynamic\FoxyStripe\Form |
14
|
|
|
*/ |
15
|
|
|
class QuantityField extends NumericField |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private static $allowed_actions = [ |
|
|
|
|
21
|
|
|
'newvalue', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $properties |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function Field($properties = []) |
29
|
|
|
{ |
30
|
|
|
//Requirements::javascript('dynamic/foxystripe: javascript/quantity.js'); |
31
|
|
|
//Requirements::css('dynamic/foxystripe: client/dist/css/quantityfield.css'); |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->setAttribute('data-link', $this->Link('newvalue')); |
35
|
|
|
$this->setAttribute('data-code', $this->getProduct()->Code); |
36
|
|
|
$this->setAttribute('data-id', $this->getProduct()->ID); |
37
|
|
|
|
38
|
|
|
return parent::Field($properties); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return ProductPage |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function getProduct() |
45
|
|
|
{ |
46
|
|
|
return $this->getForm()->getProduct(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param SS_HTTPRequest $request |
|
|
|
|
51
|
|
|
* @return bool|string |
52
|
|
|
*/ |
53
|
|
|
public function newvalue(HTTPRequest $request) |
54
|
|
|
{ |
55
|
|
|
if (!$value = $request->getVar('value')) { |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$code = $request->getVar('code')) { |
60
|
|
|
return ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->extend('updateQuantity', $value); |
64
|
|
|
|
65
|
|
|
$data = array( |
66
|
|
|
'quantity' => $value, |
67
|
|
|
'quantityGenerated' => AddToCartForm::getGeneratedValue($code, 'quantity', $value, 'value'), |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$this->extend('updateData', $data); |
71
|
|
|
return json_encode($data); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|