Creating Magento order programmatically

If you’re working with Magento , most probably you’ll face a situation when you need existing fake orders, and there are none at the moment. It’s not relevant to create orders (or customers) using the Magento interface, as you can do it programmatically, which takes less time and effort.

This action can be of great help if you need to create a number of orders quickly to test your store features.

Seeking the way to export orders with all the relevant data? Try out our Magento Export Orders extension.

Now, in this article I’ll explain now to create Magento orders programmatically, also adding information on creating customers programmatically, as these two actions are closely connected.

Creating Magento order programmatically

OrderGenerator.php is a class for orders generation – download it at the end of the article.

Orders are created with createOrder($products) method. You can use an array, which specifies the products you want to add to the order, as an argument.

Parameters format corresponds to $_POST variable values format when adding products to cart using the front end.

Here’s an example:

		'product' => 418,
		'super_attribute' => array(
		    92 => 26,
		    180 => 79
		'qty' => 1
    Adding a configurable product:
    Product is the product ID
    super_attribute  is the value of configurable product options
    qty is the quantity of products we’re going to add
		'product' => 553,
		'options' => array(
		    11 => 8
		'qty' => 2
Add a simple product with custom options:    
    Options is a custom options value
		'product' => 'rand',
		'min' => 1,
		'max' => 3
    Add random products to cart
    product = rand
    min, max are minimum and maximum product quantities we’re adding to cart 

Let’s set shipping and billing methods before creating orders:



Also we can point out the customer which the order is assigned to:


You can pass customer ID, Mage_Customer_Model_Customer object or OrderGenerator::CUSTOMER_RANDOM constant to the function.

If you use OrderGenerator::CUSTOMER_RANDOM, the generator will pick a random one from the existing customers.

Bonus: creating Magento customers programmatically

Creating customers in Magento goes close to generating orders, as you basically can’t have an order without a customer.

CustomerGenerator.php is a class to create customers programmatically (download it here). A customer is created using createCustomer method. You can set the customer data using the same format as in the $_defaultData internal variable.

There is an {id} placeholder in field values to be changed with the ID of the created customer.

How to use it?

You’ll also find examples.php in the archive you can download for free at the end of the article.

The file contains examples of using order and customer generators for Magento.

create_order_with_custom_products() is a simple example of creating a new order.

create_random_orders($qty) creates $qty of orders, each of them is created from a random customer existing in Magento at the moment.

create_customers_with_order($qty) creates $qty of customers, plus an order per each customer.

create_customers_and_random_orders($customersQty, $ordersQty) creates $customersQty of customers and $ordersQty of orders. For every order the generator chooses a random customer from the newly created ones.

Download the files to create Magento orders/customers programmatically

Want to test everything yourself? Subscribe to Amasty newsletter to get the files. Inside you’ll find:

  • Magento order generation code
  • Magento customer generation code
  • Examples of using the generators

Do you have any questions on creating Magento orders or customers programmatically? Ask in comments.