Skip to content

Commit 6d68985

Browse files
committed
Handle invalid country codes
1 parent 646aa18 commit 6d68985

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

includes/Generator/Customer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Customer extends Generator {
1717
* @param bool $save Save the object before returning or not.
1818
* @param array $assoc_args Arguments passed via the CLI for additional customization.
1919
*
20-
* @return \WC_Customer Customer object with data populated.
20+
* @return \WC_Customer|\WP_Error Customer object with data populated.
2121
*/
2222
public static function generate( $save = true, array $assoc_args = array() ) {
2323
self::init_faker();
@@ -43,6 +43,11 @@ public static function generate( $save = true, array $assoc_args = array() ) {
4343

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

46+
$country = CustomerInfo::get_valid_country_code( $country );
47+
if ( is_wp_error( $country ) ) {
48+
return $country;
49+
}
50+
4651
if ( ! $type ) {
4752
$type = self::$faker->randomDigit() < 7 ? 'person' : 'company'; // 70% person, 30% company.
4853
}
@@ -140,6 +145,9 @@ public static function batch( $amount, array $args = array() ) {
140145

141146
for ( $i = 1; $i <= $amount; $i++ ) {
142147
$customer = self::generate( true, $args );
148+
if ( is_wp_error( $customer ) ) {
149+
return $customer;
150+
}
143151
$customer_ids[] = $customer->get_id();
144152
}
145153

includes/Generator/CustomerInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CustomerInfo {
1515
*
1616
* @return string|\WP_Error
1717
*/
18-
protected static function get_valid_country_code( string $country_code = '' ) {
18+
public static function get_valid_country_code( string $country_code = '' ) {
1919
$country_code = strtoupper( $country_code );
2020

2121
if ( $country_code && ! WC()->countries->country_exists( $country_code ) ) {

0 commit comments

Comments
 (0)