Skip to content

Commit 5710a95

Browse files
committed
👔 Add new check for unsupported countries
1 parent 0dbe673 commit 5710a95

1 file changed

Lines changed: 53 additions & 32 deletions

File tree

‎modules/ppcp-store-sync/src/CartValidation/ShippingValidator.php‎

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace WooCommerce\PayPalCommerce\StoreSync\CartValidation;
1818

19+
use WC_Countries;
1920
use WC_Validation;
2021
use WC_Product;
2122

@@ -31,6 +32,14 @@
3132

3233
class ShippingValidator implements ValidatorInterface {
3334

35+
/**
36+
* List of shipping countries that are supported by the agentic integration.
37+
* Managed on plugin-side for extra code and test stability.
38+
*
39+
* If this list is expanded, also update the message in {@see validate_country()}
40+
*/
41+
private const PAYPAL_SUPPORTED_COUNTRIES = array( 'US' );
42+
3443
private ProductManager $product_manager;
3544

3645
public function __construct( ProductManager $product_manager ) {
@@ -114,15 +123,15 @@ private function validate_address_completeness( Address $address ): array {
114123
->user_message( 'Please provide a complete street address.' )
115124
->for_field( 'shipping_address.address_line_1' )
116125
->add_resolution(
117-
ResolutionOption::create_provide_missing_field()
118-
->label( 'Provide street address' )
119-
->set_meta( 'field', 'address_line_1' )
120-
)
121-
->add_resolution(
122-
ResolutionOption::create_update_address()
123-
->label( 'Update shipping address' )
124-
->priority( Priority::LOW )
125-
);
126+
ResolutionOption::create_provide_missing_field()
127+
->label( 'Provide street address' )
128+
->set_meta( 'field', 'address_line_1' )
129+
)
130+
->add_resolution(
131+
ResolutionOption::create_update_address()
132+
->label( 'Update shipping address' )
133+
->priority( Priority::LOW )
134+
);
126135
}
127136

128137
if ( ! $address->admin_area_2() ) {
@@ -131,15 +140,15 @@ private function validate_address_completeness( Address $address ): array {
131140
->user_message( 'Please provide a city.' )
132141
->for_field( 'shipping_address.admin_area_2' )
133142
->add_resolution(
134-
ResolutionOption::create_provide_missing_field()
135-
->label( 'Provide city' )
136-
->set_meta( 'field', 'admin_area_2' )
137-
)
138-
->add_resolution(
139-
ResolutionOption::create_update_address()
140-
->label( 'Update shipping address' )
141-
->priority( Priority::LOW )
142-
);
143+
ResolutionOption::create_provide_missing_field()
144+
->label( 'Provide city' )
145+
->set_meta( 'field', 'admin_area_2' )
146+
)
147+
->add_resolution(
148+
ResolutionOption::create_update_address()
149+
->label( 'Update shipping address' )
150+
->priority( Priority::LOW )
151+
);
143152
}
144153

145154
$postal_code = $address->postal_code();
@@ -149,15 +158,15 @@ private function validate_address_completeness( Address $address ): array {
149158
->user_message( 'Please provide a postal code.' )
150159
->for_field( 'shipping_address.postal_code' )
151160
->add_resolution(
152-
ResolutionOption::create_provide_missing_field()
153-
->label( 'Provide postal code' )
154-
->set_meta( 'field', 'postal_code' )
155-
)
156-
->add_resolution(
157-
ResolutionOption::create_update_address()
158-
->label( 'Update shipping address' )
159-
->priority( Priority::LOW )
160-
);
161+
ResolutionOption::create_provide_missing_field()
162+
->label( 'Provide postal code' )
163+
->set_meta( 'field', 'postal_code' )
164+
)
165+
->add_resolution(
166+
ResolutionOption::create_update_address()
167+
->label( 'Update shipping address' )
168+
->priority( Priority::LOW )
169+
);
161170
} else {
162171
$postal_validation =
163172
$this->validate_postal_code_format( $postal_code, $address->country_code() );
@@ -339,6 +348,17 @@ private function validate_country( ?string $country_code ): ?ValidationIssue {
339348
);
340349
}
341350

351+
if ( $this->get_wc_countries() && ! $this->is_paypal_supported_country( $country_code ) ) {
352+
return ValidationIssue::create_shipping_unavailable( sprintf( 'Shipping to %s is not supported by PayPal', $country_code ) )
353+
->user_message( 'PayPal currently only supports shipping to the United States.' )
354+
->for_field( 'shipping_address.country_code' )
355+
->add_resolution(
356+
ResolutionOption::create_update_address()
357+
->label( 'Use a supported shipping country' )
358+
->priority( Priority::HIGH )
359+
);
360+
}
361+
342362
return null;
343363
}
344364

@@ -380,17 +400,18 @@ private function get_country_name( string $country_code ): string {
380400
return $countries[ $country_code ] ?? $country_code;
381401
}
382402

383-
/**
384-
* @return \WC_Countries|null
385-
*/
386-
private function get_wc_countries() {
403+
private function get_wc_countries(): ?WC_Countries {
387404
if ( ! function_exists( 'WC' ) ) {
388405
return null;
389406
}
390407

391408
// The only place in the class that has a `WC()` dependency.
392409
$wc = WC();
393410

394-
return $wc ? $wc->countries : null;
411+
return $wc->countries;
412+
}
413+
414+
private function is_paypal_supported_country( string $country_code ): bool {
415+
return in_array( $country_code, self::PAYPAL_SUPPORTED_COUNTRIES, true );
395416
}
396417
}

0 commit comments

Comments
 (0)