From 6a7c565cb414e804b12ca6c62a686003481b3614 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Fri, 10 Dec 2021 10:17:02 +0300 Subject: [PATCH] Add validate countryIso. Fix bug with duplicate customer address --- .../class-wc-retailcrm-abstracts-address.php | 18 +++++++++++++++-- .../class-wc-retailcrm-customer-switcher.php | 20 +++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php index a7757ef..e9f3993 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php @@ -84,7 +84,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat if ($order instanceof WC_Order && empty($customerBillingAddress)) { return array( 'index' => $order->get_billing_postcode(), - 'countryIso' => $order->get_billing_country(), + 'countryIso' => $this->validateCountryCode($order->get_billing_country()), 'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()), 'city' => $order->get_billing_city(), 'text' => $this->joinAddresses($order->get_billing_address_1(), $order->get_billing_address_2()) @@ -92,7 +92,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat } else { return array( 'index' => $customer->get_billing_postcode(), - 'countryIso' => $customer->get_billing_country(), + 'countryIso' => $this->validateCountryCode($customer->get_billing_country()), 'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()), 'city' => $customer->get_billing_city(), 'text' => $this->joinAddresses($customer->get_billing_address_1(), $customer->get_billing_address_2()) @@ -100,6 +100,20 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat } } + /** + * Validate countryIso. Check if a given code represents a valid ISO 3166-1 alpha-2 code. + * + * @param $countryCode + * + * @return string + */ + private function validateCountryCode($countryCode) + { + $countries = new WC_Countries(); + + return $countries->country_exists($countryCode) ? $countryCode : ''; + } + /** * Glue two addresses * diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index 5cf7652..03c2297 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -245,11 +245,11 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface */ private static function setBillingAddressToOrder($wcOrder, $address) { - $wcOrder->set_billing_state(self::arrayValue($address, 'region', '--')); - $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '--')); - $wcOrder->set_billing_country(self::arrayValue($address, 'country', '--')); - $wcOrder->set_billing_city(self::arrayValue($address, 'city', '--')); - $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); + $wcOrder->set_billing_state(self::arrayValue($address, 'region', '')); + $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '')); + $wcOrder->set_billing_country(self::arrayValue($address, 'country', '')); + $wcOrder->set_billing_city(self::arrayValue($address, 'city', '')); + $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '')); } /** @@ -262,11 +262,11 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface */ private static function setShippingAddressToOrder($wcOrder, $address) { - $wcOrder->set_shipping_state(self::arrayValue($address, 'region', '--')); - $wcOrder->set_shipping_postcode(self::arrayValue($address, 'index', '--')); - $wcOrder->set_shipping_country(self::arrayValue($address, 'country', '--')); - $wcOrder->set_shipping_city(self::arrayValue($address, 'city', '--')); - $wcOrder->set_shipping_address_1(self::arrayValue($address, 'text', '--')); + $wcOrder->set_shipping_state(self::arrayValue($address, 'region', '')); + $wcOrder->set_shipping_postcode(self::arrayValue($address, 'index', '')); + $wcOrder->set_shipping_country(self::arrayValue($address, 'country', '')); + $wcOrder->set_shipping_city(self::arrayValue($address, 'city', '')); + $wcOrder->set_shipping_address_1(self::arrayValue($address, 'text', '')); } /**