Skip to content

Commit 411ac16

Browse files
authored
Merge pull request #113 from woocommerce/add/product-type-arg
Add a "type" argument to the generate products command
2 parents 722b291 + 726179f commit 411ac16

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ WooCommerce Smooth Generator requires Composer and WP-CLI to function.
1717
Generate products based on the number of products parameter.
1818
- `wp wc generate products <nr of products>`
1919

20+
Generate products of the specified type. `simple` or `variable`.
21+
- `wp wc generate products <nr of products> --type=simple`
22+
2023
### Orders
2124

2225
Generate orders from existing products based on the number of orders parameter, customers will also be generated to mimic guest checkout.

includes/CLI.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function products( $args, $assoc_args ) {
4343
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating products', $amount );
4444

4545
for ( $i = 1; $i <= $amount; $i++ ) {
46-
Generator\Product::generate();
46+
Generator\Product::generate( true, $assoc_args );
4747
$progress->tick();
4848
}
4949

@@ -198,7 +198,27 @@ public static function coupons( $args, $assoc_args ) {
198198
}
199199
}
200200

201-
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ) );
201+
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ), array(
202+
'shortdesc' => 'Generate products.',
203+
'synopsis' => array(
204+
array(
205+
'name' => 'amount',
206+
'type' => 'positional',
207+
'description' => 'The number of products to generate.',
208+
'optional' => true,
209+
'default' => 10,
210+
),
211+
array(
212+
'name' => 'type',
213+
'type' => 'assoc',
214+
'description' => 'Specify one type of product to generate. Otherwise defaults to a mix.',
215+
'optional' => true,
216+
'options' => array( 'simple', 'variable' ),
217+
),
218+
),
219+
'longdesc' => "## EXAMPLES\n\nwc generate products 10\n\nwc generate products 20 --type=variable",
220+
) );
221+
202222
WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array(
203223
'synopsis' => array(
204224
array(
@@ -229,6 +249,7 @@ public static function coupons( $args, $assoc_args ) {
229249
),
230250
),
231251
) );
252+
232253
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );
233254

234255
WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array(

includes/Generator/Product.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,22 @@ protected static function init_faker() {
7676
/**
7777
* Return a new product.
7878
*
79-
* @param bool $save Save the object before returning or not.
79+
* @param bool $save Save the object before returning or not.
80+
* @param array $assoc_args Arguments passed via the CLI for additional customization.
8081
* @return \WC_Product The product object consisting of random data.
8182
*/
82-
public static function generate( $save = true ) {
83+
public static function generate( $save = true, $assoc_args = array() ) {
8384
self::init_faker();
8485

85-
// 20% chance of a variable product.
86-
$is_variable = self::$faker->boolean( 20 );
87-
88-
if ( $is_variable ) {
89-
$product = self::generate_variable_product();
90-
} else {
91-
$product = self::generate_simple_product();
86+
$type = self::get_product_type( $assoc_args );
87+
switch ( $type ) {
88+
case 'simple':
89+
default:
90+
$product = self::generate_simple_product();
91+
break;
92+
case 'variable':
93+
$product = self::generate_variable_product();
94+
break;
9295
}
9396

9497
if ( $product ) {
@@ -222,6 +225,30 @@ protected static function generate_attributes( $qty = 1, $maximum_terms = 10 ) {
222225
return $attributes;
223226
}
224227

228+
/**
229+
* Returns a product type to generate. If no type is specified, or an invalid type is specified,
230+
* a weighted random type is returned.
231+
*
232+
* @param array $assoc_args CLI arguments.
233+
* @return string A product type.
234+
*/
235+
protected static function get_product_type( array $assoc_args ) {
236+
$type = $assoc_args['type'] ?? null;
237+
$types = array(
238+
'simple',
239+
'variable',
240+
);
241+
242+
if ( ! is_null( $type ) && in_array( $type, $types, true ) ) {
243+
return $type;
244+
} else {
245+
return self::random_weighted_element( array(
246+
'simple' => 80,
247+
'variable' => 20,
248+
) );
249+
}
250+
}
251+
225252
/**
226253
* Generate a variable product and return it.
227254
*

0 commit comments

Comments
 (0)