Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
order_with_shipping_and_invoice.php
Go to the documentation of this file.
1 <?php
8 
9 require 'order.php';
10 
12 
14 $order = $objectManager->create(\Magento\Sales\Model\Order::class)
15  ->loadByIncrementId('100000001');
16 
18 $invoiceService = $objectManager->create(\Magento\Sales\Api\InvoiceManagementInterface::class);
19 
21 $transaction = $objectManager->create(\Magento\Framework\DB\Transaction::class);
22 
23 $order->setData(
24  'base_to_global_rate',
25  1
26 )->setData(
27  'base_to_order_rate',
28  1
29 )->setData(
30  'shipping_amount',
31  20
32 )->setData(
33  'base_shipping_amount',
34  20
35 );
36 
37 $invoice = $invoiceService->prepareInvoice($order);
38 $invoice->register();
39 
40 $order->setIsInProcess(true);
41 
42 $items = [];
43 foreach ($order->getItems() as $orderItem) {
44  $items[$orderItem->getId()] = $orderItem->getQtyOrdered();
45 }
46 $shipment = $objectManager->get(ShipmentFactory::class)->create($order, $items);
47 
48 $transaction->addObject($invoice)->addObject($shipment)->addObject($order)->save();
$orderItem
Definition: order.php:30
foreach($order->getItems() as $orderItem) $shipment