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();
$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->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' );