Пример #1
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));
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
 }