Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected static function init_faker() {
if ( ! self::$faker ) {
self::$faker = \Faker\Factory::create( 'en_US' );
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Device( self::$faker ) );
}
}

Expand Down
11 changes: 11 additions & 0 deletions includes/Generator/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static function batch( $amount, array $args = array() ) {
// In case multiple batches are being run in one request, refresh the cache data.
RandomRuntimeCache::clear( 'product_cat' );
RandomRuntimeCache::clear( 'product_tag' );
RandomRuntimeCache::clear( 'product_brand' );

return $product_ids;
}
Expand Down Expand Up @@ -314,6 +315,7 @@ protected static function generate_variable_product() {
'image_id' => self::get_image(),
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
'brand_ids' => self::get_term_ids( 'product_brand', 1 ),
'gallery_image_ids' => $gallery,
'reviews_allowed' => self::$faker->boolean(),
'purchase_note' => self::$faker->boolean() ? self::$faker->text() : '',
Expand Down Expand Up @@ -409,6 +411,7 @@ protected static function generate_simple_product() {
'downloadable' => false,
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
'brand_ids' => self::get_term_ids( 'product_brand', 1 ),
'shipping_class_id' => 0,
'image_id' => $image_id,
'gallery_image_ids' => $gallery,
Expand All @@ -431,14 +434,17 @@ protected static function maybe_generate_terms( int $product_amount ): void {
$cats = 5;
$cat_depth = 1;
$tags = 10;
$brands = 5;
} elseif ( $product_amount < 50 ) {
$cats = 10;
$cat_depth = 2;
$tags = 20;
$brands = 10;
} else {
$cats = 20;
$cat_depth = 3;
$tags = 40;
$brands = 10;
}

$existing_cats = count( self::get_term_ids( 'product_cat', $cats ) );
Expand All @@ -450,6 +456,11 @@ protected static function maybe_generate_terms( int $product_amount ): void {
if ( $existing_tags < $tags ) {
Term::batch( $tags - $existing_tags, 'product_tag' );
}

$existing_brands = count( self::get_term_ids( 'product_brand', $brands ) );
if ( $existing_brands < $brands ) {
Term::batch( $brands - $existing_brands, 'product_brand' );
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion includes/Generator/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static function generate( $save = true, string $taxonomy = 'product_cat',

parent::maybe_initialize_generators();

if ( $taxonomy_obj->hierarchical ) {
if ( $taxonomy_obj->hierarchical && 'product_brand' === $taxonomy ) {
$term_name = ucwords( self::$faker->department( 1 ) );
} elseif ( $taxonomy_obj->hierarchical ) {
$term_name = ucwords( self::$faker->department( 3 ) );
} else {
$term_name = self::random_weighted_element( array(
Expand Down