Back to Top

Programmatically Create Order in Magento 2

Updated 30 December 2025

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.

Searching for an experienced
Magento Company ?
Find out More

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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


19 comments

  • kalpesh patel
    • Jaydip Kansagra
  • kalpesh patel
  • MagentoUser
  • Pranav
  • Jenish Khunt
  • TAhmed
    • webkul
      • TAhmed
        • Jaydip Kansagra
  • Sampathi Ramakrishna
    • webkul
  • udit gupta
  • theunknown
  • chaosratt
    • webkul
      • Mitesh Patel
        • webkul
      • chaosratt
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home