0

I'm barely new to Magento2 and Magento in general. The version I'm using is ----> 2.4.3-p1

I'm facing this issue: when I add a new Address in checkout page, it doesn't show the Region/State field, but just the city.

I don't know why this is happening, also if I try to checkout with that second address, I got this error Please specify a regionId in shipping address.

So I'm trying to solve these issues.

In this screenshot I'm showing you the problem with that missing Region/State text.

For formatting the addresses I've created these two files, overwriting them:

  1. app/design/frontend/Magenio/vyvo/CommunityEngineering_JapaneseAddress/web/template/checkout/shipping-information/address-renderer/jp.html

  2. app/design/frontend/Magenio/vyvo/CommunityEngineering_JapaneseAddress/web/template/checkout/shipping-address/address-renderer/jp.html

And everything works fine, except for that missing text and the error when doing the checkout choosing the second address.

enter image description here

If you can help me understand guys it would be awesome, thanks!

3 Answers 3

0

Replace save new address function in view/shipping.js file /** * Save new shipping address */ saveNewAddress: function () { var addressData, newShippingAddress;

        this.source.set('params.invalid', false);
        this.triggerShippingDataValidateEvent();

        if (!this.source.get('params.invalid')) {
            addressData = this.source.get('shippingAddress');
            
            addressData['save_in_address_book'] = this.saveInAddressBook ? 1 : 0;
            
            //In address append state also
            if (addressData['region_id'] && $('[name="region_id"]').attr("aria-required")) {
                var regionId = addressData['region_id'];
                var $selectedOption = $('[name="region_id"] option[value="' + regionId + '"]:selected').first();
                if ($selectedOption.length) {
                    var regionName = $selectedOption.text();
                    addressData['region'] = regionName;
                } else {
                    console.error('Selected option not found.');
                }
            } 
            
            // New address must be selected as a shipping address
            newShippingAddress = createShippingAddress(addressData);
            selectShippingAddress(newShippingAddress);
            checkoutData.setSelectedShippingAddress(newShippingAddress.getKey());
            checkoutData.setNewCustomerShippingAddress($.extend(true, {}, addressData));
            this.getPopUp().closeModal();
            this.isNewAddressAdded(true);
        }
    },
0

Try to change settings at Store > Configuration > General > General > State Options

enter image description here

9
  • I've tried this solution before, but nothing changed unfortunately. Now in "State is Required for" I choosed Italy, and in "Allow to Choose State if it is Optional for Country" I choosed Yes. I did it again, and did bin/magento c:f, but still that Region/State is missing.
    – D.D.
    Apr 29, 2022 at 8:08
  • did you try doing it on the specific store view? Apr 29, 2022 at 8:11
  • No I didn't, at least I think I didn't, how could I do that?
    – D.D.
    Apr 29, 2022 at 8:31
  • In the upper left corner, under the title "Configuration", there is a dropdown menu "Scope". Try also to modify "Country options" Apr 29, 2022 at 8:39
  • 1
    No problem, thanks for helping anyways :)
    – D.D.
    Apr 29, 2022 at 9:00
0

To address the issue you're facing, kindly follow the steps below:

  1. Firstly, make a copy of the 'vendor/magento/module-checkout/view/frontend/web/js/view/shipping.js' file.

  2. Then, navigate to your custom theme directory, located at 'app/design/frontend/[NameSpace]/[ThemeName]/Magento_Checkout/web/js/view/shipping.js'.

  3. Paste the copied 'shipping.js' file into this directory, overwriting the existing file.

  4. Once you've done this, locate the 'saveNewAddress' function within the 'shipping.js' file.

  5. Replace the existing code within this function with the updated code.

          /**
          * Save new shipping address
          */
         saveNewAddress: function () {
             var addressData,
                 newShippingAddress;
    
             this.source.set('params.invalid', false);
             this.triggerShippingDataValidateEvent();
    
             if (!this.source.get('params.invalid')) {
                 addressData = this.source.get('shippingAddress');
                 // if user clicked the checkbox, its value is true or false. Need to convert.
                 addressData['save_in_address_book'] = this.saveInAddressBook ? 1 : 0;
                 if(addressData['region_id'] && $('[name="region_id"]').attr("aria-required")){
                     addressData['region'] = $('[name="region_id"]').find("option:selected").attr("data-title");
                 }else{
                     addressData['region'] = $('[name="region"]').val();
                 }
                 // New address must be selected as a shipping address
                 newShippingAddress = createShippingAddress(addressData);
                 selectShippingAddress(newShippingAddress);
                 checkoutData.setSelectedShippingAddress(newShippingAddress.getKey());
                 checkoutData.setNewCustomerShippingAddress($.extend(true, {}, addressData));
                 this.getPopUp().closeModal();
                 this.isNewAddressAdded(true);
             }
         },
    

These steps should help you resolve the issue you're encountering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.