Skip to content

Commit 7c998eb

Browse files
committed
Add method for using term cache during product generation
1 parent 786325b commit 7c998eb

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

includes/Generator/Product.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace WC\SmoothGenerator\Generator;
99

10+
use WC\SmoothGenerator\Util\RandomRuntimeCache;
11+
1012
/**
1113
* Product data generator.
1214
*/
1315
class Product extends Generator {
14-
1516
/**
1617
* Holds array of product IDs for generating relationships.
1718
*
@@ -139,6 +140,9 @@ public static function batch( $amount, array $args = array() ) {
139140
$product_ids[] = $product->get_id();
140141
}
141142

143+
// In case multiple batches are being run in one request, refresh the cache data.
144+
RandomRuntimeCache::reset();
145+
142146
return $product_ids;
143147
}
144148

@@ -310,8 +314,8 @@ protected static function generate_variable_product() {
310314
'upsell_ids' => self::get_existing_product_ids(),
311315
'cross_sell_ids' => self::get_existing_product_ids(),
312316
'image_id' => self::get_image(),
313-
'category_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_cat', $name ),
314-
'tag_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_tag', $name ),
317+
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
318+
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
315319
'gallery_image_ids' => $gallery,
316320
'reviews_allowed' => self::$faker->boolean(),
317321
'purchase_note' => self::$faker->boolean() ? self::$faker->text() : '',
@@ -405,8 +409,8 @@ protected static function generate_simple_product() {
405409
'menu_order' => self::$faker->numberBetween( 0, 10000 ),
406410
'virtual' => $is_virtual,
407411
'downloadable' => false,
408-
'category_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_cat', $name ),
409-
'tag_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_tag', $name ),
412+
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
413+
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
410414
'shipping_class_id' => 0,
411415
'image_id' => $image_id,
412416
'gallery_image_ids' => $gallery,
@@ -415,6 +419,46 @@ protected static function generate_simple_product() {
415419
return $product;
416420
}
417421

422+
/**
423+
* Get a number of random term IDs for a specific taxonomy.
424+
*
425+
* @param string $taxonomy The taxonomy to get terms for.
426+
* @param int $limit The number of term IDs to get.
427+
*
428+
* @return array
429+
*/
430+
protected static function get_term_ids( $taxonomy, $limit ) {
431+
if ( $limit <= 0 ) {
432+
return array();
433+
}
434+
435+
if ( ! RandomRuntimeCache::exists( $taxonomy ) ) {
436+
$args = array(
437+
'taxonomy' => $taxonomy,
438+
'number' => 20,
439+
'orderby' => 'count',
440+
'order' => 'ASC',
441+
'hide_empty' => false,
442+
'fields' => 'ids',
443+
);
444+
445+
if ( 'product_cat' === $taxonomy ) {
446+
$uncategorized = get_term_by( 'slug', 'uncategorized', 'product_cat' );
447+
if ( $uncategorized ) {
448+
$args['exclude'] = $uncategorized->term_id;
449+
}
450+
}
451+
452+
$term_ids = get_terms( $args );
453+
454+
RandomRuntimeCache::set( $taxonomy, $term_ids );
455+
}
456+
457+
RandomRuntimeCache::shuffle( $taxonomy );
458+
459+
return RandomRuntimeCache::get( $taxonomy, $limit );
460+
}
461+
418462
/**
419463
* Generate an image gallery.
420464
*

0 commit comments

Comments
 (0)