Skip to content

Commit 03e6a5d

Browse files
committed
Add type assoc arg for the products generator
1 parent 2bec9cb commit 03e6a5d

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

includes/CLI.php

Lines changed: 18 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

@@ -177,7 +177,22 @@ public static function coupons( $args, $assoc_args ) {
177177
}
178178
}
179179

180-
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ) );
180+
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ), array(
181+
'synopsis' => array(
182+
array(
183+
'name' => 'amount',
184+
'type' => 'positional',
185+
'optional' => true,
186+
'default' => 100,
187+
),
188+
array(
189+
'name' => 'type',
190+
'type' => 'assoc',
191+
'optional' => true,
192+
),
193+
),
194+
) );
195+
181196
WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array(
182197
'synopsis' => array(
183198
array(
@@ -208,6 +223,7 @@ public static function coupons( $args, $assoc_args ) {
208223
),
209224
),
210225
) );
226+
211227
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );
212228

213229
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)