Magento 2 – Checkout Shipping Address Not Showing Country

checkoutmagento2shipping-address

I've made it so it programmatically creates a user's mail and shipping address whenever they sign in via an external SSO. e.g. google sso.

The shipping address appears fine in the user's address book which includes the country.

but not in the checkout page, nor in the admin area:

enter image description here

The country comes back as a country code for example: GB or AU but in lower case.

Code for the address creation:

$_customer = array (
            'given_name' => $given_name,
            'family_name' => $family_name,
            'email' => $email,
            'birthdate' => $birthdate,
            'gender' => $gender,
            'street_address' => $decodedAddress->street_address,
            'locality' => $decodedAddress->locality,
            'postal_code' => $decodedAddress->postal_code,
            'country' => $decodedAddress->country,
            'region' => $decodedAddress->region,
            'phone_number' => $phone_number,
        );

        $customer = $this->customerCustomerFactory->create();

        $customer->setEmail($_customer['email'])
                ->setFirstname($_customer['given_name'])
                ->setLastname($_customer['family_name'])
                ->setInchooSocialconnectPPid($pixelpinId)
                ->setInchooSocialconnectPPtoken($token)
                ->save();

        $customer->setConfirmation(null);
        $customer->save();

        if(empty($decodedAddress->street_address)) {

        }
        else
        {
            $customAddress = $this->addresss->create();

            $customAddress->setCustomerId($customer->getId())
                    ->setFirstname($_customer['given_name'])
                    ->setLastname($_customer['family_name'])
                    ->setCountryId($_customer['country'])
                    ->setPostcode($_customer['postal_code'])
                    ->setCity($_customer['locality'])
                    ->setTelephone($_customer['phone_number'])
                    ->setStreet($_customer['street_address'])
                    ->setIsDefaultBilling('1')
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');
                $customAddress->save();
        }


        $this->customerSession->setCustomerAsLoggedIn($customer);

Best Answer

Magento doesn't recognize lower case country codes. So the country codes need to be upper-case for Magento to recognize the county code.

Related Topic