2015-02-05



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.

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 here.

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:

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

Download order/customer generation code for free!

Subscribe to Amasty blog updates and download the files at once:

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

The post Creating Magento order programmatically appeared first on Amasty Blog.

Show more