/** * @param Address $object * @param array $data * @return OrderInterface */ public function convert(Address $object, $data = []) { $orderData = $this->objectCopyService->getDataFromFieldset( 'quote_convert_address', 'to_order', $object ); /** * @var $order \Magento\Sales\Model\Order */ $order = $this->orderFactory->create(); $this->dataObjectHelper->populateWithArray( $order, array_merge($orderData, $data), '\Magento\Sales\Api\Data\OrderInterface' ); $order->setStoreId($object->getQuote()->getStoreId()) ->setQuoteId($object->getQuote()->getId()) ->setIncrementId($object->getQuote()->getReservedOrderId()); $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $object->getQuote(), $order); $this->eventManager->dispatch( 'sales_convert_quote_to_order', ['order' => $order, 'quote' => $object->getQuote()] ); return $order; }
/** * 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; }
/** * @param \Magento\Sales\Api\Data\OrderInterface $baseOrder * @param \Magento\Sales\Api\Data\OrderAddressInterface $billingAddress * @param array $addresses * @param array $payments * @param array $items * @param $quoteId * @param \Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress * @return \PHPUnit_Framework_MockObject_MockObject */ protected function prepareOrderFactory(\Magento\Sales\Api\Data\OrderInterface $baseOrder, \Magento\Sales\Api\Data\OrderAddressInterface $billingAddress, array $addresses, array $payments, array $items, $quoteId, \Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress = null, $customerId = null) { $order = $this->getMock('Magento\\Sales\\Model\\Order', ['setShippingAddress', 'getAddressesCollection', 'getAddresses', 'getBillingAddress', 'addAddresses', 'setBillingAddress', 'setAddresses', 'setPayments', 'setItems', 'setQuoteId'], [], '', false); $this->orderFactory->expects($this->once())->method('create')->willReturn($order); $this->orderFactory->expects($this->never())->method('populate')->with($baseOrder); if ($shippingAddress) { $order->expects($this->once())->method('setShippingAddress')->with($shippingAddress); } if ($customerId) { $this->orderFactory->expects($this->once())->method('setCustomerId')->with($customerId); } $order->expects($this->any())->method('getAddressesCollection'); $order->expects($this->any())->method('getAddresses'); $order->expects($this->any())->method('getBillingAddress')->willReturn(false); $order->expects($this->any())->method('addAddresses')->withAnyParameters()->willReturnSelf(); $order->expects($this->once())->method('setBillingAddress')->with($billingAddress); $order->expects($this->once())->method('setAddresses')->with($addresses); $order->expects($this->once())->method('setPayments')->with($payments); $order->expects($this->once())->method('setItems')->with($items); $order->expects($this->once())->method('setQuoteId')->with($quoteId); return $order; }