Lately I’ve been working on using Google Sheets to create dashboards for WooCommerce Order data. I want to write about it here, but since I can’t show you the sensitive customer data that is in my client’s site, I need some test data.

If you just want a fix for getting some random new order data into your site then here’s a function for that:

function create_random_order_data() {

  $orderstatus = ['pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed'];
  $paytypes = [
    ['bacs', 'Direct Bank Transfer'],
    ['stripe', 'Credit Card'], 
    ['ppcp-gateway', 'Paypal'],
  ];

  $namedataurl = 'https://reqres.in/api/users/';
  $response = wp_remote_get( $namedataurl );
  $customers = json_decode($response['body'])->data;

  foreach ($customers as $customer ) {

    $randomaddressurl = 'https://random-data-api.com/api/address/random_address';
    $response = wp_remote_get( $randomaddressurl );
    $address = json_decode($response['body']);

      $custaddress = array(
        'first_name' => $customer->first_name,
        'last_name'  => $customer->last_name,
        'email'      => $customer->email,
        'phone'      => '0415000000',
        'address_1'  => $address->street_address,
        'address_2'  => '',
        'city'       => $address->city,
        'state'      => $address->state,
        'postcode'   => $address->postcode,
        'country'    => $address->country,
      );

    $order = wc_create_order();

    //https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
    $query = new WC_Product_Query( array(
        'limit' => rand(1,4),
    ) );
    $products = $query->get_products();

    foreach ($products as $prod) {
  // error_log(print_r( $prod, true ));
      $order->add_product( wc_get_product( $prod->get_id() ) );
    }

    $order->set_address( $custaddress, 'billing' );
    $order->set_address( $custaddress, 'shipping' );
    
    $payment_method = $paytypes[array_rand($paytypes)];
    $order->set_payment_method( $payment_method[0] );
    $order->set_payment_method_title( $payment_method[1] );

    $order->set_status( $orderstatus[array_rand($orderstatus)], 'Order is created programmatically' );

    $order->calculate_totals();
    $order->save();

  }
}

add_action( 'init', 'create_random_order_data' );

You can put this code in your functions.php file or in a custom plugin. Just remember that it’s going to run on every page refresh, so once you’ve used it, comment out the

add_action( 'init', 'create_random_order_data' );

line or your order data will spiral out of control.

How Random

Heads up! This is going to create you some very random data. It won’t be a lot like real customer data because, for example, normally you’ll get more processing, on-hold or completed orders than you will refunded and failed. Also in the real world, on-hold orders should correspond to Direct Bank Transfer payment methods, which this doesn’t. You could alter the function to make that happen though if you like.

If you want a breakdown of what’s going on here so that you can understand and possibly debug anything that’s gone wrong, read on!

How the Function is Working

If you’d like some reference points, I’ve modified a lot of the code in this Stack Overflow question to make this work.

Running WooCommerce Functions on a Hook

Firstly, you need to run this function on a hook, because if you just run it from the functions file, WooCommerce won’t be loaded and available and you’ll get an error when you try to call its functions. I’m using the init hook here, which works fine:

add_action( 'init', 'create_random_order_data' );

Getting Fake Customer and Address data from APIs

To make sample order data you need fake name and address data. Here I’m using two publicly accessible APIs that will give me that data. I’m using WordPress’s wp_remote_get function to call them.

The API at https://reqres.in/api/users/ is giving me six user names and I’m looping through them to create orders:

$namedataurl = 'https://reqres.in/api/users/';
$response = wp_remote_get( $namedataurl );
$customers = json_decode($response['body'])->data;

foreach ($customers as $customer ) {
  ...
}

Six users is not a lot, so depending on whether you want your data to be more varied you can try to find another API that will give you that – but for now, the framework is here and you can just swap in another API if you want.

To get the address data I’m using this API at https://random-data-api.com/api/address/random_address which is truely random and seems like it will spit odd address data out at me forever! Within the customer loop I’m getting an address and setting all the customer details:

$randomaddressurl = 'https://random-data-api.com/api/address/random_address';
$response = wp_remote_get( $randomaddressurl );
$address = json_decode($response['body']);

$custaddress = array(
 'first_name' => $customer->first_name,
 'last_name'  => $customer->last_name,
 'email'      => $customer->email,
 'phone'      => '0415000000',
 'address_1'  => $address->street_address,
 'address_2'  => '',
 'city'       => $address->city,
 'state'      => $address->state,
 'postcode'   => $address->postcode,
 'country'    => $address->country,
);

The phone number is hardcoded, but again, you can change this if you want to swap in another API for phone numbers, of which there is one in the list here in the Random Data API.

Creating the order

Next, and still in the customer loop, I’m creating the order with some more really random data points:

$order = wc_create_order();

//https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
$query = new WC_Product_Query( array(
  'limit' => rand(1,4),
) );

$products = $query->get_products();

foreach ($products as $prod) {
  // error_log(print_r( $prod, true ));
  $order->add_product( $prod ) );
}

$order->set_address( $custaddress, 'billing' );
$order->set_address( $custaddress, 'shipping' );
    
$payment_method = $paytypes[array_rand($paytypes)];
$order->set_payment_method( $payment_method[0] );
$order->set_payment_method_title( $payment_method[1] );

$order->set_status( $orderstatus[array_rand($orderstatus)], 'Order is created programmatically' );

$order->calculate_totals();
$order->save();

I’m using wc_create_order to create my order. This is simple and works out of the box as long as you remember you need to call it on a hook (in our case being the init hook).

The wc_create_order call passes back an order object that has a series of methods such as add_product, set_address, set_payment_method, set_status etc, that you can use to very simply set data on the order .

Getting Products to Attach to the Order

I’m then using the WC_Product_Query object to to get up to 4 randomly selected products to attach to the order:

$query = new WC_Product_Query( array(
  'limit' => rand(1,4),
) );

$products = $query->get_products();

foreach ($products as $prod) {
  $order->add_product( wc_get_product( $prod->get_id() ) ) );
}

Order Status and Payment Method

I’m randomly accessing two arrays I’ve created at the top of the function to set other details such as order status and payment method:

$orderstatus = ['pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed'];
$paytypes = [
  ['bacs', 'Direct Bank Transfer'],
  ['stripe', 'Credit Card'], 
  ['ppcp-gateway', 'Paypal'],
];

I’m using PHP’s array_rand function to get random values out of them and pass them into WooCommerce’s set status and set payment details functions:

$payment_method = $paytypes[array_rand($paytypes)];
$order->set_payment_method( $payment_method[0] );
$order->set_payment_method_title( $payment_method[1] );

$order->set_status( $orderstatus[array_rand($orderstatus)], 'Order is created programmatically' );

Final Thoughts

This data might not be perfect for your use case, however you can tamper with it to your heart’s content to get something closer to what you need.

So that’s it. As ever, if you notice bugs or things that could be better in this function, please let me know in the comments!

Hey there – thanks for reading!
Have I helped you? If so, could you…
Getting Sample Order Data into WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *