/** * Submit quote * * @param Quote $quote * @param array $orderData * @return \Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ protected function submitQuote(QuoteEntity $quote, $orderData = []) { $order = $this->orderFactory->create(); $this->quoteValidator->validateBeforeSubmit($quote); if (!$quote->getCustomerIsGuest()) { if ($quote->getCustomerId()) { $this->_prepareCustomerQuote($quote); } $this->customerManagement->populateCustomerInfo($quote); } $addresses = []; $quote->reserveOrderId(); if ($quote->isVirtual()) { $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData)); } else { $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getShippingAddress(), $orderData)); $shippingAddress = $this->quoteAddressToOrderAddress->convert($quote->getShippingAddress(), ['address_type' => 'shipping', 'email' => $quote->getCustomerEmail()]); $addresses[] = $shippingAddress; $order->setShippingAddress($shippingAddress); $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod()); } $billingAddress = $this->quoteAddressToOrderAddress->convert($quote->getBillingAddress(), ['address_type' => 'billing', 'email' => $quote->getCustomerEmail()]); $addresses[] = $billingAddress; $order->setBillingAddress($billingAddress); $order->setAddresses($addresses); $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment())); $order->setItems($this->resolveItems($quote)); if ($quote->getCustomer()) { $order->setCustomerId($quote->getCustomer()->getId()); } $order->setQuoteId($quote->getId()); //To Set Vat Exempt Quote values to Order $order->setVatpername($quote->getVatpername()); $order->setVatcomment($quote->getVatcomment()); $order->setVatdeclare($quote->getVatdeclare()); $order->setCustomerEmail($quote->getCustomerEmail()); $order->setCustomerFirstname($quote->getCustomerFirstname()); $order->setCustomerMiddlename($quote->getCustomerMiddlename()); $order->setCustomerLastname($quote->getCustomerLastname()); $this->eventManager->dispatch('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]); try { $order = $this->orderManagement->place($order); $quote->setIsActive(false); $this->eventManager->dispatch('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]); $this->quoteRepository->save($quote); } catch (\Exception $e) { $this->eventManager->dispatch('sales_model_service_quote_submit_failure', ['order' => $order, 'quote' => $quote, 'exception' => $e]); throw $e; } return $order; }
/** * Prepare order based on quote address * * @param \Magento\Quote\Model\Quote\Address $address * @return \Magento\Sales\Model\Order * @throws \Magento\Checkout\Exception */ protected function _prepareOrder(\Magento\Quote\Model\Quote\Address $address) { $quote = $this->getQuote(); $quote->unsReservedOrderId(); $quote->reserveOrderId(); $quote->collectTotals(); $order = $this->quoteAddressToOrder->convert($address); $order->setQuote($quote); $order->setBillingAddress($this->quoteAddressToOrderAddress->convert($quote->getBillingAddress())); if ($address->getAddressType() == 'billing') { $order->setIsVirtual(1); } else { $order->setShippingAddress($this->quoteAddressToOrderAddress->convert($address)); } $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment())); if ($this->priceCurrency->round($address->getGrandTotal()) == 0) { $order->getPayment()->setMethod('free'); } foreach ($address->getAllItems() as $item) { $_quoteItem = $item->getQuoteItem(); if (!$_quoteItem) { throw new \Magento\Checkout\Exception(__('Item not found or already ordered')); } $item->setProductType($_quoteItem->getProductType())->setProductOptions($_quoteItem->getProduct()->getTypeInstance()->getOrderOptions($_quoteItem->getProduct())); $orderItem = $this->quoteItemToOrderItem->convert($item); if ($item->getParentItem()) { $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); } $order->addItem($orderItem); } return $order; }