Skip to content

Commit cb90963

Browse files
authored
Merge pull request #46 from shiki/add/order-status-arg
Add --status argument for Orders
2 parents 656e40a + 99e58ed commit cb90963

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Generate orders with random dates between `--date-start` and the current date.
3030
Generate orders with random dates between `--date-start` and `--date-end`.
3131
- `wp wc generate orders <nr of orders> --date-start=2018-04-01 --date-end=2018-04-24`
3232

33+
Generate orders with a specific status.
34+
- `wp wc generate orders <nr of orders> --status=completed`
35+
3336
You may wish to disable emails if creating a large number of orders as this will trigger emails. To block all emails from your site, install a plugin like [Disable Emails](https://wordpress.org/plugins/disable-emails/).
3437

3538
### Customers

includes/CLI.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,18 @@ public static function orders( $args, $assoc_args ) {
8181
$amount = 100;
8282
}
8383

84+
if ( ! empty( $assoc_args['status'] ) ) {
85+
$status = $assoc_args['status'];
86+
if ( ! wc_is_order_status( 'wc-' . $status ) ) {
87+
WP_CLI::log( "The argument \"$status\" is not a valid order status." );
88+
return;
89+
}
90+
}
91+
8492
if ( $amount > 0 ) {
85-
$progress = \WP_CLI\Utils\make_progress_bar('Generating orders', $amount);
86-
for ($i = 1; $i <= $amount; $i++) {
87-
Generator\Order::generate(true, $assoc_args);
93+
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );
94+
for ( $i = 1; $i <= $amount; $i++ ) {
95+
Generator\Order::generate( true, $assoc_args );
8896
$progress->tick();
8997
}
9098
$progress->finish();
@@ -128,7 +136,7 @@ public static function customers( $args, $assoc_args ) {
128136
'name' => 'amount',
129137
'type' => 'positional',
130138
'optional' => true,
131-
'default' => 100
139+
'default' => 100,
132140
),
133141
array(
134142
'name' => 'date-start',
@@ -140,6 +148,11 @@ public static function customers( $args, $assoc_args ) {
140148
'type' => 'assoc',
141149
'optional' => true,
142150
),
151+
array(
152+
'name' => 'status',
153+
'type' => 'assoc',
154+
'optional' => true,
155+
),
143156
),
144157
) );
145158
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );

includes/Generator/Order.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
5858
$order->set_shipping_postcode( $customer->get_shipping_postcode() );
5959
$order->set_shipping_state( $customer->get_shipping_state() );
6060
$order->set_shipping_country( $customer->get_shipping_country() );
61-
$order->set_status( self::random_weighted_element( array(
62-
'completed' => 70,
63-
'processing' => 15,
64-
'on-hold' => 5,
65-
'failed' => 10,
66-
) ) );
61+
$order->set_status( self::get_status( $assoc_args ) );
6762
$order->calculate_totals( true );
6863

6964
$date = self::get_date_created( $assoc_args );
@@ -129,6 +124,26 @@ protected static function get_date_created( $assoc_args ) {
129124
return $dates[ array_rand( $dates ) ];
130125
}
131126

127+
/**
128+
* Returns a status to use as the order's status. If no status argument has been passed, this will
129+
* return a random status.
130+
*
131+
* @param array $assoc_args CLI arguments.
132+
* @return string An order status.
133+
*/
134+
private static function get_status( $assoc_args ) {
135+
if ( ! empty( $assoc_args['status'] ) ) {
136+
return $assoc_args['status'];
137+
} else {
138+
return self::random_weighted_element( array(
139+
'completed' => 70,
140+
'processing' => 15,
141+
'on-hold' => 5,
142+
'failed' => 10,
143+
) );
144+
}
145+
}
146+
132147
/**
133148
* Get random products selected from existing products.
134149
*

0 commit comments

Comments
 (0)