public function testConvert() { $orderData = ['test' => 'test1']; $data = ['test' => 'beer']; /** * @var \Magento\Quote\Model\Quote\Address $object */ $object = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $this->objectCopyMock->expects($this->once())->method('getDataFromFieldset')->with('sales_convert_quote_address', 'to_order_address', $object)->willReturn($orderData); $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($this->orderInterfaceMock, ['test' => 'beer'], '\\Magento\\Sales\\Api\\Data\\OrderAddressInterface')->willReturnSelf(); $this->orderAddressRepositoryMock->expects($this->once())->method('create')->willReturn($this->orderInterfaceMock); $this->assertSame($this->orderInterfaceMock, $this->converter->convert($object, $data)); }
public function testSubmit() { $orderData = []; $isGuest = true; $isVirtual = false; $customerId = 1; $quoteId = 1; $quoteItem = $this->getMock('Magento\\Quote\\Model\\Quote\\Item', [], [], '', false); $billingAddress = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $shippingAddress = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $payment = $this->getMock('Magento\\Quote\\Model\\Quote\\Payment', [], [], '', false); $baseOrder = $this->getMock('Magento\\Sales\\Api\\Data\\OrderInterface', [], [], '', false); $convertedBillingAddress = $this->getMock('Magento\\Sales\\Api\\Data\\OrderAddressInterface', [], [], '', false); $convertedShippingAddress = $this->getMock('Magento\\Sales\\Api\\Data\\OrderAddressInterface', [], [], '', false); $convertedPayment = $this->getMock('Magento\\Sales\\Api\\Data\\OrderPaymentInterface', [], [], '', false); $convertedQuoteItem = $this->getMock('Magento\\Sales\\Api\\Data\\OrderItemInterface', [], [], '', false); $addresses = [$convertedShippingAddress, $convertedBillingAddress]; $payments = [$convertedPayment]; $quoteItems = [$quoteItem]; $convertedItems = [$convertedQuoteItem]; $quote = $this->getQuote($isGuest, $isVirtual, $billingAddress, $payment, $customerId, $quoteId, $quoteItems, $shippingAddress); $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote); $this->quoteAddressToOrder->expects($this->once())->method('convert')->with($shippingAddress, $orderData)->willReturn($baseOrder); $this->quoteAddressToOrderAddress->expects($this->at(0))->method('convert')->with($shippingAddress, ['address_type' => 'shipping', 'email' => '*****@*****.**'])->willReturn($convertedShippingAddress); $this->quoteAddressToOrderAddress->expects($this->at(1))->method('convert')->with($billingAddress, ['address_type' => 'billing', 'email' => '*****@*****.**'])->willReturn($convertedBillingAddress); $this->quoteItemToOrderItem->expects($this->once())->method('convert')->with($quoteItem, ['parent_item' => null])->willReturn($convertedQuoteItem); $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)->willReturn($convertedPayment); $order = $this->prepareOrderFactory($baseOrder, $convertedBillingAddress, $addresses, $payments, $convertedItems, $quoteId, $convertedShippingAddress); $this->orderManagement->expects($this->once())->method('place')->with($order)->willReturn($order); $this->eventManager->expects($this->at(0))->method('dispatch')->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]); $this->eventManager->expects($this->at(1))->method('dispatch')->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]); $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote); $this->assertEquals($order, $this->model->submit($quote, $orderData)); }
public function testConvert() { $orderData = ['test' => 'test1']; $data = ['test' => 'beer']; $quoteId = 1; $storeId = 777; $object = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false); $quote = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false); $object->expects($this->exactly(5))->method('getQuote')->willReturn($quote); $quote->expects($this->once())->method('getId')->willReturn($quoteId); $quote->expects($this->once())->method('getStoreId')->willReturn($storeId); $this->objectCopyMock->expects($this->once())->method('getDataFromFieldset')->with( 'quote_convert_address', 'to_order', $object )->willReturn($orderData); $this->dataObjectHelper->expects($this->once())->method('populateWithArray') ->with($this->orderMock, ['test' => 'beer'], '\Magento\Sales\Api\Data\OrderInterface') ->willReturnSelf(); $this->orderMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf(); $this->orderMock->expects($this->once())->method('setQuoteId')->with($quoteId)->willReturnSelf(); $this->orderDataFactoryMock->expects($this->once())->method('create')->willReturn($this->orderMock); $this->eventManagerMock->expects($this->once()) ->method('dispatch') ->with('sales_convert_quote_to_order', ['order' => $this->orderMock, 'quote' => $quote]); $this->assertSame($this->orderMock, $this->converter->convert($object, $data)); }
/** * {@inheritdoc} */ public function convert(\Magento\Quote\Model\Quote\Address $object, $data = array()) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'convert'); if (!$pluginInfo) { return parent::convert($object, $data); } else { return $this->___callPlugins('convert', func_get_args(), $pluginInfo); } }
/** * 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; }