Skip to content

Improvements to Customer generator #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2024
Merged
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
18 changes: 16 additions & 2 deletions includes/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function () use ( $progress ) {
while ( $remaining_amount > 0 ) {
$batch = min( $remaining_amount, Generator\Customer::MAX_BATCH_SIZE );

$result = Generator\Customer::batch( $batch );
$result = Generator\Customer::batch( $batch, $assoc_args );

if ( is_wp_error( $result ) ) {
WP_CLI::error( $result );
Expand Down Expand Up @@ -337,8 +337,22 @@ function () use ( $progress ) {
'optional' => true,
'default' => 10,
),
array(
'name' => 'country',
'type' => 'assoc',
'description' => 'The ISO 3166-1 alpha-2 country code to use for localizing the customer data. If none is specified, any country in the "Selling location(s)" setting may be used.',
'optional' => true,
'default' => '',
),
array(
'name' => 'type',
'type' => 'assoc',
'description' => 'The type of customer to generate data for. If none is specified, it will be a 70% person, 30% company mix.',
'optional' => true,
'options' => array( 'company', 'person' ),
),
),
'longdesc' => "## EXAMPLES\n\nwc generate customers 10",
'longdesc' => "## EXAMPLES\n\nwc generate customers 10\n\nwc generate customers --country=ES --type=company",
) );

WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array(
Expand Down
201 changes: 90 additions & 111 deletions includes/Generator/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,130 +11,105 @@
* Customer data generator.
*/
class Customer extends Generator {

/**
* Return a new customer.
*
* @param bool $save Save the object before returning or not.
* @return \WC_Customer Customer object with data populated.
* @param bool $save Save the object before returning or not.
* @param array $assoc_args Arguments passed via the CLI for additional customization.
*
* @return \WC_Customer|\WP_Error Customer object with data populated.
*/
public static function generate( $save = true ) {
public static function generate( $save = true, array $assoc_args = array() ) {
self::init_faker();

// Make sure a unique username and e-mail are used.
do {
$username = self::$faker->userName();
} while ( username_exists( $username ) );

do {
$email = self::$faker->safeEmail();
} while ( email_exists( $email ) );

/*PERSON*/
$person['billing']['firstname'] = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) );
$person['billing']['lastname'] = self::$faker->lastName();

// 50% chance
if ( (bool) wp_rand( 0, 1 ) ) {
$person['shipping']['firstname'] = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) );
$person['shipping']['lastname'] = self::$faker->lastName();
} else {
$person['shipping']['firstname'] = $person['billing']['firstname'];
$person['shipping']['lastname'] = $person['billing']['lastname'];
$args = filter_var_array(
$assoc_args,
array(
'country' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array(
'regexp' => '/^[A-Za-z]{2}$/',
'default' => '',
),
),
'type' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array(
'regexp' => '/^(company|person)$/',
),
),
)
);

list( 'country' => $country, 'type' => $type ) = $args;

$country = CustomerInfo::get_valid_country_code( $country );
if ( is_wp_error( $country ) ) {
return $country;
}

/*COMPANY*/
$company_variations = array( 'B2B', 'C2C', 'C2B', 'B2C' );
$relationType = self::$faker->randomElements( $company_variations, $count = 1 );
if ( ! $type ) {
$type = self::$faker->randomDigit() < 7 ? 'person' : 'company'; // 70% person, 30% company.
}

