/** * Convert quote address to order address * * @param \Magento\Sales\Model\Quote\Address $address * @return \Magento\Sales\Model\Order\Address */ public function addressToOrderAddress(\Magento\Sales\Model\Quote\Address $address) { $orderAddress = $this->_orderAddressFactory->create()->setStoreId($address->getStoreId())->setAddressType($address->getAddressType())->setCustomerId($address->getCustomerId())->setCustomerAddressId($address->getCustomerAddressId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_address', 'to_order_address', $address, $orderAddress); $this->_eventManager->dispatch('sales_convert_quote_address_to_order_address', array('address' => $address, 'order_address' => $orderAddress)); return $orderAddress; }
/** * Initialize new order based on payment data * * Takes arbitrary number of \Magento\Framework\Object instances to be treated as items for new order * * @return \Magento\Sales\Model\Order * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function createOrder() { $items = array(); $itemInfoObjects = func_get_args(); $billingAmount = 0; $shippingAmount = 0; $taxAmount = 0; $isVirtual = 1; $weight = 0; foreach ($itemInfoObjects as $itemInfo) { $item = $this->_getItem($itemInfo); $billingAmount += $item->getPrice(); $shippingAmount += $item->getShippingAmount(); $taxAmount += $item->getTaxAmount(); $weight += $item->getWeight(); if (!$item->getIsVirtual()) { $isVirtual = 0; } $items[] = $item; } $grandTotal = $billingAmount + $shippingAmount + $taxAmount; $order = $this->_orderFactory->create(); $billingAddress = $this->_addressFactory->create()->setData($this->getBillingAddressInfo())->setId(null); $shippingInfo = $this->getShippingAddressInfo(); $shippingAddress = $this->_addressFactory->create()->setData($shippingInfo)->setId(null); $payment = $this->_paymentFactory->create()->setMethod($this->getMethodCode()); $transferDataKeys = array('store_id', 'store_name', 'customer_id', 'customer_email', 'customer_firstname', 'customer_lastname', 'customer_middlename', 'customer_prefix', 'customer_suffix', 'customer_taxvat', 'customer_gender', 'customer_is_guest', 'customer_note_notify', 'customer_group_id', 'customer_note', 'shipping_method', 'shipping_description', 'base_currency_code', 'global_currency_code', 'order_currency_code', 'store_currency_code', 'base_to_global_rate', 'base_to_order_rate', 'store_to_base_rate', 'store_to_order_rate'); $orderInfo = $this->getOrderInfo(); foreach ($transferDataKeys as $key) { if (isset($orderInfo[$key])) { $order->setData($key, $orderInfo[$key]); } elseif (isset($shippingInfo[$key])) { $order->setData($key, $shippingInfo[$key]); } } $order->setStoreId($this->getStoreId())->setState(\Magento\Sales\Model\Order::STATE_NEW)->setBaseToOrderRate($this->getInfoValue('order_info', 'base_to_quote_rate'))->setStoreToOrderRate($this->getInfoValue('order_info', 'store_to_quote_rate'))->setOrderCurrencyCode($this->getInfoValue('order_info', 'quote_currency_code'))->setBaseSubtotal($billingAmount)->setSubtotal($billingAmount)->setBaseShippingAmount($shippingAmount)->setShippingAmount($shippingAmount)->setBaseTaxAmount($taxAmount)->setTaxAmount($taxAmount)->setBaseGrandTotal($grandTotal)->setGrandTotal($grandTotal)->setIsVirtual($isVirtual)->setWeight($weight)->setTotalQtyOrdered($this->getInfoValue('order_info', 'items_qty'))->setBillingAddress($billingAddress)->setShippingAddress($shippingAddress)->setPayment($payment); foreach ($items as $item) { $order->addItem($item); } return $order; }