@@ -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