Don't verify empty country codes on checkout

PR #28849 introduced a verification of the posted country code
on checkout, so an invalid code will throw an error. However there
are cases when an empty code is legitimately received, for example
when using Paypal checkout directly from the product page and
the customer doesn't have an address in his Paypal profile.
This commit is contained in:
Nestor Soriano 2021-04-07 09:59:56 +02:00
parent 855f48d53b
commit a37b2a7474
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 25 additions and 1 deletions

View File

@ -747,7 +747,7 @@ class WC_Checkout {
$field_label = isset( $field['label'] ) ? $field['label'] : '';
if ( $validate_fieldset &&
( isset( $field['type'] ) && 'country' === $field['type'] ) &&
( isset( $field['type'] ) && 'country' === $field['type'] && '' !== $data[ $key ] ) &&
! WC()->countries->country_exists( $data[ $key ] ) ) {
/* translators: ISO 3166-1 alpha-2 country code */
$errors->add( $key . '_validation', sprintf( __( "'%s' is not a valid country code.", 'woocommerce' ), $data[ $key ] ) );

View File

@ -93,6 +93,30 @@ class WC_Checkout_Test extends \WC_Unit_Test_Case {
$this->assertEmpty( $errors->get_error_message( 'shipping_country_validation' ) );
}
/**
* @testdox 'validate_posted_data' doesn't add errors for empty billing/shipping countries.
*
* @testWith [true]
* [false]
*
* @param bool $ship_to_different_address True to simulate shipping to a different address than the billing address.
*/
public function test_validate_posted_data_does_not_add_error_for_empty_country( $ship_to_different_address ) {
$_POST = array(
'billing_country' => '',
'shipping_country' => '',
'ship_to_different_address' => $ship_to_different_address,
);
$data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$errors = new WP_Error();
$this->sut->validate_posted_data( $data, $errors );
$this->assertEmpty( $errors->get_error_message( 'billing_country_validation' ) );
$this->assertEmpty( $errors->get_error_message( 'shipping_country_validation' ) );
}
/**
* @testdox 'validate_checkout' adds a "We don't ship to country X" error but only if the country exists.
*