Creating Magento 2 orders programmatically

Table of Content

Creating Magento 2 orders programmatically
Posted in: Magento 2 Guides

If you are a Magento 2 developer, some day and not even once you’ll need to create a certain amount of Magento 2 orders, and preferably programmatically, as manual creating of a bigger number of orders can be time consuming and simply inconvenient.

I created this Magento 2 order generator for the needs of Amasty team and decided to share it with you so you can set Magento 2 payment method programmatically.

Note: Seeking the way to export Magento 2/ Magento 1 orders with all the related data, try out our same-titled extensions.

I made this generator because we needed a faster creation of orders tool for testing and debugging purposes. For example, I personally use it to check how statistics work, how the extensions is dealing with the big amounts of data, and for other particular reasons.

You can download the generator at the end of the article.

Note: We’re providing this tool for free and as is, and we do not bear any responsibility for your actions concerning this tool. If you don’t understand what’s written in the article, please leave this to a specialist.


Creating Magento 2 order programmatically

Magento 2 lets you create the console extensions, and I chose this type because, basically, adding any interface for this task would be unnecessary.

How to install the Magento 2 order generator

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

  1. Copy the extension files to the default catalog.
  2. Go to the console, the default catalog (check this additionally because the console might go to some other catalog by default) and run the php bin/magento setup:upgrade command.

How to run the M2 order generator + additional feature: generating customers

After you have installed the extension, you can generate some orders. This order generator can also generate customers, but only along with the orders. You can’t generate just customers with this tool.

Now, to create the orders and customers with the tool, run the console command php bin/magento amasty:create_orders 5 3, where 5 is the number of orders that should be generated for each customer, and 3 is the number of customers for which the orders will be generated.

So you can set your own numbers here. Also, you can skip specifying the customers number, and the tool will generate the orders for a single customer automatically.

Also, please keep in mind that the tool needs time to generate the necessary amount of orders and customers.

When the tool generates a big amount of orders for a single customer, the generation takes more time than usual. I’m still not completely sure about the source of this effect, but looks like it’s also happening when creating a big amount of orders for a single customer without any script. We’ll investigate this issue further.

Here’s the example of running this order generator from the console:

Magento 2 order generation example

If you specified a bigger number of orders or customers, say, 200, and have already started the generation but for some reason you need to stop it, you can interrupt the execution with CTRL+C – a standard shortcut for breaking any command in Linux.

Here’s how the generated Magento 2 orders look like:

Orders generated for Magento 2

Here’s how the generated M2 customers look like:

Customers generated for M2

Fixing the common stock issue

When you’re generating a lot of orders, you can run with in stock products pretty fast. To save your time, just run the php bin/magento amasty:update_product_qty 200 command, where 200 is the number of in stock items which will be set for all the products. So, all of your products will be in stock again with 200 items available for each product.

Adding more settings

If you need more diversity or some specific properties for the generated orders, go to the app/code/Amasty/Console/Console/Command/CreateOrdersCommand.php file and set the necessary parameters right there.

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 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 if you are willing to use some particular existing customers for order generation.

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

Hope this tool will save you a couple of hours =)

If you’re looking for the way of generating orders for Magento 1, follow this link.

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

© 2009-2024 Amasty. All Rights Reserved.