Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
order_express_with_invoice_and_shipping.php
Go to the documentation of this file.
1 <?php
17 
18 require __DIR__ . '/../../../Magento/Catalog/_files/product_simple.php';
19 
20 $objectManager = Bootstrap::getObjectManager();
21 
22 $addressData = include __DIR__ . '/address_data.php';
24  Address::class,
25  ['data' => $addressData]
26 );
27 $billingAddress->setAddressType('billing');
29 $shippingAddress->setId(null)->setAddressType('shipping');
30 
31 $payment = $objectManager->create(Payment::class);
32 $payment->setMethod(Config::METHOD_WPP_EXPRESS);
33 
35 $orderItem = $objectManager->create(Item::class);
36 $orderItem->setProductId($product->getId())->setQtyOrdered(1);
37 $orderItem->setBasePrice($product->getPrice());
38 $orderItem->setPrice($product->getPrice());
39 $orderItem->setRowTotal($product->getPrice());
40 $orderItem->setRowTotalInclTax($product->getPrice());
41 $orderItem->setBaseRowTotal($product->getPrice());
42 $orderItem->setBaseRowTotalInclTax($product->getPrice());
43 $orderItem->setBaseRowInvoiced($product->getPrice());
44 $orderItem->setProductType('simple');
45 
46 $itemsAmount = $product->getPrice();
49 
51 $order = $objectManager->create(Order::class);
52 $order->setCustomerEmail('[email protected]')
53  ->setIncrementId('100000001')
54  ->addItem($orderItem)
55  ->setSubtotal($itemsAmount)
56  ->setBaseSubtotal($itemsAmount)
57  ->setBaseGrandTotal($totalAmount)
58  ->setGrandTotal($totalAmount)
59  ->setBaseCurrencyCode('USD')
60  ->setCustomerIsGuest(true)
61  ->setStoreId(1)
62  ->setEmailSent(true)
63  ->setState(Order::STATE_PROCESSING)
64  ->setBillingAddress($billingAddress)
65  ->setShippingAddress($shippingAddress)
66  ->setBaseTotalPaid($totalAmount)
67  ->setTotalPaid($totalAmount)
68  ->setData('base_to_global_rate', 1)
69  ->setData('base_to_order_rate', 1)
70  ->setData('shipping_amount', $shippingAmount)
71  ->setData('base_shipping_amount', $shippingAmount)
72  ->setPayment($payment);
73 
75 $orderRepository = $objectManager->get(OrderRepositoryInterface::class);
77 
79 $invoiceService = $objectManager->create(InvoiceManagementInterface::class);
80 
82 $transaction = $objectManager->create(Transaction::class);
83 
84 $invoice = $invoiceService->prepareInvoice($order, [$orderItem->getId() => 1]);
85 $invoice->register();
86 
87 $transaction->addObject($invoice)->addObject($order)->save();
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60