Creating an Order Programatically in Magento 2 is a common requirement for integrations such as ERP systems, CRMs, mobile apps, payment gateways, or custom checkout flows.
Magento provides a robust service-layer architecture that enables developers to create orders securely while adhering to business rules such as pricing, taxes, shipping, and inventory management.
This blog outlines a clean, service-based approach to creating orders in Magento 2 programmatically.
When Do You Need to Create Orders Programmatically?
- Importing orders from external platforms
- Third‑party system integration (ERP, CRM, POS)
- Custom checkout implementation
- API-based order creation
- Automated or scheduled order creation
Magento’s Internal Order Creation Flow
Magento internally uses a quote-to-order flow even for programmatic orders. This ensures accurate pricing, tax calculations, shipping validation, and proper inventory updates.
Final Implementation: Create Order Programmatically
<?php
namespace Webkul\OrderCreate\Model;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
class CreateOrder
{
protected $storeManager;
protected $customerRepository;
protected $quoteFactory;
protected $cartRepository;
protected $cartManagement;
protected $productRepository;
public function __construct(
StoreManagerInterface $storeManager,
CustomerRepositoryInterface $customerRepository,
QuoteFactory $quoteFactory,
CartRepositoryInterface $cartRepository,
CartManagementInterface $cartManagement,
ProductRepositoryInterface $productRepository
) {
$this->storeManager = $storeManager;
$this->customerRepository = $customerRepository;
$this->quoteFactory = $quoteFactory;
$this->cartRepository = $cartRepository;
$this->cartManagement = $cartManagement;
$this->productRepository = $productRepository;
}
public function createOrder($customerId, $sku, $qty = 1)
{
$store = $this->storeManager->getStore();
$quote = $this->quoteFactory->create()->setStore($store);
$customer = $this->customerRepository->getById($customerId);
$quote->assignCustomer($customer);
$product = $this->productRepository->get($sku);
$quote->addProduct($product, $qty);
$addressData = [
'firstname' => 'John',
'lastname' => 'Doe',
'street' => 'Street Address',
'city' => 'New York',
'postcode' => '10001',
'telephone' => '1234567890',
'country_id'=> 'US',
'region' => 'New York'
];
$quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate');
$quote->setPaymentMethod('checkmo');
$quote->getPayment()->importData(['method' => 'checkmo']);
$quote->collectTotals();
$this->cartRepository->save($quote);
return $this->cartManagement->placeOrder($quote->getId());
}
}
Best Practices
Finally, prefer implementing order creation logic within service classes or cron jobs for better scalability and maintainability.
Firstly, log all failures and exceptions so that issues can be easily traced and debugged later.
Secondly, validate product stock availability before adding items to the quote to avoid inventory conflicts.
Additionally, avoid executing this logic inside frontend controllers, as it can impact performance and security.
Conclusion
By following Magento’s checkout flow internally for Creating an Order Programatically, you ensure accurate pricing, tax calculation, inventory handling, and order consistency.
This method is reliable, scalable, and suitable for enterprise-level integrations.
If you need technical assistance, please reach out to us at [email protected]. Additionally, discover various solutions to improve your online store by visiting the Adobe Commerce modules section.
For professional advice or to create custom functionalities, consider hiring Adobe Commerce Developers for your project.

19 comments
how to solve this error?
Code is working fine for me except that Address is not saving in address book. Even I have set ‘save_in_address_book’ to 1 but its not working. Please help! Many thanks in advance.
Been trying to get this script to work, but keep running into an error.
Fatal error: Call to a member function getStore() on a non-object in /app/code/core/Mage/Sales/Model/Quote/Payment.php on line 219
I’ve narrowed it down to line 89 triggering it in your code, but after looking at payment.php i’m no closer to an answer.
I’m initializing magento and specifying which site to use as below…
require_once ‘app/Mage.php’;
Mage::init(‘wholesale’, ‘store’);
The customer gets created so it appears at least everything above it is working fine, but i’m not sure how to troubleshoot it from here.
Regards,
thanks