Merge pull request #29605 from woocommerce/fix/28759-billing-shipping-addresses

Fix/28759 billing shipping addresses
This commit is contained in:
Roy Ho 2021-04-07 14:34:01 -07:00 committed by GitHub
commit 8052fbe7be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 2 deletions

View File

@ -242,6 +242,27 @@ class WC_Customer extends WC_Legacy_Customer {
return $this->get_calculated_shipping();
}
/**
* Indicates if the customer has a non-empty shipping address.
*
* Note that this does not indicate if the customer's shipping address
* is complete, only that one or more fields are populated.
*
* @since 5.3.0
*
* @return bool
*/
public function has_shipping_address() {
foreach ( $this->get_shipping() as $address_field ) {
// Trim guards against a case where a subset of saved shipping address fields contain whitespace.
if ( strlen( trim( $address_field ) ) > 0 ) {
return true;
}
}
return false;
}
/**
* Get if customer is VAT exempt?
*

View File

@ -126,12 +126,13 @@ class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Cust
protected function set_defaults( &$customer ) {
try {
$default = wc_get_customer_default_location();
$has_shipping_address = $customer->has_shipping_address();
if ( ! $customer->get_billing_country() ) {
$customer->set_billing_country( $default['country'] );
}
if ( ! $customer->get_shipping_country() ) {
if ( ! $customer->get_shipping_country() && ! $has_shipping_address ) {
$customer->set_shipping_country( $customer->get_billing_country() );
}
@ -139,7 +140,7 @@ class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Cust
$customer->set_billing_state( $default['state'] );
}
if ( ! $customer->get_shipping_state() ) {
if ( ! $customer->get_shipping_state() && ! $has_shipping_address ) {
$customer->set_shipping_state( $customer->get_billing_state() );
}

View File

@ -0,0 +1,114 @@
<?php
/**
* Tests relating to the WC_Customer_Data_Store_Session class.
*/
class WC_Customer_Data_Store_Session_Test extends WC_Unit_Test_Case {
/**
* Ensure that the country and state shipping address fields only inherit
* the corresponding billing address values if a shipping address is not set.
*
* @see https://github.com/woocommerce/woocommerce/issues/28759
* @dataProvider provide_customers_with_different_addresses
*
* @param WC_Customer $customer The customer object being tested.
* @param bool $states_should_match If the billing and shipping states should match.
* @param bool $countries_should_match If the billing and shipping countries should match.
*/
public function test_setting_default_address_fields( WC_Customer $customer, bool $states_should_match, bool $countries_should_match ) {
$session_data = new WC_Customer_Data_Store_Session();
$session_data->read( $customer );
if ( $states_should_match ) {
$this->assertEquals( $customer->get_shipping_state(), $customer->get_billing_state() );
} else {
$this->assertNotEquals( $customer->get_shipping_state(), $customer->get_billing_state() );
}
if ( $countries_should_match ) {
$this->assertEquals( $customer->get_shipping_country(), $customer->get_billing_country() );
} else {
$this->assertNotEquals( $customer->get_shipping_country(), $customer->get_billing_country() );
}
}
/**
* Customer objects with a mixture of billing and shipping addresses.
*
* Each inner dataset is organized as follows:
*
* [
* (WC_Customer) $customer_object,
* (bool) $states_should_match,
* (bool) $countries_should_match,
* ]
*
* @return array[]
*/
public function provide_customers_with_different_addresses() {
$has_billing_address_only = new WC_Customer();
$has_billing_address_only->set_email( 'wc-customer-test-01@test.user' );
$has_billing_address_only->set_billing_address( '1234 Quality Lane' );
$has_billing_address_only->set_billing_city( 'Testville' );
$has_billing_address_only->set_billing_country( 'US' );
$has_billing_address_only->set_billing_state( 'CA' );
$has_billing_address_only->set_billing_postcode( '90123' );
$has_billing_address_only->save();
$separate_billing_and_shipping_state_and_country = new WC_Customer();
$separate_billing_and_shipping_state_and_country->set_email( 'wc-customer-test-02@test.user' );
$separate_billing_and_shipping_state_and_country->set_billing_address( '4567 Scenario Street' );
$separate_billing_and_shipping_state_and_country->set_billing_city( 'Unitly' );
$separate_billing_and_shipping_state_and_country->set_billing_country( 'UK' );
$separate_billing_and_shipping_state_and_country->set_billing_state( 'Computershire' );
$separate_billing_and_shipping_state_and_country->set_billing_postcode( 'ZX1 2PQ' );
$separate_billing_and_shipping_state_and_country->set_shipping_address( '8901 Situation Court' );
$separate_billing_and_shipping_state_and_country->set_shipping_city( 'Endtoendly' );
$separate_billing_and_shipping_state_and_country->set_shipping_country( 'CA' );
$separate_billing_and_shipping_state_and_country->set_shipping_state( 'BC' );
$separate_billing_and_shipping_state_and_country->set_shipping_postcode( 'A1B 2C3' );
$separate_billing_and_shipping_state_and_country->save();
$separate_billing_state_same_country = new WC_Customer();
$separate_billing_state_same_country->set_email( 'wc-customer-test-03@test.user' );
$separate_billing_state_same_country->set_billing_address( '4567 Scenario Street' );
$separate_billing_state_same_country->set_billing_city( 'Unitly' );
$separate_billing_state_same_country->set_billing_country( 'UK' );
$separate_billing_state_same_country->set_billing_state( 'Computershire' );
$separate_billing_state_same_country->set_billing_postcode( 'ZX1 2PQ' );
$separate_billing_state_same_country->set_shipping_address( '8901 Situation Court' );
$separate_billing_state_same_country->set_shipping_city( 'Endtoendly' );
$separate_billing_state_same_country->set_shipping_country( 'UK' );
$separate_billing_state_same_country->set_shipping_state( 'Byteshire' );
$separate_billing_state_same_country->set_shipping_postcode( 'RS1 2TU' );
$separate_billing_state_same_country->save();
$shipping_address_is_effectively_empty = new WC_Customer();
$shipping_address_is_effectively_empty->set_email( 'wc-customer-test-04@test.user' );
$shipping_address_is_effectively_empty->set_shipping_address( ' ' );
$shipping_address_is_effectively_empty->save();
return array(
'has_billing_address_only' => array(
$has_billing_address_only,
true,
true,
),
'separate_billing_and_shipping_state_and_country' => array(
$separate_billing_and_shipping_state_and_country,
false,
false,
),
'separate_billing_state_same_country' => array(
$separate_billing_state_same_country,
false,
true,
),
'shipping_address_is_effectively_empty' => array(
$shipping_address_is_effectively_empty,
true,
true,
),
);
}
}