This topic will check how to create Order programmatically in Magento 2.
$order = [
'currency_id' => 'USD',
'email' => 'email@domain.com',
'shipping_address' => [
'firstname' => 'John',
'lastname' => 'Doe',
'street' => 'Demo Street',
'city' => 'Demo City',
'country_id' => 'US',
'region' => 'Illinois',
'region_id' => 562,
'postcode' => 60029,
'telephone' => 9876543210,
'fax' => '0987',
'save_in_address_book' => 0
],
'items' => [
['product_id' => '1', 'qty' => 1, 'price' => 100], // simple product
['product_id' => '2', 'qty' => 1, 'super_attribute' => array(93=>52,142=>167)] //configurable product, pass super_attribte for configurable product
]
];
$this->createOrder($order);
We will pass the Order Info to a function createOrder();
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Quote\Model\QuoteFactory $quote,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
) {
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->productRepository = $productRepository;
$this->customerRepository = $customerRepository;
$this->quote = $quote;
$this->quoteManagement = $quoteManagement;
$this->orderSender = $orderSender;
parent::__construct($context);
}
public function createOrder($order)
{
$store = $this->storeManager->getStore();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($order['email']); // load customet by email address
if (!$customer->getEntityId()) {
//If not avilable then create this customer
$customer->setWebsiteId($websiteId)->setStore($store)->setFirstname($order['shipping_address']['firstname'])->setLastname($order['shipping_address']['lastname'])->setEmail($order['email'])->setPassword($order['email']);
$customer->save();
}
$quote = $this->quote->create(); // Create Quote Object
$quote->setStore($store); // Set Store
$customer = $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer); // Assign quote to Customer
//add items in quote
foreach ($order['items'] as $item) {
$product = $this->getProductById($item['product_id']);
$product->setPrice($item['price']);
$quote->addProduct($product, intval($item['qty']));
}
$quote->getBillingAddress()->addData($order['shipping_address']);
$quote->getShippingAddress()->addData($order['shipping_address']);
// Collect Rates and Set Shipping & Payment Method
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate');
$quote->setPaymentMethod('checkmo');
$quote->setInventoryProcessed(false);
$quote->save();
// Set Sales Order Payment
$quote->getPayment()->importData(['method' => 'checkmo']);
// Collect Totals & Save Quote
$quote->collectTotals()->save();
// Create Order From Quote
$orderdata = $this->quoteManagement->submit($quote);
/* for send order email to customer email id */
$this->orderSender->send($order);
/* get order real id from order */
$orderId = $orderdata->getIncrementId();
if($orderId){
$result['success'] = $orderId;
} else {
$result = ['error' => true, 'msg' => 'Error occurs for Order placed'];
}
return $orderId;
}
With the help of code, we will also be able to create orders programmatically in Magento 2 also will be able to help in creating Duplicate Orders in Magento 2.