order.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. use Magento\Sales\Api\OrderRepositoryInterface;
  8. use Magento\Sales\Model\Order;
  9. use Magento\Sales\Model\Order\Address;
  10. use Magento\Sales\Model\Order\Item as OrderItem;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\ObjectManager;
  14. /** @var ObjectManager $objectManager */
  15. $objectManager = Bootstrap::getObjectManager();
  16. $addressData = include __DIR__ . '/../../Sales/_files/address_data.php';
  17. require __DIR__ . '/../../../Magento/Catalog/_files/product_simple.php';
  18. $billingAddress = $objectManager->create(Address::class, ['data' => $addressData]);
  19. $billingAddress->setAddressType('billing');
  20. $shippingAddress = clone $billingAddress;
  21. $shippingAddress->setId(null)
  22. ->setAddressType('shipping');
  23. /** @var OrderItem $orderItem */
  24. $orderItem = $objectManager->create(OrderItem::class);
  25. $orderItem->setProductId($product->getId())
  26. ->setQtyOrdered(2)
  27. ->setBasePrice($product->getPrice())
  28. ->setPrice($product->getPrice())
  29. ->setRowTotal($product->getPrice())
  30. ->setProductType('simple');
  31. require __DIR__ . '/payment.php';
  32. $order = $objectManager->create(Order::class);
  33. $order->setIncrementId('100000002')
  34. ->setSubtotal($product->getPrice() * 2)
  35. ->setBaseSubtotal($product->getPrice() * 2)
  36. ->setCustomerEmail('admin@example.com')
  37. ->setCustomerIsGuest(true)
  38. ->setBillingAddress($billingAddress)
  39. ->setShippingAddress($shippingAddress)
  40. ->setStoreId(
  41. $objectManager->get(StoreManagerInterface::class)->getStore()
  42. ->getId()
  43. )
  44. ->addItem($orderItem)
  45. ->setPayment($payment);
  46. /** @var OrderRepositoryInterface $orderRepository */
  47. $orderRepository = $objectManager->get(OrderRepositoryInterface::class);
  48. $orderRepository->save($order);