Skip to content

Commit f5d44ca

Browse files
committed
feat: add create order
1 parent 5a605f3 commit f5d44ca

File tree

2 files changed

+43
-84
lines changed

2 files changed

+43
-84
lines changed

Model/Api/Resolver/Store/Cart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function add($args)
6161
'super_attribute' => $super_attributes,
6262
'links' => $links
6363
];
64+
6465
$product_info = $this->_productRepository->getById($args['id']);
6566

6667
try {

Model/Api/Resolver/Store/Checkout.php

Lines changed: 42 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class Checkout extends Resolver
8282
*/
8383
protected $orderSender;
8484

85+
/**
86+
* @var \Magento\Quote\Model\Quote\PaymentFactory
87+
*/
88+
protected $paymentFactory;
89+
8590
public function __construct(
8691
Config $shippingModelConfig,
8792
UrlInterface $url,
@@ -97,7 +102,8 @@ public function __construct(
97102
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
98103
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
99104
\Magento\Quote\Model\QuoteManagement $quoteManagement,
100-
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
105+
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
106+
\Magento\Quote\Model\Quote\PaymentFactory $paymentFactory
101107
) {
102108
$this->_currencyHelper = $currencyHelper;
103109
$this->url = $url;
@@ -114,6 +120,7 @@ public function __construct(
114120
$this->customerRepository = $customerRepository;
115121
$this->quoteManagement = $quoteManagement;
116122
$this->orderSender = $orderSender;
123+
$this->paymentFactory = $paymentFactory;
117124
}
118125

119126
public function link()
@@ -407,7 +414,6 @@ public function updateOrder($args)
407414
->setCity($shippingAddress['city'])
408415
->setCountryId($shippingAddress['country_id'])
409416
->setRegionId($shippingAddress['zone_id'])
410-
// ->setRegion($region)
411417
->setPostcode($shippingAddress['postcode'])
412418
->setTelephone($shippingAddress['phone'])
413419
->setSaveInAddressBook(0)
@@ -446,11 +452,23 @@ public function updateOrder($args)
446452
->setSaveInAddressBook(0)
447453
->setSameAsBilling(0);
448454

455+
$this->checkoutSession->getQuote()->setCustomerEmail($shippingAddress['email']);
456+
$this->checkoutSession->getQuote()->setCustomerFirstname($shippingAddress['firstName']);
457+
$this->checkoutSession->getQuote()->setCustomerLastname($shippingAddress['lastName']);
458+
$this->checkoutSession->getQuote()->setCustomerIsGuest(1);
459+
449460
$this->checkoutSession->getQuote()->setBillingAddress($payment_address);
450461
$this->checkoutSession->getQuote()->setShippingAddress($shipping_address);
451462

463+
452464
$this->_sessionManager->setPaymentMethod($args['paymentMethod']);
453465
$this->_sessionManager->setShippingMethod($args['shippingMethod']);
466+
if(!empty($args['paymentMethod'])) {
467+
$payment = $this->paymentFactory->create();
468+
$payment->setMethod($args['paymentMethod']);
469+
$this->checkoutSession->getQuote()->setPayment($payment);
470+
}
471+
$this->checkoutSession->getQuote()->save();
454472

455473
$that = $this;
456474
return [
@@ -486,97 +504,37 @@ public function getCartItemsSkus() {
486504

487505
public function confirmOrder()
488506
{
489-
490-
$store = $this->storeManager->getStore();
491-
$storeId = $store->getStoreId();
492-
493-
$websiteId = $this->storeManager->getStore()->getWebsiteId();
494-
495-
$shippingAddress = $this->_sessionManager->getShippingAddress();
496-
$paymentAddress = $this->_sessionManager->getPaymentAddress();
497-
498-
$shippingMethod = $this->_sessionManager->getShippingMethod();
507+
$total = $this->checkoutSession->getQuote()->getGrandTotal();
508+
$order = $this->quoteManagement->submit($this->checkoutSession->getQuote());
499509
$paymentMethod = $this->_sessionManager->getPaymentMethod();
500510

501-
$customer = $this->customerFactory->create();
502-
$customer->setWebsiteId($websiteId);
503-
$customer->loadByEmail($shippingAddress['email']);// load customet by email address
504-
if(!$customer->getId()){
505-
//For guest customer create new cusotmer
506-
$customer->setWebsiteId($websiteId)
507-
->setStore($store)
508-
->setFirstname($shippingAddress['firstName'])
509-
->setLastname($shippingAddress['lastName'])
510-
->setEmail($shippingAddress['email'])
511-
->setPassword($shippingAddress['email']);
512-
$customer->save();
513-
}
514-
515-
// $quote=$this->_cartModel->getQuote();//$this->quote->create(); //Create object of quote
516-
/**
517-
* @var \Magento\Quote\Model\Quote
518-
*/
519-
$quote = $this->load->resolver('store/cart/getQuote');
520-
521-
$quote->setStore($store); //set store for our quote
522-
// /* for registered customer */
523-
524-
$customer= $this->customerRepository->getById($customer->getId());
525-
$quote->setCurrency();
526-
$quote->assignCustomer($customer); //Assign quote to customer
527-
528-
// $this->_cartModel->getQuote()->collectTotals();
529-
//add items in quote
530-
531-
// $results = $this->_cartModel->getItems();
532-
533-
// foreach ($results as $value) {
534-
// var_dump('item');
535-
// // /** @var \Magento\Catalog\Model\Product $product */
536-
// // $product = $value->getProduct();
537-
// // $quote->addProduct($product,$value->getQty());
538-
// }
539-
540-
// //Set Billing and shipping Address to quote
541-
$quote->setBillingAddress($this->checkoutSession->getQuote()->getBillingAddress());
542-
$quote->setShippingAddress($this->checkoutSession->getQuote()->getShippingAddress());
543-
544-
$methodInfo = explode('.', $shippingMethod);
545-
var_dump(count($quote->getAllVisibleItems()));
546-
547-
// set shipping method
548-
$shippingAddress=$quote->getShippingAddress();
549-
$shippingAddress->setCollectShippingRates(true)
550-
->collectShippingRates()
551-
->setShippingMethod($methodInfo[1])->setShippingCarrierCode($methodInfo[0]); //shipping method, please verify flat rate shipping must be enable
552-
// // $quote->setPaymentMethod($paymentMethod); //payment method, please verify checkmo must be enable from admin
553-
// $quote->setInventoryProcessed(false); //decrease item stock equal to qty
554-
$quote->save(); //quote save
555-
// // Set Sales Order Payment, We have taken check/money order
556-
// $quote->getPayment()->importData(['method' => $paymentMethod]);
557-
558-
// // Collect Quote Totals & Save
559-
$quote->collectTotals()->save();
560-
// // Create Order From Quote Object
561-
$order = $this->quoteManagement->submit($quote);
562-
563-
var_dump($order ? 1 : 0);
564-
565511
$orderId = null;
566512

567-
// if($order) {
568-
// // throw new ÷
513+
if(!$order) {
514+
return [];
515+
}
516+
517+
$this->orderSender->send($order);
569518

570-
// /* for send order email to customer email id */
571-
// $this->orderSender->send($order);
519+
$orderId = $order->getIncrementId();
572520

573-
// /* get order real id from order */
574-
// $orderId = $order->getIncrementId();
575-
// }
521+
$this->load->model('store/checkout');
576522

523+
$response = $this->model_store_checkout->requestCheckout(
524+
'mutation($paymentMethod: String, $total: Float, $callback: String) {
525+
createOrder(paymentMethod: $paymentMethod, total: $total, callback: $callback) {
526+
url
527+
}
528+
}',
529+
array(
530+
'paymentMethod' => $paymentMethod,
531+
'total' => floatval($total),
532+
'callback' => ''//$this->url->link('extension/d_vuefront/store/checkout/callback', 'order_id='.$orderId, true)
533+
)
534+
);
577535

578536
return [
579-
'url' => '1',
537+
'url' => $response['createOrder']['url'],
580538
'order' => [
581539
'id' => $orderId
582540
]

0 commit comments

Comments
 (0)