@@ -26,16 +26,37 @@ class Checkout extends Resolver
26
26
*/
27
27
protected $ checkoutSession ;
28
28
29
+ /**
30
+ * @var \Magento\Quote\Api\Data\AddressInterface
31
+ */
32
+ protected $ address ;
33
+
34
+ /**
35
+ * @var \Magento\Checkout\Api\ShippingInformationManagementInterface
36
+ */
37
+ protected $ shippingInformationManagement ;
38
+
39
+ /**
40
+ * @var \Magento\Checkout\Api\Data\ShippingInformationInterface
41
+ */
42
+ protected $ shippingInformation ;
43
+
29
44
public function __construct (
30
45
Config $ shippingModelConfig ,
31
46
UrlInterface $ url ,
32
47
Session $ session ,
33
- Data $ currencyHelper
48
+ Data $ currencyHelper ,
49
+ \Magento \Quote \Api \Data \AddressInterface $ address ,
50
+ \Magento \Checkout \Api \Data \ShippingInformationInterface $ shippingInformation ,
51
+ \Magento \Checkout \Api \ShippingInformationManagementInterface $ shippingInformationManagement
34
52
) {
35
53
$ this ->_currencyHelper = $ currencyHelper ;
36
54
$ this ->url = $ url ;
37
55
$ this ->_shippingModelConfig = $ shippingModelConfig ;
38
56
$ this ->checkoutSession = $ session ;
57
+ $ this ->address = $ address ;
58
+ $ this ->shippingInformationManagement = $ shippingInformationManagement ;
59
+ $ this ->shippingInformation = $ shippingInformation ;
39
60
}
40
61
41
62
public function link ()
@@ -85,8 +106,8 @@ public function shippingMethods($args)
85
106
foreach ($ carriers as $ shippingCode => $ shippingModel ) {
86
107
$ shippingPrice = $ this ->_currencyHelper ->currency ($ shippingModel ->getConfigData ('price ' ), true , false );
87
108
$ result [] = [
88
- 'id ' => $ shippingModel ->getCarrierCode (),
89
- 'codename ' => $ shippingModel ->getCarrierCode (),
109
+ 'id ' => $ shippingModel ->getCarrierCode () . ' . ' . $ shippingCode ,
110
+ 'codename ' => $ shippingModel ->getCarrierCode () . ' . ' . $ shippingCode ,
90
111
"name " => $ shippingModel ->getConfigData ('title ' ) . ' - ' . $ shippingPrice
91
112
];
92
113
}
@@ -109,12 +130,6 @@ public function paymentAddress()
109
130
'required ' => true
110
131
];
111
132
112
- $ fields [] = [
113
- 'type ' => 'text ' ,
114
- 'name ' => 'email ' ,
115
- 'required ' => true
116
- ];
117
-
118
133
$ fields [] = [
119
134
'type ' => 'text ' ,
120
135
'name ' => 'company ' ,
@@ -130,11 +145,21 @@ public function paymentAddress()
130
145
'name ' => 'address2 ' ,
131
146
'required ' => false
132
147
];
148
+ $ fields [] = [
149
+ 'type ' => 'text ' ,
150
+ 'name ' => 'address3 ' ,
151
+ 'required ' => false
152
+ ];
133
153
$ fields [] = [
134
154
'type ' => 'text ' ,
135
155
'name ' => 'city ' ,
136
156
'required ' => true
137
157
];
158
+ $ fields [] = [
159
+ 'type ' => 'zone ' ,
160
+ 'name ' => 'zone_id ' ,
161
+ 'required ' => true
162
+ ];
138
163
$ fields [] = [
139
164
'type ' => 'text ' ,
140
165
'name ' => 'postcode ' ,
@@ -147,12 +172,6 @@ public function paymentAddress()
147
172
'required ' => true
148
173
];
149
174
150
- $ fields [] = [
151
- 'type ' => 'zone ' ,
152
- 'name ' => 'zone_id ' ,
153
- 'required ' => true
154
- ];
155
-
156
175
$ agree = null ;
157
176
158
177
return [
@@ -218,11 +237,18 @@ public function shippingAddress()
218
237
'required ' => true
219
238
];
220
239
240
+ $ fields [] = [
241
+ 'type ' => 'phone ' ,
242
+ 'name ' => 'phone ' ,
243
+ 'required ' => true
244
+ ];
245
+
221
246
return $ fields ;
222
247
}
223
248
224
249
public function createOrder ($ args )
225
250
{
251
+ $ this ->checkoutSession ->regenerateId ();
226
252
// $this->session->data['shipping_address'] = array();
227
253
228
254
// foreach ($this->shippingAddress() as $value) {
@@ -262,33 +288,52 @@ public function updateOrder($args)
262
288
// }
263
289
// }
264
290
265
- // foreach ($args['shippingAddress'] as $value) {
266
- // if (strpos($value['name'], "vfCustomField-") !== false) {
267
- // if ($value['value']) {
268
- // $field_name = str_replace("vfCustomField-", "", $value['name']);
269
- // $field_name = explode('-', $field_name);
270
- // if (!isset($this->session->data['shipping_address']['custom_field'][$field_name[0]])) {
271
- // $this->session->data['shipping_address']['custom_field'][$field_name[0]] = array();
272
- // }
273
- // $this->session->data['shipping_address']['custom_field'][$field_name[0]][$field_name[1]] = $value['value'];
274
- // }
275
- // } else {
276
- // if ($value['value']) {
277
- // $this->session->data['shipping_address'][$value['name']] = $value['value'];
278
- // }
279
- // }
280
- // }
291
+ $ shippingAddress = [];
281
292
282
- // if (!empty($args['shippingMethod'])) {
283
- // $shipping = explode('.', $args['shippingMethod']);
293
+ foreach ($ this ->shippingAddress () as $ value ) {
294
+ $ shippingAddress [$ value ['name ' ]] = '' ;
295
+ }
284
296
285
- // $this->load->model('extension/shipping/'.$shipping[0]);
297
+ foreach ($ args ['shippingAddress ' ] as $ value ) {
298
+ if ($ value ['value ' ]) {
299
+ $ shippingAddress [$ value ['name ' ]] = $ value ['value ' ];
300
+ }
301
+ }
286
302
287
- // $quote = $this->{'model_extension_shipping_' . $shipping[0]}->getQuote($this->session->data['shipping_address']);
288
- // if ($quote) {
289
- // $this->session->data['shipping_method'] = $quote['quote'][$shipping[1]];
290
- // }
291
- // }
303
+ $ shipping_address = $ this ->address
304
+ ->setFirstname ($ shippingAddress ['firstName ' ])
305
+ ->setLastname ($ shippingAddress ['lastName ' ])
306
+ ->setStreet ($ shippingAddress ['address1 ' ])
307
+ ->setCity ($ shippingAddress ['city ' ])
308
+ ->setCountryId ($ shippingAddress ['country_id ' ])
309
+ ->setRegionId ($ shippingAddress ['zone_id ' ])
310
+ // ->setRegion($region)
311
+ ->setPostcode ($ shippingAddress ['postcode ' ])
312
+ ->setTelephone ($ shippingAddress ['phone ' ])
313
+ ->setSaveInAddressBook (0 )
314
+ ->setSameAsBilling (0 );
315
+
316
+ $ shipping_information = $ this ->shippingInformation ->setShippingAddress ($ shipping_address );
317
+
318
+ if (!empty ($ args ['shippingMethod ' ])) {
319
+
320
+
321
+ $ methodInfo = explode ('. ' , $ args ['shippingMethod ' ]);
322
+
323
+ $ shipping_information = $ shipping_information ->setShippingCarrierCode ($ methodInfo [0 ])
324
+ ->setShippingMethodCode ($ methodInfo [1 ]);
325
+
326
+ if ($ this ->checkoutSession ->getQuote ()) {
327
+ $ cartId = $ this ->checkoutSession ->getQuote ()->getId ();
328
+ $ cartSkuArray = $ this ->getCartItemsSkus ();
329
+ if ($ cartSkuArray ) {
330
+ try {
331
+ $ this ->shippingInformationManagement ->saveAddressInformation ($ cartId , $ shipping_information );
332
+ } catch (\Exception $ e ) {
333
+ }
334
+ }
335
+ }
336
+ }
292
337
293
338
// $this->session->data['payment_method'] = $args['paymentMethod'];
294
339
$ that = $ this ;
@@ -314,6 +359,15 @@ public function updateOrder($args)
314
359
];
315
360
}
316
361
362
+ public function getCartItemsSkus () {
363
+ $ cartSkuArray = [];
364
+ $ cartItems = $ this ->checkoutSession ->getQuote ()->getAllVisibleItems ();
365
+ foreach ($ cartItems as $ product ) {
366
+ $ cartSkuArray [] = $ product ->getSku ();
367
+ }
368
+ return $ cartSkuArray ;
369
+ }
370
+
317
371
public function confirmOrder ()
318
372
{
319
373
return [
@@ -326,6 +380,17 @@ public function confirmOrder()
326
380
327
381
public function totals ()
328
382
{
329
- return [];
383
+ $ totals = $ this ->checkoutSession ->getQuote ()->getTotals ();
384
+
385
+ $ result = [];
386
+
387
+ foreach ($ totals as $ key => $ value ) {
388
+ $ result [] = [
389
+ "title " => $ value ->toArray ()['title ' ],
390
+ "text " => $ this ->_currencyHelper ->currency ($ value ->toArray ()['value ' ], true , false )
391
+ ];
392
+ }
393
+
394
+ return $ result ;
330
395
}
331
396
}
0 commit comments