1
0
mirror of synced 2025-01-18 08:51:41 +03:00

Add validate countryIso. Fix bug with duplicate customer address

This commit is contained in:
Dima Uryvskiy 2021-12-10 10:17:02 +03:00 committed by GitHub
parent 5407d9111f
commit 6a7c565cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 12 deletions

View File

@ -84,7 +84,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
if ($order instanceof WC_Order && empty($customerBillingAddress)) { if ($order instanceof WC_Order && empty($customerBillingAddress)) {
return array( return array(
'index' => $order->get_billing_postcode(), '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()), 'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()),
'city' => $order->get_billing_city(), 'city' => $order->get_billing_city(),
'text' => $this->joinAddresses($order->get_billing_address_1(), $order->get_billing_address_2()) '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 { } else {
return array( return array(
'index' => $customer->get_billing_postcode(), '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()), 'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()),
'city' => $customer->get_billing_city(), 'city' => $customer->get_billing_city(),
'text' => $this->joinAddresses($customer->get_billing_address_1(), $customer->get_billing_address_2()) '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 * Glue two addresses
* *

View File

@ -245,11 +245,11 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface
*/ */
private static function setBillingAddressToOrder($wcOrder, $address) private static function setBillingAddressToOrder($wcOrder, $address)
{ {
$wcOrder->set_billing_state(self::arrayValue($address, 'region', '--')); $wcOrder->set_billing_state(self::arrayValue($address, 'region', ''));
$wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '--')); $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', ''));
$wcOrder->set_billing_country(self::arrayValue($address, 'country', '--')); $wcOrder->set_billing_country(self::arrayValue($address, 'country', ''));
$wcOrder->set_billing_city(self::arrayValue($address, 'city', '--')); $wcOrder->set_billing_city(self::arrayValue($address, 'city', ''));
$wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); $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) private static function setShippingAddressToOrder($wcOrder, $address)
{ {
$wcOrder->set_shipping_state(self::arrayValue($address, 'region', '--')); $wcOrder->set_shipping_state(self::arrayValue($address, 'region', ''));
$wcOrder->set_shipping_postcode(self::arrayValue($address, 'index', '--')); $wcOrder->set_shipping_postcode(self::arrayValue($address, 'index', ''));
$wcOrder->set_shipping_country(self::arrayValue($address, 'country', '--')); $wcOrder->set_shipping_country(self::arrayValue($address, 'country', ''));
$wcOrder->set_shipping_city(self::arrayValue($address, 'city', '--')); $wcOrder->set_shipping_city(self::arrayValue($address, 'city', ''));
$wcOrder->set_shipping_address_1(self::arrayValue($address, 'text', '--')); $wcOrder->set_shipping_address_1(self::arrayValue($address, 'text', ''));
} }
/** /**