1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Forms; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use SilverShop\Model\Order; |
7
|
|
|
use SilverShop\Model\Variation\Variation; |
8
|
|
|
use SilverStripe\Forms\CheckboxField; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FormField; |
11
|
|
|
use SilverStripe\Forms\TextField; |
12
|
|
|
use SilverStripe\ORM\ArrayList; |
13
|
|
|
use SilverStripe\ORM\SS_List; |
14
|
|
|
use SilverStripe\View\SSViewer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Field for editing cart/items within a form |
18
|
|
|
* |
19
|
|
|
* @package shop |
20
|
|
|
*/ |
21
|
|
|
class CartEditField extends FormField |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Order |
25
|
|
|
*/ |
26
|
|
|
protected $cart; |
27
|
|
|
|
28
|
|
|
protected $items; |
29
|
|
|
|
30
|
|
|
protected $template = 'Cart'; |
31
|
|
|
|
32
|
|
|
protected $editableItemsCallback; |
33
|
|
|
|
34
|
|
|
public function __construct($name, $title, $cart) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($name, $title); |
37
|
|
|
$this->cart = $cart; |
38
|
|
|
$this->items = $cart->Items(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set tempalte for rendering editable cart. |
43
|
|
|
* |
44
|
|
|
* @param string $template |
45
|
|
|
*/ |
46
|
|
|
public function setTemplate($template) |
47
|
|
|
{ |
48
|
|
|
$this->template = $template; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Allow overriding the given items list. |
54
|
|
|
* This helps with formatting, grouping, ordering etc. |
55
|
|
|
*/ |
56
|
|
|
public function setItemsList(SS_List $list) |
57
|
|
|
{ |
58
|
|
|
$this->items = $list; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the items list being used to produce the cart. |
64
|
|
|
* |
65
|
|
|
* @return SS_List |
66
|
|
|
*/ |
67
|
|
|
public function getItemsList() |
68
|
|
|
{ |
69
|
|
|
return $this->items; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Provides a way to modify the editableItems list |
74
|
|
|
* before it is rendered. |
75
|
|
|
* |
76
|
|
|
* @param Closure $callback |
77
|
|
|
*/ |
78
|
|
|
public function setEditableItemsCallback(Closure $callback) |
79
|
|
|
{ |
80
|
|
|
$this->editableItemsCallback = $callback; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Render the cart with editable item fields. |
85
|
|
|
* |
86
|
|
|
* @param array $properties |
87
|
|
|
*/ |
88
|
|
|
public function Field($properties = []) |
89
|
|
|
{ |
90
|
|
|
$editables = $this->editableItems(); |
91
|
|
|
$customcartdata = [ |
92
|
|
|
'Items' => $editables, |
93
|
|
|
]; |
94
|
|
|
// NOTE: this was originally incorrect - passing just $editables and $customcartdata |
95
|
|
|
// which broke modules like Display_Logic. |
96
|
|
|
$this->extend('onBeforeRender', $this, $editables, $customcartdata); |
97
|
|
|
|
98
|
|
|
return SSViewer::execute_template( |
99
|
|
|
$this->template, |
100
|
|
|
$this->cart->customise($customcartdata), |
101
|
|
|
['Editable' => true] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add quantity, variation and remove fields to the |
107
|
|
|
* item set. |
108
|
|
|
* |
109
|
|
|
* @param SS_List $items |
110
|
|
|
*/ |
111
|
|
|
protected function editableItems() |
112
|
|
|
{ |
113
|
|
|
$editables = ArrayList::create(); |
114
|
|
|
foreach ($this->items as $item) { |
115
|
|
|
$buyable = $item->Buyable(); |
116
|
|
|
if (!$buyable) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
// If the buyable is a variation, use the belonging product instead for variation-form generation |
120
|
|
|
if ($buyable instanceof Variation) { |
121
|
|
|
$buyable = $buyable->Product(); |
122
|
|
|
} |
123
|
|
|
$name = $this->name . "[$item->ID]"; |
124
|
|
|
$quantity = TextField::create( |
125
|
|
|
$name . '[Quantity]', |
126
|
|
|
'Quantity', |
127
|
|
|
$item->Quantity |
128
|
|
|
) |
129
|
|
|
->addExtraClass('numeric') |
130
|
|
|
->setAttribute('type', 'number') |
131
|
|
|
->setAttribute('min', '0'); |
132
|
|
|
|
133
|
|
|
$variationfield = false; |
134
|
|
|
if ($buyable->hasMany('Variations')) { |
135
|
|
|
$variations = $buyable->Variations(); |
136
|
|
|
if ($variations->exists()) { |
137
|
|
|
$variationfield = DropdownField::create( |
138
|
|
|
$name . '[ProductVariationID]', |
139
|
|
|
_t('SilverShop\Model\Variation\Variation.SINGULARNAME', 'Variation'), |
140
|
|
|
$variations->map('ID', 'Title'), |
141
|
|
|
$item->ProductVariationID |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
$remove = CheckboxField::create($name . '[Remove]', _t('SilverShop\Generic.Remove', 'Remove')); |
146
|
|
|
$editables->push( |
147
|
|
|
$item->customise( |
148
|
|
|
[ |
149
|
|
|
'QuantityField' => $quantity, |
150
|
|
|
'VariationField' => $variationfield, |
151
|
|
|
'RemoveField' => $remove, |
152
|
|
|
] |
153
|
|
|
) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (is_callable($this->editableItemsCallback)) { |
158
|
|
|
$callback = $this->editableItemsCallback; |
159
|
|
|
$editables = $callback($editables); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $editables; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|