@@ -271,21 +271,24 @@ protected function _prepareForm()
271271
272272 $ this ->_form ->setValues ($ this ->getFormValues ());
273273
274- if ($ this ->_form ->getElement ('country_id ' )->getValue ()) {
275- $ countryId = $ this ->_form ->getElement ('country_id ' )->getValue ();
276- $ this ->_form ->getElement ('country_id ' )->setValue (null );
277- foreach ($ this ->_form ->getElement ('country_id ' )->getValues () as $ country ) {
274+ $ countryElement = $ this ->_form ->getElement ('country_id ' );
275+
276+ $ this ->processCountryOptions ($ countryElement );
277+
278+ if ($ countryElement ->getValue ()) {
279+ $ countryId = $ countryElement ->getValue ();
280+ $ countryElement ->setValue (null );
281+ foreach ($ countryElement ->getValues () as $ country ) {
278282 if ($ country ['value ' ] == $ countryId ) {
279- $ this -> _form -> getElement ( ' country_id ' ) ->setValue ($ countryId );
283+ $ countryElement ->setValue ($ countryId );
280284 }
281285 }
282286 }
283- if ($ this -> _form -> getElement ( ' country_id ' ) ->getValue () === null ) {
284- $ this -> _form -> getElement ( ' country_id ' ) ->setValue (
287+ if ($ countryElement ->getValue () === null ) {
288+ $ countryElement ->setValue (
285289 $ this ->directoryHelper ->getDefaultCountry ($ this ->getStore ())
286290 );
287291 }
288- $ this ->processCountryOptions ($ this ->_form ->getElement ('country_id ' ));
289292 // Set custom renderer for VAT field if needed
290293 $ vatIdElement = $ this ->_form ->getElement ('vat_id ' );
291294 if ($ vatIdElement && $ this ->getDisplayVatValidationButton () !== false ) {
@@ -309,7 +312,7 @@ protected function _prepareForm()
309312 */
310313 private function processCountryOptions (\Magento \Framework \Data \Form \Element \AbstractElement $ countryElement )
311314 {
312- $ storeId = $ this ->getBackendQuoteSession ()-> getStoreId ();
315+ $ storeId = $ this ->getAddressStoreId ();
313316 $ options = $ this ->getCountriesCollection ()
314317 ->loadByStore ($ storeId )
315318 ->toOptionArray ();
@@ -388,4 +391,14 @@ public function getAddressAsString(\Magento\Customer\Api\Data\AddressInterface $
388391
389392 return $ this ->escapeHtml ($ result );
390393 }
394+
395+ /**
396+ * Return address store id.
397+ *
398+ * @return int
399+ */
400+ protected function getAddressStoreId ()
401+ {
402+ return $ this ->getBackendQuoteSession ()->getStoreId ();
403+ }
391404}
0 commit comments