From f0c164d2d49dfe13088bb0744f3a1c357c4a9204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=93=91=F0=9D=93=AA=F0=9D=93=BB=F0=9D=93=BB?= =?UTF-8?q?=F0=9D=94=82=20=F0=9D=93=97=F0=9D=93=BE=F0=9D=93=B0=F0=9D=93=B1?= =?UTF-8?q?=F0=9D=93=AE=F0=9D=93=BC?= <3594411+barryhughes@users.noreply.github.com> Date: Fri, 26 Mar 2021 17:08:37 -0700 Subject: [PATCH 1/2] Do not copy fields from the billing address to the shipping address. | #28759 --- includes/class-wc-customer.php | 19 +++ .../class-wc-customer-data-store-session.php | 5 +- ...ss-wc-customer-data-store-session-test.php | 114 ++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/php/includes/data-stores/class-wc-customer-data-store-session-test.php diff --git a/includes/class-wc-customer.php b/includes/class-wc-customer.php index 3c5c53079f3..65abb9adf14 100644 --- a/includes/class-wc-customer.php +++ b/includes/class-wc-customer.php @@ -242,6 +242,25 @@ 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. + * + * @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? * diff --git a/includes/data-stores/class-wc-customer-data-store-session.php b/includes/data-stores/class-wc-customer-data-store-session.php index 4a2585d20ca..9b5d66ad4f9 100644 --- a/includes/data-stores/class-wc-customer-data-store-session.php +++ b/includes/data-stores/class-wc-customer-data-store-session.php @@ -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() ); } diff --git a/tests/php/includes/data-stores/class-wc-customer-data-store-session-test.php b/tests/php/includes/data-stores/class-wc-customer-data-store-session-test.php new file mode 100644 index 00000000000..75ef103b8e0 --- /dev/null +++ b/tests/php/includes/data-stores/class-wc-customer-data-store-session-test.php @@ -0,0 +1,114 @@ +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, + ), + ); + } +} From 568e1e4f940e4d390fdd7e89ca9b70cdbaba43f4 Mon Sep 17 00:00:00 2001 From: Barry Hughes <3594411+barryhughes@users.noreply.github.com> Date: Wed, 7 Apr 2021 14:11:08 -0700 Subject: [PATCH 2/2] Add `@since` tag --- includes/class-wc-customer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/class-wc-customer.php b/includes/class-wc-customer.php index ef476d5f0e9..da549f99907 100644 --- a/includes/class-wc-customer.php +++ b/includes/class-wc-customer.php @@ -248,6 +248,8 @@ class WC_Customer extends WC_Legacy_Customer { * 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() {