switch ( $relationType[0] ) {
case 'B2B':
$company['billing']['company_name'] = self::$faker->company();
if ( self::$faker->randomFloat( 0, 0, 1 ) == 1 ) {
$company['shipping']['company_name'] = self::$faker->company();
} else {
$company['shipping']['company_name'] = $company['billing']['company_name'];
}
$keys_for_address = array( 'email' );

break;
case 'C2C':
$company['billing']['company_name'] = '';
$company['shipping']['company_name'] = '';
break;
case 'B2C':
$company['billing']['company_name'] = self::$faker->company();
$company['shipping']['company_name'] = '';
break;
case 'C2B':
$company['billing']['company_name'] = '';
$company['shipping']['company_name'] = self::$faker->company();
break;
$customer_data = array(
'role' => 'customer',
);
switch ( $type ) {
case 'person':
default:
$customer_data = array_merge( $customer_data, CustomerInfo::generate_person( $country ) );
$other_customer_data = CustomerInfo::generate_person( $country );
$keys_for_address[] = 'first_name';
$keys_for_address[] = 'last_name';
break;

case 'company':
$customer_data = array_merge( $customer_data, CustomerInfo::generate_company( $country ) );
$other_customer_data = CustomerInfo::generate_company( $country );
$keys_for_address[] = 'company';
break;
}
/*ADDRESS*/
$address['billing']['address0'] = self::$faker->buildingNumber() . ' ' . self::$faker->streetName();
$address['billing']['address1'] = self::$faker->streetAddress();
$address['billing']['city'] = self::$faker->city();
$address['billing']['state'] = self::$faker->stateAbbr();
$address['billing']['postcode'] = self::$faker->postcode();
$address['billing']['country'] = self::$faker->countryCode();
$address['billing']['phone'] = self::$faker->e164PhoneNumber();
$address['billing']['email'] = $email;

// 50% chance
if ( (bool) wp_rand( 0, 1 ) ) {
$address['shipping']['address0'] = self::$faker->buildingNumber() . ' ' . self::$faker->streetName();
$address['shipping']['address1'] = self::$faker->streetAddress();
$address['shipping']['city'] = self::$faker->city();
$address['shipping']['state'] = self::$faker->stateAbbr();
$address['shipping']['postcode'] = self::$faker->postcode();
$address['shipping']['country'] = self::$faker->countryCode();
} else {
$address['shipping']['address0'] = $address['billing']['address0'];
$address['shipping']['address1'] = $address['billing']['address1'];
$address['shipping']['city'] = $address['billing']['city'];
$address['shipping']['state'] = $address['billing']['state'];
$address['shipping']['postcode'] = $address['billing']['postcode'];
$address['shipping']['country'] = $address['billing']['country'];

$customer_data['billing'] = array_merge(
CustomerInfo::generate_address( $country ),
array_intersect_key( $customer_data, array_fill_keys( $keys_for_address, '' ) )
);

$has_shipping = self::$faker->randomDigit() < 5;
if ( $has_shipping ) {
$same_shipping = self::$faker->randomDigit() < 5;
if ( $same_shipping ) {
$customer_data['shipping'] = $customer_data['billing'];
} else {
$customer_data['shipping'] = array_merge(
CustomerInfo::generate_address( $country ),
array_intersect_key( $other_customer_data, array_fill_keys( $keys_for_address, '' ) )
);
}
}

unset( $customer_data['company'], $customer_data['shipping']['email'] );

foreach ( array( 'billing', 'shipping' ) as $address_type ) {
if ( isset( $customer_data[ $address_type ] ) ) {
$address_data = array_combine(
array_map(
fn( $line ) => $address_type . '_' . $line,
array_keys( $customer_data[ $address_type ] )
),
array_values( $customer_data[ $address_type ] )
);

$customer_data = array_merge( $customer_data, $address_data );
unset( $customer_data[ $address_type ] );
}
}

$customer = new \WC_Customer();

$customer->set_props(
array(
'date_created' => null,
'date_modified' => null,
'email' => $email,
'first_name' => $person['billing']['firstname'],
'last_name' => $person['billing']['lastname'],
'display_name' => $person['billing']['firstname'],
'role' => 'customer',
'username' => $username,
'password' => self::$faker->password(),
'billing_first_name' => $person['billing']['firstname'],
'billing_last_name' => $person['billing']['lastname'],
'billing_company' => $company['billing']['company_name'],
'billing_address_0' => $address['billing']['address0'],
'billing_address_1' => $address['billing']['address1'],
'billing_city' => $address['billing']['city'],
'billing_state' => $address['billing']['state'],
'billing_postcode' => $address['billing']['postcode'],
'billing_country' => $address['billing']['country'],
'billing_email' => $address['billing']['email'],
'billing_phone' => $address['billing']['phone'],
'shipping_first_name' => $person['shipping']['firstname'],
'shipping_last_name' => $person['shipping']['lastname'],
'shipping_company' => $company['shipping']['company_name'],
'shipping_address_0' => $address['shipping']['address0'],
'shipping_address_1' => $address['shipping']['address1'],
'shipping_city' => $address['shipping']['city'],
'shipping_state' => $address['shipping']['state'],
'shipping_postcode' => $address['shipping']['postcode'],
'shipping_country' => $address['shipping']['country'],
'is_paying_customer' => false,
)
);
$customer->set_props( $customer_data );

if ( $save ) {
$customer->save();
Expand All @@ -155,11 +130,12 @@ public static function generate( $save = true ) {
/**
* Create multiple customers.
*
* @param int $amount The number of customers to create.
* @param int $amount The number of customers to create.
* @param array $args Additional args for customer creation.
*
* @return int[]|\WP_Error
*/
public static function batch( $amount ) {
public static function batch( $amount, array $args = array() ) {
$amount = self::validate_batch_amount( $amount );
if ( is_wp_error( $amount ) ) {
return $amount;
Expand All @@ -168,7 +144,10 @@ public static function batch( $amount ) {
$customer_ids = array();

for ( $i = 1; $i <= $amount; $i++ ) {
$customer = self::generate( true );
$customer = self::generate( true, $args );
if ( is_wp_error( $customer ) ) {
return $customer;
}
$customer_ids[] = $customer->get_id();
}

Expand Down
Loading