/** * Creates a sample order * * @throws \Magento\Framework\Exception\LocalizedException */ public function createOrder() { $store = $this->storeManager->getStore(); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $customer = $this->getCustomer(); $repositoryCustomer = $this->customerRepository->getById($customer->getId()); $quote = $this->quoteFactory->create(); $quote->setStore($store); $quote->assignCustomer($repositoryCustomer); $this->addItemsToQuote($quote); $quote->getBillingAddress()->addData($this->orderSampleData['address']); $quote->getShippingAddress()->addData($this->orderSampleData['address']); $this->addressRate->setCode(self::ORDER_SHIPPING_METHOD_CODE); $this->addressRate->getPrice(1); $shippingAddress = $quote->getShippingAddress(); $shippingAddress->addShippingRate($this->addressRate); $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod(self::ORDER_SHIPPING_METHOD_CODE); $quote->setPaymentMethod(self::ORDER_PAYMENT_METHOD_CODE); $quote->setInventoryProcessed(false); $quote->save(); $quote->getPayment()->importData(['method' => self::ORDER_PAYMENT_METHOD_CODE]); $quote->collectTotals()->save(); $order = $this->quoteManagement->submit($quote); $order->setEmailSent(0); }
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)); }
/** * @param Quote $quote * * @return Order */ public function submitOrder($quote) { /* ev. use placeOrder? */ /* $this->_quoteManagement->placeOrder() */ /** @var Order $order */ $order = $this->_quoteManagement->submit($quote); $order->save(); return $order; }
/** * Add one order to customer. * * @param $customer * @param $itemsData */ public function addOrder($customer, $itemsData) { /* create order for Russian store/stock */ $this->_manStore->setCurrentStore(self::STORE_ID_RUS); $store = $this->_manStore->getStore(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->_manObj->create(\Magento\Quote\Model\Quote::class); $quote->setStore($store); $quote->assignCustomer($customer); $quote->setInventoryProcessed(false); //not effect inventory /** Populate orders with data. */ $this->_populateQuoteItems($quote); $this->_populateQuoteAddrShipping($quote, $customer); $this->_populateQuoteAddrBilling($quote, $customer); $this->_populateQuoteShippingMethod($quote); $this->_populateQuotePaymentMethod($quote); /* save quote then reload it by ID to create IDs for items (see MOBI-434, $_items, $_data['items'], $_data['items_collection']) */ $quote->collectTotals(); $quote->save(); $id = $quote->getId(); $quote = $this->_manObj->create(\Magento\Quote\Model\Quote::class); $quote->load($id); $quoteItems = $quote->getItemsCollection(); // Create Order From Quote /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->_manQuote->submit($quote); $items = $order->getItems(); $item = reset($items); $item->setBaseOriginalPrice(8); $item->save(); $order->save(); /* register PV */ $this->_manEvent->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]); /* prepare invoice */ $invoice = $this->_manInvoice->prepareInvoice($order); $invoice->register(); $invoice->save(); // $invoiceId = $invoice->getEntityId(); /* update date paid in PV register */ // $orderId = $order->getEntityId(); // $bind = [ // \Praxigento\Pv\Data\Entity\Sale::ATTR_DATE_PAID => self::DATE_PAID // ]; // $this->_repoPvSale->updateById($orderId, $bind); /* transfer PV to customer account */ // $invoice->load($invoiceId); // $order->load($orderId); $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING); $this->_manEvent->dispatch('sales_order_invoice_pay', ['order' => $order, 'invoice' => $invoice]); }
/** * Create order based on checkout type. Create customer if necessary. * * @return $this */ public function saveOrder() { $this->validate(); $isNewCustomer = false; switch ($this->getCheckoutMethod()) { case self::METHOD_GUEST: $this->_prepareGuestQuote(); break; case self::METHOD_REGISTER: $this->_prepareNewCustomerQuote(); $isNewCustomer = true; break; default: $this->_prepareCustomerQuote(); break; } $order = $this->quoteManagement->submit($this->getQuote()); if ($isNewCustomer) { try { $this->_involveNewCustomer(); } catch (\Exception $e) { $this->_logger->critical($e); } } $this->_checkoutSession->setLastQuoteId($this->getQuote()->getId())->setLastSuccessQuoteId($this->getQuote()->getId())->clearHelperData(); if ($order) { $this->_eventManager->dispatch('checkout_type_onepage_save_order_after', ['order' => $order, 'quote' => $this->getQuote()]); /** * a flag to set that there will be redirect to third party after confirmation */ $redirectUrl = $this->getQuote()->getPayment()->getOrderPlaceRedirectUrl(); /** * we only want to send to customer about new order when there is no redirect to third party */ if (!$redirectUrl && $order->getCanSendNewEmailFlag()) { try { $this->orderSender->send($order); } catch (\Exception $e) { $this->_logger->critical($e); } } // add order information to the session $this->_checkoutSession->setLastOrderId($order->getId())->setRedirectUrl($redirectUrl)->setLastRealOrderId($order->getIncrementId())->setLastOrderStatus($order->getStatus()); } $this->_eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $this->getQuote()]); return $this; }
/** * Create new order * * @return \Magento\Sales\Model\Order */ public function createOrder() { $this->_prepareCustomer(); $this->_validate(); $quote = $this->getQuote(); $this->_prepareQuoteItems(); $orderData = []; if ($this->getSession()->getOrder()->getId()) { $oldOrder = $this->getSession()->getOrder(); $originalId = $oldOrder->getOriginalIncrementId(); if (!$originalId) { $originalId = $oldOrder->getIncrementId(); } $orderData = ['original_increment_id' => $originalId, 'relation_parent_id' => $oldOrder->getId(), 'relation_parent_real_id' => $oldOrder->getIncrementId(), 'edit_increment' => $oldOrder->getEditIncrement() + 1, 'increment_id' => $originalId . '-' . ($oldOrder->getEditIncrement() + 1)]; $quote->setReservedOrderId($orderData['increment_id']); } $order = $this->quoteManagement->submit($quote, $orderData); if ($this->getSession()->getOrder()->getId()) { $oldOrder = $this->getSession()->getOrder(); $oldOrder->setRelationChildId($order->getId()); $oldOrder->setRelationChildRealId($order->getIncrementId()); $this->orderManagement->cancel($oldOrder->getEntityId()); $order->save(); } if ($this->getSendConfirmation()) { $this->emailSender->send($order); } $this->_eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]); return $order; }
/** * Place the order when customer returned from PayPal until this moment all quote data must be valid. * * @param string $token * @param string|null $shippingMethodCode * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function place($token, $shippingMethodCode = null) { if ($shippingMethodCode) { $this->updateShippingMethod($shippingMethodCode); } $isNewCustomer = false; switch ($this->getCheckoutMethod()) { case \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST: $this->_prepareGuestQuote(); break; case \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER: $this->_prepareNewCustomerQuote(); $isNewCustomer = true; break; default: $this->_prepareCustomerQuote(); break; } $this->_ignoreAddressValidation(); $this->_quote->collectTotals(); $order = $this->quoteManagement->submit($this->_quote); if ($isNewCustomer) { try { $this->_involveNewCustomer(); } catch (\Exception $e) { $this->_logger->critical($e); } } if (!$order) { return; } // commence redirecting to finish payment, if paypal requires it if ($order->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_REDIRECT)) { $this->_redirectUrl = $this->_config->getExpressCheckoutCompleteUrl($token); } switch ($order->getState()) { // even after placement paypal can disallow to authorize/capture, but will wait until bank transfers money case \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT: // TODO break; // regular placement, when everything is ok // regular placement, when everything is ok case \Magento\Sales\Model\Order::STATE_PROCESSING: case \Magento\Sales\Model\Order::STATE_COMPLETE: case \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW: $this->orderSender->send($order); $this->_checkoutSession->start(); break; default: break; } $this->_order = $order; }