diff --git a/includes/Generator/Customer.php b/includes/Generator/Customer.php index e3f5b47..c0355c0 100644 --- a/includes/Generator/Customer.php +++ b/includes/Generator/Customer.php @@ -31,18 +31,6 @@ public static function generate( $save = true ) { $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']; - } /*COMPANY*/ $company_variations = array( 'B2B', 'C2C', 'C2B', 'B2C' ); @@ -73,13 +61,37 @@ public static function generate( $save = true ) { default: break; } + + + /*PERSON*/ + if(strlen($company['billing']['company_name']) >= 1 && (bool) wp_rand( 0, 1 )){ + $person['billing']['firstname'] = ''; + $person['billing']['lastname'] = ''; + } else { + $person['billing']['firstname'] = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) ); + $person['billing']['lastname'] = self::$faker->lastName(); + } + + if(strlen($company['shipping']['company_name']) >= 1 && (bool) wp_rand( 0, 1 )){ + if(strlen($company['billing']['company_name']) >= 1 && (bool) wp_rand( 0, 1 )){ + $person['shipping']['firstname'] = $person['billing']['firstname']; + $person['shipping']['lastname'] = $person['billing']['lastname']; + } else { + $person['shipping']['firstname'] = ''; + $person['shipping']['lastname'] = ''; + } + } else { + $person['shipping']['firstname'] = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) ); + $person['shipping']['lastname'] = self::$faker->lastName(); + } + /*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']['country'] = Customer::getAllowedCountry(); $address['billing']['phone'] = self::$faker->e164PhoneNumber(); $address['billing']['email'] = $email; @@ -90,7 +102,7 @@ public static function generate( $save = true ) { $address['shipping']['city'] = self::$faker->city(); $address['shipping']['state'] = self::$faker->stateAbbr(); $address['shipping']['postcode'] = self::$faker->postcode(); - $address['shipping']['country'] = self::$faker->countryCode(); + $address['shipping']['country'] = Customer::getAllowedCountry(); } else { $address['shipping']['address0'] = $address['billing']['address0']; $address['shipping']['address1'] = $address['billing']['address1']; @@ -144,6 +156,28 @@ public static function generate( $save = true ) { return $customer; } + /** + * returns allowed country based on the woocommerce settings + */ + public static function getAllowedCountry(){ + $allowedCountries = get_option('woocommerce_allowed_countries'); + $allowedOnes = get_option('woocommerce_specific_allowed_countries'); + $restrictedOnes = get_option('woocommerce_all_except_countries'); + + if ($allowedCountries == 'specific'){ + $country = self::$faker->randomElements( $allowedOnes, $count = 1 ); + $country = $country[0]; + } else if ($allowedCountries == 'all_except'){ + do{ + $country = self::$faker->countryCode(); + } while (in_array($country,$restrictedOnes)); + } else { + $country = self::$faker->countryCode(); + } + + return $country; + } + /** * Disable sending WooCommerce emails when generating objects.