@@ -109,6 +109,13 @@ public function getAddressData(string $type, array $orderData, Quote $quote): ar
109109 $ email = $ this ->cleanEmail ($ orderData ['customer ' ]['email ' ] ?? '' );
110110 }
111111
112+ $ stateCode = $ address ['state_code ' ] ?? '' ;
113+ $ stateName = $ address ['state ' ] ?? '' ;
114+ $ countryCode = $ address ['country_code ' ];
115+ $ regionId = !empty ($ stateCode ) || !empty ($ stateName )
116+ ? $ this ->getRegionId ($ stateCode , $ countryCode , $ stateName )
117+ : null ;
118+
112119 $ addressData = [
113120 'customer_id ' => $ customerId ,
114121 'company ' => $ this ->sanitize ($ company , self ::PATTERN_NAME , 255 ),
@@ -117,10 +124,9 @@ public function getAddressData(string $type, array $orderData, Quote $quote): ar
117124 'lastname ' => $ this ->sanitize ($ address ['last_name ' ], self ::PATTERN_NAME , 255 ) ?? '- ' ,
118125 'street ' => $ this ->getStreet ($ address , (int )$ storeId ),
119126 'city ' => $ this ->sanitize ($ address ['city ' ], self ::PATTERN_CITY , 100 ),
120- 'country_id ' => $ address ['country_code ' ],
121- 'region ' => !empty ($ address ['state_code ' ])
122- ? $ this ->getRegionId ($ address ['state_code ' ], $ address ['country_code ' ])
123- : null ,
127+ 'country_id ' => $ countryCode ,
128+ 'region_id ' => $ regionId ,
129+ 'region ' => $ regionId ? null : ($ stateName ?: null ),
124130 'postcode ' => $ address ['zip_code ' ],
125131 'telephone ' => $ this ->sanitize ($ telephone , self ::PATTERN_TELEPHONE , 20 ) ?? '000 ' ,
126132 'vat_id ' => $ this ->getVatId ($ type , $ orderData , $ storeId ),
@@ -267,14 +273,40 @@ public function getStreet(array $address, int $storeId): string
267273 }
268274
269275 /**
276+ * Multi-strategy region lookup:
277+ * 1. loadByCode($code, $countryId) — e.g. "NH" + "US"
278+ * 2. loadByCode($countryId-$code, $countryId) — e.g. "LV-099" + "LV"
279+ * 3. loadByName($stateName, $countryId) — e.g. "Tukuma novads" + "LV"
280+ * 4. Return null — graceful fallback
281+ *
270282 * @param string $code
271283 * @param string $countryId
272- * @return mixed
284+ * @param string $stateName
285+ * @return int|null
273286 */
274- private function getRegionId (string $ code , string $ countryId)
287+ private function getRegionId (string $ code , string $ countryId, string $ stateName ): ? int
275288 {
276- $ region = $ this ->regionFactory ->create ();
277- return $ region ->loadByCode ($ code , $ countryId )->getId ();
289+ if (!empty ($ code )) {
290+ $ region = $ this ->regionFactory ->create ()->loadByCode ($ code , $ countryId );
291+ if ($ region ->getId ()) {
292+ return (int )$ region ->getId ();
293+ }
294+
295+ $ prefixedCode = $ countryId . '- ' . $ code ;
296+ $ region = $ this ->regionFactory ->create ()->loadByCode ($ prefixedCode , $ countryId );
297+ if ($ region ->getId ()) {
298+ return (int )$ region ->getId ();
299+ }
300+ }
301+
302+ if (!empty ($ stateName )) {
303+ $ region = $ this ->regionFactory ->create ()->loadByName ($ stateName , $ countryId );
304+ if ($ region ->getId ()) {
305+ return (int )$ region ->getId ();
306+ }
307+ }
308+
309+ return null ;
278310 }
279311
280312 /**
@@ -297,7 +329,7 @@ private function saveAddress(array $addressData, int $customerId, string $type):
297329 ->setStreet (explode ("\n" , (string )$ addressData ['street ' ]))
298330 ->setCity ($ addressData ['city ' ])
299331 ->setCountryId ($ addressData ['country_id ' ])
300- ->setRegionId ($ addressData ['region ' ])
332+ ->setRegionId ($ addressData ['region_id ' ])
301333 ->setPostcode ($ addressData ['postcode ' ])
302334 ->setVatId ($ addressData ['vat_id ' ])
303335 ->setTelephone ($ addressData ['telephone ' ]);
0 commit comments