Skip to content

Commit 9c9bc08

Browse files
committed
feat: add create order api
1 parent 5dce39b commit 9c9bc08

File tree

2 files changed

+228
-37
lines changed

2 files changed

+228
-37
lines changed

Model/Api/Resolver/Store/Checkout.php

Lines changed: 227 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Magento\Framework\UrlInterface;
88
use Magento\Shipping\Model\Config;
99
use Vuefront\Vuefront\Model\Api\System\Engine\Resolver;
10+
use Magento\Store\Model\ScopeInterface;
1011

1112
class Checkout extends Resolver
1213
{
@@ -41,14 +42,68 @@ class Checkout extends Resolver
4142
*/
4243
protected $shippingInformation;
4344

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+
4490
public function __construct(
4591
Config $shippingModelConfig,
4692
UrlInterface $url,
4793
Session $session,
4894
Data $currencyHelper,
4995
\Magento\Quote\Api\Data\AddressInterface $address,
5096
\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
52107
) {
53108
$this->_currencyHelper = $currencyHelper;
54109
$this->url = $url;
@@ -57,6 +112,15 @@ public function __construct(
57112
$this->address = $address;
58113
$this->shippingInformationManagement = $shippingInformationManagement;
59114
$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;
60124
}
61125

62126
public function link()
@@ -130,6 +194,12 @@ public function paymentAddress()
130194
'required' => true
131195
];
132196

197+
$fields[] = [
198+
'type' => 'text',
199+
'name' => 'email',
200+
'required' => true
201+
];
202+
133203
$fields[] = [
134204
'type' => 'text',
135205
'name' => 'company',
@@ -169,7 +239,11 @@ public function paymentAddress()
169239
$fields[] = [
170240
'type' => 'country',
171241
'name' => 'country_id',
172-
'required' => true
242+
'required' => true,
243+
'defaultValue' => $this->scopeConfig->getValue(
244+
'general/country/default',
245+
ScopeInterface::SCOPE_WEBSITES
246+
)
173247
];
174248

175249
$agree = null;
@@ -195,6 +269,12 @@ public function shippingAddress()
195269
'required' => true
196270
];
197271

272+
$fields[] = [
273+
'type' => 'text',
274+
'name' => 'email',
275+
'required' => true
276+
];
277+
198278
$fields[] = [
199279
'type' => 'text',
200280
'name' => 'company',
@@ -234,7 +314,11 @@ public function shippingAddress()
234314
$fields[] = [
235315
'type' => 'country',
236316
'name' => 'country_id',
237-
'required' => true
317+
'required' => true,
318+
'defaultValue' => $this->scopeConfig->getValue(
319+
'general/country/default',
320+
ScopeInterface::SCOPE_WEBSITES
321+
)
238322
];
239323

240324
$fields[] = [
@@ -249,49 +333,65 @@ public function shippingAddress()
249333
public function createOrder($args)
250334
{
251335
$this->checkoutSession->regenerateId();
252-
// $this->session->data['shipping_address'] = array();
253336

254-
// foreach ($this->shippingAddress() as $value) {
255-
// $this->session->data['shipping_address'][$value['name']] = '';
256-
// }
337+
$paymentAddress = [];
257338

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+
}
261346

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('');
266363

267-
// $this->session->data['payment_method'] = null;
268-
// $this->session->data['shipping_method'] = null;
269364
return ['success' => 'success'];
270365
}
271366

272367
public function updateOrder($args)
273368
{
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);
290386

291387
$shippingAddress = [];
292388

293389
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+
}
295395
}
296396

297397
foreach ($args['shippingAddress'] as $value) {
@@ -300,6 +400,8 @@ public function updateOrder($args)
300400
}
301401
}
302402

403+
$this->_sessionManager->setShippingAddress($shippingAddress);
404+
303405
$shipping_address = $this->address
304406
->setFirstname($shippingAddress['firstName'])
305407
->setLastname($shippingAddress['lastName'])
@@ -316,8 +418,6 @@ public function updateOrder($args)
316418
$shipping_information = $this->shippingInformation->setShippingAddress($shipping_address);
317419

318420
if (!empty($args['shippingMethod'])) {
319-
320-
321421
$methodInfo = explode('.', $args['shippingMethod']);
322422

323423
$shipping_information = $shipping_information->setShippingCarrierCode($methodInfo[0])
@@ -335,7 +435,24 @@ public function updateOrder($args)
335435
}
336436
}
337437

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+
339456
$that = $this;
340457
return [
341458
'paymentMethods' => function ($root, $args) use ($that) {
@@ -370,10 +487,83 @@ public function getCartItemsSkus() {
370487

371488
public function confirmOrder()
372489
{
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+
373563
return [
374564
'url' => '1',
375565
'order' => [
376-
'id' => '1'
566+
'id' => $orderId
377567
]
378568
];
379569
}

etc/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ type AddressField {
361361
type: String
362362
name: String
363363
label: String
364+
defaultValue: String
364365
required: Boolean
365366
values: [FieldValue]
366367
}

0 commit comments

Comments
 (0)