diff --git a/includes/CLI.php b/includes/CLI.php index 5b0ca91..111ff06 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -121,10 +121,16 @@ public static function orders( $args, $assoc_args ) { public static function customers( $args, $assoc_args ) { list( $amount ) = $args; + $domain = ''; + + if ( ! empty( $assoc_args['email-domain'] ) ) { + $domain = $assoc_args['email-domain']; + } + static::disable_emails(); $progress = \WP_CLI\Utils\make_progress_bar( 'Generating customers', $amount ); for ( $i = 1; $i <= $amount; $i++ ) { - Generator\Customer::generate(); + Generator\Customer::generate( true, $domain ); $progress->tick(); } $progress->finish(); @@ -242,7 +248,21 @@ public static function coupons( $args, $assoc_args ) { ), ), ) ); -WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) ); +WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ), array( + 'synopsis' => array( + array( + 'name' => 'amount', + 'type' => 'positional', + 'optional' => true, + 'default' => 100, + ), + array( + 'name' => 'email-domain', + 'type' => 'assoc', + 'optional' => true, + ), + ), +) ); WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array( 'synopsis' => array( diff --git a/includes/Generator/Customer.php b/includes/Generator/Customer.php index 86ae7a7..f14f21d 100644 --- a/includes/Generator/Customer.php +++ b/includes/Generator/Customer.php @@ -15,23 +15,23 @@ class Customer extends Generator { /** * Return a new customer. * - * @param bool $save Save the object before returning or not. + * @param bool $save Save the object before returning or not. + * @param string $emailDomain An optional domain to be used for all customers' email addresses. * @return \WC_Customer Customer object with data populated. */ - public static function generate( $save = true ) { + public static function generate( $save = true, $emailDomain = '' ) { self::init_faker(); - // Make sure a unique username and e-mail are used. - do { - $username = self::$faker->userName(); - } while ( username_exists( $username ) ); + $safeEmailDomain = $emailDomain ? $emailDomain : self::$faker->safeEmailDomain(); + // Make sure a unique username and e-mail are used. do { - $email = self::$faker->safeEmail(); - } while ( email_exists( $email ) ); + $firstname = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) ); + $lastname = self::$faker->lastName(); + $username = strtolower( "$firstname.$lastname" ); + $email = "$username@$safeEmailDomain"; + } while ( username_exists( $username ) || email_exists( $email ) ); - $firstname = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) ); - $lastname = self::$faker->lastName(); $company = self::$faker->company(); $address1 = self::$faker->buildingNumber() . ' ' . self::$faker->streetName(); $address2 = self::$faker->streetAddress();