1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ShoppingCart\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\Validator; |
8
|
|
|
use SilverStripe\Forms\FormAction; |
9
|
|
|
use SilverStripe\Forms\HiddenField; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Forms\RequiredFields; |
12
|
|
|
use SilverStripe\ORM\ValidationResult; |
13
|
|
|
use SilverStripe\Control\RequestHandler; |
14
|
|
|
use SilverCommerce\OrdersAdmin\Model\LineItem; |
15
|
|
|
use SilverCommerce\ShoppingCart\ShoppingCartFactory; |
16
|
|
|
use SilverCommerce\QuantityField\Forms\QuantityField; |
|
|
|
|
17
|
|
|
use SilverCommerce\ShoppingCart\Control\ShoppingCart; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Form dedicated to adding the selected item to cart. |
21
|
|
|
* |
22
|
|
|
* This form is intended to be used |
23
|
|
|
*/ |
24
|
|
|
class AddToCartForm extends Form |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Default form Name property |
28
|
|
|
*/ |
29
|
|
|
const DEFAULT_NAME = "AddToCartForm"; |
30
|
|
|
|
31
|
|
|
public function __construct(RequestHandler $controller = null, $name = self::DEFAULT_NAME, FieldList $fields = null, FieldList $actions = null, Validator $validator = null) |
32
|
|
|
{ |
33
|
|
|
$base_fields = FieldList::create( |
34
|
|
|
HiddenField::create('ID'), |
35
|
|
|
HiddenField::create('ClassName'), |
36
|
|
|
QuantityField::create( |
37
|
|
|
'Quantity', |
38
|
|
|
_t('ShoppingCart.Qty','Qty') |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
if (!empty($fields)) { |
43
|
|
|
$base_fields->merge($fields); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$fields = $base_fields; |
47
|
|
|
|
48
|
|
|
$base_actions = FieldList::create( |
49
|
|
|
FormAction::create( |
50
|
|
|
'doAddItemToCart', |
51
|
|
|
_t('Catalogue.AddToCart','Add to Cart') |
52
|
|
|
)->addExtraClass('btn btn-primary') |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if (!empty($actions)) { |
56
|
|
|
$base_actions->merge($actions); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$actions = $base_actions; |
60
|
|
|
|
61
|
|
|
$base_validator = RequiredFields::create(["Quantity"]); |
62
|
|
|
|
63
|
|
|
if (!empty($validator)) { |
64
|
|
|
$base_validator->appendRequiredFields($validator); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$validator = $base_validator; |
68
|
|
|
|
69
|
|
|
parent::__construct( |
70
|
|
|
$controller, |
71
|
|
|
$name, |
72
|
|
|
$fields, |
73
|
|
|
$actions, |
74
|
|
|
$validator |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->addExtraClass("add-to-cart-form"); |
78
|
|
|
|
79
|
|
|
$this->setTemplate("SilverCommerce\\ShoppingCart\\Forms\\Includes\\AddToCartForm"); |
80
|
|
|
|
81
|
|
|
$this->extend("updateAddToCartForm"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the classname of the product. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getProductClass() |
90
|
|
|
{ |
91
|
|
|
return $this |
92
|
|
|
->Fields() |
93
|
|
|
->dataFieldByName("ClassName") |
94
|
|
|
->getValue(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the classname of the product to add |
99
|
|
|
* |
100
|
|
|
* @param $class Classname of product |
|
|
|
|
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function setProductClass($class) |
104
|
|
|
{ |
105
|
|
|
$this |
106
|
|
|
->Fields() |
107
|
|
|
->dataFieldByName("ClassName") |
108
|
|
|
->setValue($class); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the ID of the product. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getProductID() |
119
|
|
|
{ |
120
|
|
|
return $this |
121
|
|
|
->Fields() |
122
|
|
|
->dataFieldByName("ID") |
123
|
|
|
->getValue(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the ID of the product to add |
128
|
|
|
* |
129
|
|
|
* @param $ID ID of product |
|
|
|
|
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public function setProductID($ID) |
133
|
|
|
{ |
134
|
|
|
$this |
135
|
|
|
->Fields() |
136
|
|
|
->dataFieldByName("ID") |
137
|
|
|
->setValue($ID); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function doAddItemToCart($data) |
143
|
|
|
{ |
144
|
|
|
$classname = $data["ClassName"]; |
145
|
|
|
$id = $data["ID"]; |
146
|
|
|
$cart = ShoppingCartFactory::create(); |
147
|
|
|
$error = false; |
148
|
|
|
$redirect_to_cart = Config::inst() |
149
|
|
|
->get(ShoppingCart::class, "redirect_on_add"); |
150
|
|
|
|
151
|
|
|
if ($object = $classname::get()->byID($id)) { |
152
|
|
|
// Attempt to get tax rate from object |
153
|
|
|
// On a Product this is handleed via casting |
154
|
|
|
// But could be a direct association as well. |
155
|
|
|
$tax_id = $object->TaxID; |
156
|
|
|
|
157
|
|
|
$deliverable = (isset($object->Deliverable)) ? $object->Deliverable : true; |
158
|
|
|
|
159
|
|
|
$item_to_add = LineItem::create([ |
160
|
|
|
"Title" => $object->Title, |
161
|
|
|
"Content" => $object->Content, |
162
|
|
|
"Price" => $object->Price, |
163
|
|
|
"Quantity" => $data['Quantity'], |
164
|
|
|
"StockID" => $object->StockID, |
165
|
|
|
"Weight" => $object->Weight, |
166
|
|
|
"ProductClass" => $object->ClassName, |
167
|
|
|
"Stocked" => $object->Stocked, |
168
|
|
|
"Deliverable" => $deliverable, |
169
|
|
|
"TaxID" => $tax_id, |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
// Try and add item to cart, return any exceptions raised |
173
|
|
|
// as a message |
174
|
|
|
try { |
175
|
|
|
$cart->addItem($item_to_add); |
176
|
|
|
|
177
|
|
|
$message = _t( |
178
|
|
|
'ShoppingCart.AddedItemToCart', |
179
|
|
|
'Added "{item}" to your shopping cart', |
180
|
|
|
["item" => $object->Title] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$this->sessionMessage( |
184
|
|
|
$message, |
185
|
|
|
ValidationResult::TYPE_GOOD |
186
|
|
|
); |
187
|
|
|
} catch(Exception $e) { |
|
|
|
|
188
|
|
|
$error = true; |
189
|
|
|
$this->sessionMessage( |
190
|
|
|
$e->getMessage() |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
} else { |
194
|
|
|
$error = true; |
195
|
|
|
$this->sessionMessage( |
196
|
|
|
_t("ShoppingCart.ErrorAddingToCart", "Error adding item to cart") |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ($redirect_to_cart && !$error) { |
201
|
|
|
return $this |
202
|
|
|
->getController() |
203
|
|
|
->redirect($cart->Link()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this |
207
|
|
|
->getController() |
208
|
|
|
->redirectBack(); |
209
|
|
|
} |
210
|
|
|
} |
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