From b2d848c51d32048a2e87b9791279ab871b3178d7 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Mon, 16 Oct 2023 11:31:28 +0200 Subject: [PATCH] fix(orders): fix error when state does not match XX-XX format INT-171 --- src/Adapter/WcAddressAdapter.php | 7 ++-- tests/Unit/Adapter/WcAddressAdapterTest.php | 36 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Adapter/WcAddressAdapter.php b/src/Adapter/WcAddressAdapter.php index 3cff24728..8b3bd5a6f 100644 --- a/src/Adapter/WcAddressAdapter.php +++ b/src/Adapter/WcAddressAdapter.php @@ -4,7 +4,6 @@ namespace MyParcelNL\WooCommerce\Adapter; -use MyParcelNL\Pdk\Base\Support\Arr; use MyParcelNL\Pdk\Facade\Pdk; use WC_Cart; use WC_Customer; @@ -161,9 +160,11 @@ private function getSeparateAddressFromOrder(WC_Order $order, string $addressTyp */ private function getState($class, string $addressType): string { - $value = $this->getAddressField($class, Pdk::get('fieldState'), $addressType); + $value = $this->getAddressField($class, Pdk::get('fieldState'), $addressType) ?? ''; - return $value ? Arr::last(explode('-', $value)) : ''; + preg_match('/^([A-Z]{2})(?:-([A-Z]{2}))?$/', $value, $matches); + + return $matches[2] ?? $matches[1] ?? ''; } /** diff --git a/tests/Unit/Adapter/WcAddressAdapterTest.php b/tests/Unit/Adapter/WcAddressAdapterTest.php index 9340bf3a4..fbe2c11a5 100644 --- a/tests/Unit/Adapter/WcAddressAdapterTest.php +++ b/tests/Unit/Adapter/WcAddressAdapterTest.php @@ -36,6 +36,42 @@ 'meta' => [], ], + '2 letter state' => [ + 'addressType' => 'shipping', + 'address' => [ + 'billing_email' => 'test@test.com', + 'billing_phone' => '0612345678', + 'shipping_address_1' => 'Antareslaan 31', + 'shipping_address_2' => '', + 'shipping_city' => 'Hoofddorp', + 'shipping_company' => 'MyParcel', + 'shipping_country' => 'NL', + 'shipping_first_name' => 'Felicia', + 'shipping_last_name' => 'Parcel', + 'shipping_postcode' => '2132JE', + 'shipping_state' => 'NH', + ], + 'meta' => [], + ], + + 'unrecognized state' => [ + 'addressType' => 'shipping', + 'address' => [ + 'billing_email' => 'test@test.com', + 'billing_phone' => '0612345678', + 'shipping_address_1' => 'Antareslaan 31', + 'shipping_address_2' => '', + 'shipping_city' => 'Hoofddorp', + 'shipping_company' => 'MyParcel', + 'shipping_country' => 'NL', + 'shipping_first_name' => 'Felicia', + 'shipping_last_name' => 'Parcel', + 'shipping_postcode' => '2132JE', + 'shipping_state' => 'Noord-Holland', + ], + 'meta' => [], + ], + 'separate address fields' => [ 'addressType' => 'shipping', 'address' => [