/**
  * Prepare order based on quote address
  *
  * @param   \Magento\Sales\Model\Quote\Address $address
  * @return  \Magento\Sales\Model\Order
  * @throws  \Magento\Checkout\Exception
  */
 protected function _prepareOrder(\Magento\Sales\Model\Quote\Address $address)
 {
     $quote = $this->getQuote();
     $quote->unsReservedOrderId();
     $quote->reserveOrderId();
     $quote->collectTotals();
     $order = $this->_quote->addressToOrder($address);
     $order->setQuote($quote);
     $order->setBillingAddress($this->_quote->addressToOrderAddress($quote->getBillingAddress()));
     if ($address->getAddressType() == 'billing') {
         $order->setIsVirtual(1);
     } else {
         $order->setShippingAddress($this->_quote->addressToOrderAddress($address));
     }
     $order->setPayment($this->_quote->paymentToOrderPayment($quote->getPayment()));
     if ($this->_storeManager->getStore()->roundPrice($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->_quote->itemToOrderItem($item);
         if ($item->getParentItem()) {
             $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
         }
         $order->addItem($orderItem);
     }
     return $order;
 }
Beispiel #2
0
 /**
  * Submit the quote. Quote submit process will create the order based on quote data
  *
  * @return \Magento\Sales\Model\Order
  * @throws \Exception
  */
 public function submitOrderWithDataObject()
 {
     $this->_deleteNominalItems();
     $this->_validate();
     $quote = $this->_quote;
     $isVirtual = $quote->isVirtual();
     $transaction = $this->_transactionFactory->create();
     $customerData = null;
     if (!$quote->getCustomerIsGuest()) {
         $customerData = $quote->getCustomerData();
         $addresses = $quote->getCustomerAddressData();
         $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerData)->setAddresses($addresses)->create();
         if ($customerData->getId()) {
             $this->_customerAccountService->updateCustomer($customerDetails);
         } else {
             //for new customers
             $customerData = $this->_customerAccountService->createCustomerWithPasswordHash($customerDetails, $quote->getPasswordHash());
             $addresses = $this->_customerAddressService->getAddresses($customerData->getId());
             //Update quote address information
             foreach ($addresses as $address) {
                 if ($address->isDefaultBilling()) {
                     $quote->getBillingAddress()->setCustomerAddressData($address);
                 } else {
                     if ($address->isDefaultShipping()) {
                         $quote->getShippingAddress()->setCustomerAddressData($address);
                     }
                 }
             }
             if ($quote->getShippingAddress() && $quote->getShippingAddress()->getSameAsBilling()) {
                 $quote->getShippingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData());
             }
         }
         $quote->setCustomerData($customerData)->setCustomerAddressData($addresses);
     }
     $transaction->addObject($quote);
     $quote->reserveOrderId();
     if ($isVirtual) {
         $order = $this->_convertor->addressToOrder($quote->getBillingAddress());
     } else {
         $order = $this->_convertor->addressToOrder($quote->getShippingAddress());
     }
     $order->setBillingAddress($this->_convertor->addressToOrderAddress($quote->getBillingAddress()));
     if ($quote->getBillingAddress()->getCustomerAddressData()) {
         $order->getBillingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData());
     }
     if (!$isVirtual) {
         $order->setShippingAddress($this->_convertor->addressToOrderAddress($quote->getShippingAddress()));
         if ($quote->getShippingAddress()->getCustomerAddressData()) {
             $order->getShippingAddress()->setCustomerAddressData($quote->getShippingAddress()->getCustomerAddressData());
         }
     }
     $order->setPayment($this->_convertor->paymentToOrderPayment($quote->getPayment()));
     foreach ($this->_orderData as $key => $value) {
         $order->setData($key, $value);
     }
     foreach ($quote->getAllItems() as $item) {
         $orderItem = $this->_convertor->itemToOrderItem($item);
         if ($item->getParentItem()) {
             $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
         }
         $order->addItem($orderItem);
     }
     if ($customerData) {
         $order->setCustomerId($customerData->getId());
     }
     $order->setQuote($quote);
     $transaction->addObject($order);
     $transaction->addCommitCallback(array($order, 'place'));
     $transaction->addCommitCallback(array($order, 'save'));
     /**
      * We can use configuration data for declare new order status
      */
     $this->_eventManager->dispatch('checkout_type_onepage_save_order', array('order' => $order, 'quote' => $quote));
     $this->_eventManager->dispatch('sales_model_service_quote_submit_before', array('order' => $order, 'quote' => $quote));
     try {
         $transaction->save();
         $this->_inactivateQuote();
         $this->_eventManager->dispatch('sales_model_service_quote_submit_success', array('order' => $order, 'quote' => $quote));
     } catch (\Exception $e) {
         //reset order ID's on exception, because order not saved
         $order->setId(null);
         /** @var $item \Magento\Sales\Model\Order\Item */
         foreach ($order->getItemsCollection() as $item) {
             $item->setOrderId(null);
             $item->setItemId(null);
         }
         $this->_eventManager->dispatch('sales_model_service_quote_submit_failure', array('order' => $order, 'quote' => $quote));
         throw $e;
     }
     $this->_order = $order;
     return $order;
 }