7
7
use Magento \Framework \UrlInterface ;
8
8
use Magento \Shipping \Model \Config ;
9
9
use Vuefront \Vuefront \Model \Api \System \Engine \Resolver ;
10
+ use Magento \Store \Model \ScopeInterface ;
10
11
11
12
class Checkout extends Resolver
12
13
{
@@ -41,14 +42,68 @@ class Checkout extends Resolver
41
42
*/
42
43
protected $ shippingInformation ;
43
44
45
+ /**
46
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
47
+ */
48
+ protected $ scopeConfig ;
49
+
50
+ /**
51
+ * @var \Magento\Framework\Session\SessionManagerInterface
52
+ */
53
+ protected $ _sessionManager ;
54
+
55
+ /**
56
+ * @var \Magento\Store\Model\StoreManagerInterface
57
+ */
58
+ protected $ storeManager ;
59
+
60
+ /**
61
+ * @var \Magento\Customer\Model\CustomerFactory
62
+ */
63
+ protected $ customerFactory ;
64
+
65
+ /**
66
+ * @var \Magento\Catalog\Api\ProductRepositoryInterface
67
+ */
68
+ protected $ productRepository ;
69
+
70
+ /**
71
+ * @var \Magento\Customer\Api\CustomerRepositoryInterface
72
+ */
73
+ protected $ customerRepository ;
74
+
75
+ /**
76
+ * @var \Magento\Quote\Model\QuoteManagement
77
+ */
78
+ protected $ quoteManagement ;
79
+
80
+ /**
81
+ * @var \Magento\Sales\Model\Order\Email\Sender\OrderSender
82
+ */
83
+ protected $ orderSender ;
84
+
85
+ /**
86
+ * @var \Magento\Checkout\Model\Cart
87
+ */
88
+ protected $ _cartModel ;
89
+
44
90
public function __construct (
45
91
Config $ shippingModelConfig ,
46
92
UrlInterface $ url ,
47
93
Session $ session ,
48
94
Data $ currencyHelper ,
49
95
\Magento \Quote \Api \Data \AddressInterface $ address ,
50
96
\Magento \Checkout \Api \Data \ShippingInformationInterface $ shippingInformation ,
51
- \Magento \Checkout \Api \ShippingInformationManagementInterface $ shippingInformationManagement
97
+ \Magento \Checkout \Api \ShippingInformationManagementInterface $ shippingInformationManagement ,
98
+ \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
99
+ \Magento \Framework \Session \SessionManagerInterface $ sessionManager ,
100
+ \Magento \Store \Model \StoreManagerInterface $ storeManager ,
101
+ \Magento \Customer \Model \CustomerFactory $ customerFactory ,
102
+ \Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
103
+ \Magento \Customer \Api \CustomerRepositoryInterface $ customerRepository ,
104
+ \Magento \Quote \Model \QuoteManagement $ quoteManagement ,
105
+ \Magento \Sales \Model \Order \Email \Sender \OrderSender $ orderSender ,
106
+ \Magento \Checkout \Model \Cart $ cartModel
52
107
) {
53
108
$ this ->_currencyHelper = $ currencyHelper ;
54
109
$ this ->url = $ url ;
@@ -57,6 +112,15 @@ public function __construct(
57
112
$ this ->address = $ address ;
58
113
$ this ->shippingInformationManagement = $ shippingInformationManagement ;
59
114
$ this ->shippingInformation = $ shippingInformation ;
115
+ $ this ->scopeConfig = $ scopeConfig ;
116
+ $ this ->_sessionManager = $ sessionManager ;
117
+ $ this ->storeManager = $ storeManager ;
118
+ $ this ->customerFactory = $ customerFactory ;
119
+ $ this ->productRepository = $ productRepository ;
120
+ $ this ->customerRepository = $ customerRepository ;
121
+ $ this ->quoteManagement = $ quoteManagement ;
122
+ $ this ->orderSender = $ orderSender ;
123
+ $ this ->_cartModel = $ cartModel ;
60
124
}
61
125
62
126
public function link ()
@@ -130,6 +194,12 @@ public function paymentAddress()
130
194
'required ' => true
131
195
];
132
196
197
+ $ fields [] = [
198
+ 'type ' => 'text ' ,
199
+ 'name ' => 'email ' ,
200
+ 'required ' => true
201
+ ];
202
+
133
203
$ fields [] = [
134
204
'type ' => 'text ' ,
135
205
'name ' => 'company ' ,
@@ -169,7 +239,11 @@ public function paymentAddress()
169
239
$ fields [] = [
170
240
'type ' => 'country ' ,
171
241
'name ' => 'country_id ' ,
172
- 'required ' => true
242
+ 'required ' => true ,
243
+ 'defaultValue ' => $ this ->scopeConfig ->getValue (
244
+ 'general/country/default ' ,
245
+ ScopeInterface::SCOPE_WEBSITES
246
+ )
173
247
];
174
248
175
249
$ agree = null ;
@@ -195,6 +269,12 @@ public function shippingAddress()
195
269
'required ' => true
196
270
];
197
271
272
+ $ fields [] = [
273
+ 'type ' => 'text ' ,
274
+ 'name ' => 'email ' ,
275
+ 'required ' => true
276
+ ];
277
+
198
278
$ fields [] = [
199
279
'type ' => 'text ' ,
200
280
'name ' => 'company ' ,
@@ -234,7 +314,11 @@ public function shippingAddress()
234
314
$ fields [] = [
235
315
'type ' => 'country ' ,
236
316
'name ' => 'country_id ' ,
237
- 'required ' => true
317
+ 'required ' => true ,
318
+ 'defaultValue ' => $ this ->scopeConfig ->getValue (
319
+ 'general/country/default ' ,
320
+ ScopeInterface::SCOPE_WEBSITES
321
+ )
238
322
];
239
323
240
324
$ fields [] = [
@@ -249,49 +333,65 @@ public function shippingAddress()
249
333
public function createOrder ($ args )
250
334
{
251
335
$ this ->checkoutSession ->regenerateId ();
252
- // $this->session->data['shipping_address'] = array();
253
336
254
- // foreach ($this->shippingAddress() as $value) {
255
- // $this->session->data['shipping_address'][$value['name']] = '';
256
- // }
337
+ $ paymentAddress = [];
257
338
258
- // $this->session->data['payment_address'] = array(
259
- // 'custom_field' => array()
260
- // );
339
+ foreach ($ this ->paymentAddress ()['fields ' ] as $ value ) {
340
+ if (isset ($ value ['defaultValue ' ])) {
341
+ $ paymentAddress [$ value ['name ' ]] = $ value ['defaultValue ' ];
342
+ } else {
343
+ $ paymentAddress [$ value ['name ' ]] = '' ;
344
+ }
345
+ }
261
346
262
- // $paymentAddress = $this->paymentAddress();
263
- // foreach ($paymentAddress['fields'] as $value) {
264
- // $this->session->data['payment_address'][$value['name']] = '';
265
- // }
347
+ $ this ->_sessionManager ->setPaymentAddress ($ paymentAddress );
348
+
349
+ $ shippingAddress = [];
350
+
351
+ foreach ($ this ->shippingAddress () as $ value ) {
352
+ if (isset ($ value ['defaultValue ' ])) {
353
+ $ shippingAddress [$ value ['name ' ]] = $ value ['defaultValue ' ];
354
+ } else {
355
+ $ shippingAddress [$ value ['name ' ]] = '' ;
356
+ }
357
+ }
358
+
359
+ $ this ->_sessionManager ->setShippingAddress ($ shippingAddress );
360
+
361
+ $ this ->_sessionManager ->setPaymentMethod ('' );
362
+ $ this ->_sessionManager ->setShippingMethod ('' );
266
363
267
- // $this->session->data['payment_method'] = null;
268
- // $this->session->data['shipping_method'] = null;
269
364
return ['success ' => 'success ' ];
270
365
}
271
366
272
367
public function updateOrder ($ args )
273
368
{
274
- // foreach ($args['paymentAddress'] as $value) {
275
- // if (strpos($value['name'], "vfCustomField-") !== false) {
276
- // if ($value['value']) {
277
- // $field_name = str_replace("vfCustomField-", "", $value['name']);
278
- // $field_name = explode('-', $field_name);
279
- // if (!isset($this->session->data['payment_address']['custom_field'][$field_name[0]])) {
280
- // $this->session->data['payment_address']['custom_field'][$field_name[0]] = array();
281
- // }
282
- // $this->session->data['payment_address']['custom_field'][$field_name[0]][$field_name[1]] = $value['value'];
283
- // }
284
- // } else {
285
- // if ($value['value']) {
286
- // $this->session->data['payment_address'][$value['name']] = $value['value'];
287
- // }
288
- // }
289
- // }
369
+ $ paymentAddress = [];
370
+
371
+ foreach ($ this ->paymentAddress ()['fields ' ] as $ value ) {
372
+ if (isset ($ value ['defaultValue ' ])) {
373
+ $ paymentAddress [$ value ['name ' ]] = $ value ['defaultValue ' ];
374
+ } else {
375
+ $ paymentAddress [$ value ['name ' ]] = '' ;
376
+ }
377
+ }
378
+
379
+ foreach ($ args ['paymentAddress ' ] as $ value ) {
380
+ if ($ value ['value ' ]) {
381
+ $ paymentAddress [$ value ['name ' ]] = $ value ['value ' ];
382
+ }
383
+ }
384
+
385
+ $ this ->_sessionManager ->setPaymentAddress ($ paymentAddress );
290
386
291
387
$ shippingAddress = [];
292
388
293
389
foreach ($ this ->shippingAddress () as $ value ) {
294
- $ shippingAddress [$ value ['name ' ]] = '' ;
390
+ if (isset ($ value ['defaultValue ' ])) {
391
+ $ shippingAddress [$ value ['name ' ]] = $ value ['defaultValue ' ];
392
+ } else {
393
+ $ shippingAddress [$ value ['name ' ]] = '' ;
394
+ }
295
395
}
296
396
297
397
foreach ($ args ['shippingAddress ' ] as $ value ) {
@@ -300,6 +400,8 @@ public function updateOrder($args)
300
400
}
301
401
}
302
402
403
+ $ this ->_sessionManager ->setShippingAddress ($ shippingAddress );
404
+
303
405
$ shipping_address = $ this ->address
304
406
->setFirstname ($ shippingAddress ['firstName ' ])
305
407
->setLastname ($ shippingAddress ['lastName ' ])
@@ -316,8 +418,6 @@ public function updateOrder($args)
316
418
$ shipping_information = $ this ->shippingInformation ->setShippingAddress ($ shipping_address );
317
419
318
420
if (!empty ($ args ['shippingMethod ' ])) {
319
-
320
-
321
421
$ methodInfo = explode ('. ' , $ args ['shippingMethod ' ]);
322
422
323
423
$ shipping_information = $ shipping_information ->setShippingCarrierCode ($ methodInfo [0 ])
@@ -335,7 +435,24 @@ public function updateOrder($args)
335
435
}
336
436
}
337
437
338
- // $this->session->data['payment_method'] = $args['paymentMethod'];
438
+ $ payment_address = $ this ->address
439
+ ->setFirstname ($ paymentAddress ['firstName ' ])
440
+ ->setLastname ($ paymentAddress ['lastName ' ])
441
+ ->setStreet ($ paymentAddress ['address1 ' ])
442
+ ->setCity ($ paymentAddress ['city ' ])
443
+ ->setCountryId ($ paymentAddress ['country_id ' ])
444
+ ->setRegionId ($ paymentAddress ['zone_id ' ])
445
+ // ->setRegion($region)
446
+ ->setPostcode ($ paymentAddress ['postcode ' ])
447
+ ->setSaveInAddressBook (0 )
448
+ ->setSameAsBilling (0 );
449
+
450
+ $ this ->checkoutSession ->getQuote ()->setBillingAddress ($ payment_address );
451
+ $ this ->checkoutSession ->getQuote ()->setShippingAddress ($ shipping_address );
452
+
453
+ $ this ->_sessionManager ->setPaymentMethod ($ args ['paymentMethod ' ]);
454
+ $ this ->_sessionManager ->setShippingMethod ($ args ['shippingMethod ' ]);
455
+
339
456
$ that = $ this ;
340
457
return [
341
458
'paymentMethods ' => function ($ root , $ args ) use ($ that ) {
@@ -370,10 +487,83 @@ public function getCartItemsSkus() {
370
487
371
488
public function confirmOrder ()
372
489
{
490
+
491
+ $ store = $ this ->storeManager ->getStore ();
492
+ $ storeId = $ store ->getStoreId ();
493
+
494
+ $ websiteId = $ this ->storeManager ->getStore ()->getWebsiteId ();
495
+
496
+ $ shippingAddress = $ this ->_sessionManager ->getShippingAddress ();
497
+ $ paymentAddress = $ this ->_sessionManager ->getPaymentAddress ();
498
+
499
+ $ shippingMethod = $ this ->_sessionManager ->getShippingMethod ();
500
+ $ paymentMethod = $ this ->_sessionManager ->getPaymentMethod ();
501
+
502
+ $ customer = $ this ->customerFactory ->create ();
503
+ $ customer ->setWebsiteId ($ websiteId );
504
+ $ customer ->loadByEmail ($ shippingAddress ['email ' ]);// load customet by email address
505
+ if (!$ customer ->getId ()){
506
+ //For guest customer create new cusotmer
507
+ $ customer ->setWebsiteId ($ websiteId )
508
+ ->setStore ($ store )
509
+ ->setFirstname ($ shippingAddress ['firstName ' ])
510
+ ->setLastname ($ shippingAddress ['lastName ' ])
511
+ ->setEmail ($ shippingAddress ['email ' ])
512
+ ->setPassword ($ shippingAddress ['email ' ]);
513
+ $ customer ->save ();
514
+ }
515
+
516
+ $ quote =$ this ->_cartModel ->getQuote ();//$this->quote->create(); //Create object of quote
517
+ $ quote ->setStore ($ store ); //set store for our quote
518
+ /* for registered customer */
519
+
520
+ $ customer = $ this ->customerRepository ->getById ($ customer ->getId ());
521
+ $ quote ->setCurrency ();
522
+ $ quote ->assignCustomer ($ customer ); //Assign quote to customer
523
+
524
+ //add items in quote
525
+
526
+ // $results = $this->_cartModel->getItems();
527
+
528
+ // foreach ($results as $value) {
529
+ // /** @var \Magento\Catalog\Model\Product $product */
530
+ // $product = $value->getProduct();
531
+ // $quote->addProduct($product,$value->getQty());
532
+ // }
533
+
534
+ //Set Billing and shipping Address to quote
535
+ $ quote ->setBillingAddress ($ this ->checkoutSession ->getQuote ()->getBillingAddress ());
536
+ $ quote ->setShippingAddress ($ this ->checkoutSession ->getQuote ()->getShippingAddress ());
537
+
538
+ $ methodInfo = explode ('. ' , $ shippingMethod );
539
+
540
+ // set shipping method
541
+ $ shippingAddress =$ quote ->getShippingAddress ();
542
+ $ shippingAddress ->setCollectShippingRates (true )
543
+ ->collectShippingRates ()
544
+ ->setShippingMethod ($ methodInfo [1 ])->setShippingCarrierCode ($ methodInfo [0 ]); //shipping method, please verify flat rate shipping must be enable
545
+ // $quote->setPaymentMethod($paymentMethod); //payment method, please verify checkmo must be enable from admin
546
+ $ quote ->setInventoryProcessed (false ); //decrease item stock equal to qty
547
+ $ quote ->save (); //quote save
548
+ // Set Sales Order Payment, We have taken check/money order
549
+ // $quote->getPayment()->importData(['method' => $paymentMethod]);
550
+
551
+ // Collect Quote Totals & Save
552
+ $ quote ->collectTotals ()->save ();
553
+ // Create Order From Quote Object
554
+ $ order = $ this ->quoteManagement ->submit ($ quote );
555
+
556
+ /* for send order email to customer email id */
557
+ $ this ->orderSender ->send ($ order );
558
+
559
+ /* get order real id from order */
560
+ $ orderId = $ order ->getIncrementId ();
561
+
562
+
373
563
return [
374
564
'url ' => '1 ' ,
375
565
'order ' => [
376
- 'id ' => ' 1 '
566
+ 'id ' => $ orderId
377
567
]
378
568
];
379
569
}
0 commit comments