Creating Magento 2 Orders Programmatically

Creating Magento 2 orders programmatically

If you're a Magento 2 developer, sooner or later you'll need to generate multiple orders — and doing it manually can be time-consuming and inefficient. That’s why programmatic order creation is often the preferred approach.

I built this Magento 2 order generator to meet the needs of the Amasty team and decided to share it with you. It also allows you to set the Magento 2 payment method programmatically, making the process even more streamlined.

If you're looking to test how custom data behaves within orders, consider pairing this tool with our Order Attributes for Magento 2 extension. It lets you add extra fields to the checkout and order view pages, helping you collect and manage all the data you need — ideal for advanced testing or enriching customer order data.

Note: Try out our same-titled extensions to export Magento 2Magento 1 orders with all the related data.

I created this generator to speed up order creation for testing and debugging purposes. Personally, I use it to verify how statistics behave, how our extensions handle large volumes of data, and for other specific development needs.

Creating Magento 2 Orders Programmatically

Magento 2 lets you create console extensions, and I chose this method because adding a user interface for this task would be unnecessary.

How to Install the Magento 2 Order Generator

You can install the order generator like any other console extension:

1. Copy the extension files to the default catalog.

2. Go to the console in the default catalog (double-check, as the console may point to another directory by default) and run the command:
php bin/magento setup:upgrade

How to Run the M2 Order Generator + Bonus Feature: Generating Customers

After installing the extension, you can start generating orders. This Magento 2 order generator can also create customers — but only in combination with order generation. Standalone customer creation isn’t supported.

To generate orders and customers, use the command:
php bin/magento amasty:create_orders 5 3
Here, 5 is the number of orders per customer, and 3 is the number of customers to generate orders for.

You can set your own numbers. If you omit the customer number, the tool will default to generating orders for a single customer.

Keep in mind that generating a large number of Magento 2 orders for one customer may take more time. This slowdown also occurs when manually creating many orders without a script, and we’re still investigating the root cause.

Magento 2 Order Generation Example

Magento 2 order generation example

If you've set a high number of orders or customers (e.g., 200) and need to interrupt the process, use CTRL+C — the standard Linux shortcut to stop a running command.

Example Output

  • Orders generated for Magento 2

  • Customers generated for M2

Each order includes relevant Magento 2 order comments to simulate real-world data and help with debugging or testing workflows that rely on customer communication history.

Orders generated for Magento 2

Here’s how the generated M2 customers look:

Customers generated for M2

Fixing the Common Stock Issue

When generating many orders, you’ll likely run out of in-stock products quickly. To resolve this, run:
php bin/magento amasty:update_product_qty 200
This command sets all products to have 200 items in stock, saving you time on restocking manually.

 

Adding More Settings

If you need more variety or want to configure specific order attributes — such as status, payment method, or Magento save order behavior — open:
app/code/Amasty/Console/Console/Command/CreateOrdersCommand.php
You can customize parameters directly in this file.

Order generator additional settings

$_shippingMethod – the shipping method ID. For example, you can find the shipping method ID in the Cart Price rules: visit the page of editing a rule, go to the conditions tab, add a new condition, choose the shipping method, right click on the corresponding option and look for the ID like so:

Find the shipping method ID

$_paymentMethod – the payment method ID. You can find it by following the same steps as in the previous point for a shipping method:

Find the payment method ID

$_storeId – the store ID. You can find this ID in the browser address bar when you’re editing the corresponding store view:

How to find the store ID

$_emails – the customer emails array – in case you are willing to use some particular existing customers for order generation.

$_days – how old the order should be.
That's pretty much it!

October 18, 2016
October 24, 2016
October 11, 2016
Comments
Cristian
October 20, 2016
Hi, Seems is a problem with the archive. Can you check please.
Reply
Ksenia Dobreva
October 21, 2016
Hey Cristian, could you please inform me about the problem you're experiencing? Would love to help.
Ksenia Dobreva
October 24, 2016
Hey Cristian, thanks for reporting. I re-uploaded the fixed file. I also deleted your subscription, so now you can again subscribe and download the new file.
Franck
October 24, 2016
Hello, The version of this archive is wrong.
Reply
Ksenia Dobreva
October 24, 2016
Hey Franck, thanks for reporting. I re-uploaded the fixed file. I also deleted your subscription, so now you can again subscribe and download the new file.
Johnny
November 2, 2016
I have already submitted subscribe but no attachment in email.
Reply
Ksenia Dobreva
November 9, 2016
Hey Johnny, thanks for your interest. Please check your email, I've sent the files to you. Cheers!
Kirit Nariya
December 16, 2017
Hi Ksenia, when I try to create_order it throws this error Magento\Framework\Exception\LocalizedException] Product that you are trying to add is not available.
tony vu
February 9, 2017
Hi Ksenia Can I have the file to create order auto
Reply
Ksenia Dobreva
February 15, 2017
Hello there, you're welcome to subscribe to our newsletter above, and you'll get the files right away.
Muhammad Saeed
February 10, 2017
Thanks for the nice code. I've a suggestion for you. You guys used SQL query at the end of Amasty\Console\Console\Command\CreateOrdersCommand::createOrder() which is never recommended by the Magento standards. Please update that.
Reply
Ela
August 17, 2017
How Can I add guest user based order creation with this? this order generator and every other article on the internet that i found creates customer and places order. I want to place order as a guest user.
Reply
Naman
October 8, 2017
Magento\Framework\Exception\LocalizedException] Product that you are trying to add is not available.
Naman
October 7, 2017
Hi, when I try to create_order it throws this error Magento\Framework\Exception\LocalizedException] Product that you are trying to add is not available.
Reply
Kirti
December 15, 2017
Hi Naman I found same issue as you describe did you found solution of this error: "Magento\Framework\Exception\LocalizedException] Product that you are trying to add is not available."
Natalie
August 18, 2019
Hi, the subscribe link is broken.
Reply
Polina Litreyeva
August 23, 2019
Hello, Natalie. Thanks for your careful reading. We're sorry, but this form doesn't work because it is outdated. We apologize for the inconvenience.
Oliver
August 21, 2019
The button "subscribe" does not work! I got a 404-error-page
Reply
Polina Litreyeva
August 23, 2019
Hello, Oliver. Thanks for your careful reading. We're sorry, but this form doesn't work because it is outdated. We apologize for the inconvenience.
Leave your comment

Your email address will not be published

This blog was created with Amasty Blog Pro

This blog was created with Amasty Blog Pro