Skip to content

Commit 0b47467

Browse files
committed
Add cli flag use-existing-terms
1 parent 7b4857c commit 0b47467

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

includes/CLI.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function () use ( $progress ) {
263263

264264
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ), array(
265265
'shortdesc' => 'Generate products.',
266-
'synopsis' => array(
266+
'synopsis' => array(
267267
array(
268268
'name' => 'amount',
269269
'type' => 'positional',
@@ -278,8 +278,14 @@ function () use ( $progress ) {
278278
'optional' => true,
279279
'options' => array( 'simple', 'variable' ),
280280
),
281+
array(
282+
'name' => 'use-existing-terms',
283+
'type' => 'flag',
284+
'description' => 'Only apply existing categories and tags to products, rather than generating new ones.',
285+
'optional' => true,
286+
),
281287
),
282-
'longdesc' => "## EXAMPLES\n\nwc generate products 10\n\nwc generate products 20 --type=variable",
288+
'longdesc' => "## EXAMPLES\n\nwc generate products 10\n\nwc generate products 20 --type=variable --use-existing-terms",
283289
) );
284290

285291
WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array(

includes/Generator/Product.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public static function batch( $amount, array $args = array() ) {
133133
return $amount;
134134
}
135135

136+
$use_existing_terms = ! empty( $args['use-existing-terms'] );
137+
if ( ! $use_existing_terms ) {
138+
self::maybe_generate_terms( $amount );
139+
}
140+
136141
$product_ids = array();
137142

138143
for ( $i = 1; $i <= $amount; $i ++ ) {
@@ -419,6 +424,41 @@ protected static function generate_simple_product() {
419424
return $product;
420425
}
421426

427+
/**
428+
* Maybe generate a number of terms for use with products, if there aren't enough existing terms.
429+
*
430+
* Number of terms is determined by the number of products that will be generated.
431+
*
432+
* @param int $product_amount The number of products that will be generated.
433+
*
434+
* @return void
435+
*/
436+
protected static function maybe_generate_terms( int $product_amount ): void {
437+
if ( $product_amount < 10 ) {
438+
$cats = 5;
439+
$cat_depth = 1;
440+
$tags = 10;
441+
} elseif ( $product_amount < 50 ) {
442+
$cats = 10;
443+
$cat_depth = 2;
444+
$tags = 20;
445+
} else {
446+
$cats = 20;
447+
$cat_depth = 3;
448+
$tags = 40;
449+
}
450+
451+
$existing_cats = count( self::get_term_ids( 'product_cat', $cats ) );
452+
if ( $existing_cats < $cats ) {
453+
Term::batch( $cats - $existing_cats, 'product_cat', array( 'max-depth' => $cat_depth ) );
454+
}
455+
456+
$existing_tags = count( self::get_term_ids( 'product_tag', $tags ) );
457+
if ( $existing_tags < $tags ) {
458+
Term::batch( $tags - $existing_tags, 'product_tag' );
459+
}
460+
}
461+
422462
/**
423463
* Get a number of random term IDs for a specific taxonomy.
424464
*
@@ -427,7 +467,7 @@ protected static function generate_simple_product() {
427467
*
428468
* @return array
429469
*/
430-
protected static function get_term_ids( $taxonomy, $limit ) {
470+
protected static function get_term_ids( string $taxonomy, int $limit ): array {
431471
if ( $limit <= 0 ) {
432472
return array();
433473
}

0 commit comments

Comments
 (0)