Skip to content

Commit

Permalink
fix(orders): fix error when state does not match XX-XX format
Browse files Browse the repository at this point in the history
INT-171
  • Loading branch information
EdieLemoine committed Oct 16, 2023
1 parent 6753811 commit b2d848c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Adapter/WcAddressAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] ?? '';
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Adapter/WcAddressAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@
'meta' => [],
],

'2 letter state' => [
'addressType' => 'shipping',
'address' => [
'billing_email' => '[email protected]',
'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' => '[email protected]',
'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' => [
Expand Down

0 comments on commit b2d848c

Please sign in to comment.