Magento 2 create order programmatically

This is the helpful tutorial for the developers about Magento 2 create order programmatically. Why do I say that? Because the post will guide you carry out with Magento 2 console and create an amount of Magento 2 orders programmatically as well instead of the time-consuming manual progress. However, in order to start creating the order, it is required to create a quote at the same time. Let’s get all through this topic.

How to create order programmatically Magento 2

Now, you can create quote and order with the below content:

     'currency_id'  => 'USD',
     'email'        => '[email protected]', //buyer email id
     'shipping_address' =>[
            'firstname'    => 'John', //address Details
            'lastname'     => 'Doe',
                    'street' => '123 Demo',
                    'city' => 'Mageplaza',
            'country_id' => 'US',
            'region' => 'xxx',
            'postcode' => '10019',
            'telephone' => '0123456789',
            'fax' => '32423',
            'save_in_address_book' => 1
   'items'=> [ //array of product which order you want to create

Continuing to declare the order in the module helper file by the following function:

namespace YourNameSpace\ModuleName\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
    * @param Magento\Framework\App\Helper\Context $context
    * @param Magento\Store\Model\StoreManagerInterface $storeManager
    * @param Magento\Catalog\Model\Product $product
    * @param Magento\Framework\Data\Form\FormKey $formKey $formkey,
    * @param Magento\Quote\Model\Quote $quote,
    * @param Magento\Customer\Model\CustomerFactory $customerFactory,
    * @param Magento\Sales\Model\Service\OrderService $orderService,
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\Data\Form\FormKey $formkey,
        \Magento\Quote\Model\QuoteFactory $quote,
        \Magento\Quote\Model\QuoteManagement $quoteManagement,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Sales\Model\Service\OrderService $orderService  
    ) {
        $this->_storeManager = $storeManager;
        $this->_product = $product;
        $this->_formkey = $formkey;
        $this->quote = $quote;
        $this->quoteManagement = $quoteManagement;
        $this->customerFactory = $customerFactory;
        $this->customerRepository = $customerRepository;
        $this->orderService = $orderService;
     * Create Order On Your Store
     * @param array $orderData
     * @return array
    public function createMageOrder($orderData) {
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customer->loadByEmail($orderData['email']);// load customet by email address
            //If not avilable then create this customer 
        $quote=$this->quote->create(); //Create object of quote
        $quote->setStore($store); //set store for which you create quote
        // if you have allready buyer id then you can load customer directly 
        $customer= $this->customerRepository->getById($customer->getEntityId());
        $quote->assignCustomer($customer); //Assign quote to customer
        //add items in quote
        foreach($orderData['items'] as $item){
        //Set Address to quote
        // Collect Rates and Set Shipping & Payment Method
                        ->setShippingMethod('freeshipping_freeshipping'); //shipping method
        $quote->setPaymentMethod('checkmo'); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory
        $quote->save(); //Now Save quote and your quote is ready
        // Set Sales Order Payment
        $quote->getPayment()->importData(['method' => 'checkmo']);
        // Collect Totals & Save Quote
        // Create Order From Quote
        $order = $this->quoteManagement->submit($quote);
        $increment_id = $order->getRealOrderId();
            $result['order_id']= $order->getRealOrderId();
            $result=['error'=>1,'msg'=>'Your custom message'];
        return $result;

When you have done the above instruction, congratulate you have created Magento 2 orders programmatically!

If you got this error message: Exception printing is disabled by default for security reasons, this topic may help.

Enjoyed the tutorial? Spread it to your friends!


Comments for Magento 2 create order programmatically

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

10 reviews

Layered Navigation

12 reviews


no review

People also searched for:

  • magento 2 create order programmatically
  • magento 2 create order programmatically with custom options
  • magento 2 create order programmatically please specify a shipping method
  • magento 2 create order programmatically with coupon code
  • magento 2 create order programmatically admin
  • magento 2 create new order programmatically
  • magento 2 create order invoice programmatically
  • magento 2 create order configurable product programmatically