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.
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:
- Copy the extension files to the default catalog.
- 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:
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:
Here’s how the generated M2 customers look like:
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.
$_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:
$_paymentMethod – the payment method ID. You can find it following the same steps as in the previous point for a shipping method:
$_storeId – the store ID. You can find this ID in the browser address bar when you’re editing the corresponding store view:
$_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.