Magento 2 Shipping – Error Displaying Rates with UPS XML Configuration

magento2shippingups

I am trying to setup UPS XML method for Shipping Rates, and it seems to work for any address in California. When I try an address outside of California, I do not get any shipping rates back.

enter image description here

So I looked at the Magento debug.log and saw the below response. My assumption is that it is replacing the StateProvinceCode for CA when its sending the request to UPS.

I looked at the following link https://github.com/magento/magento2/issues/6875 thinking the issue could be fixed, but its still ignoring the region in the Shipping Method.

'accessRequest' => '<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
  <AccessLicenseNumber>XXXXXXXX</AccessLicenseNumber>
  <UserId>****</UserId>
  <Password>****</Password>
</AccessRequest>
',
  'request' => '<?xml version="1.0"?>
<RatingServiceSelectionRequest xml:lang="en-US">
  <Request>
    <TransactionReference>
      <CustomerContext>Rating and Service</CustomerContext>
      <XpciVersion>1.0</XpciVersion>
    </TransactionReference>
    <RequestAction>Rate</RequestAction>
    <RequestOption>Shop</RequestOption>
  </Request>
  <PickupType>
          <Code>03</Code>
          <Description>Customer Counter</Description>
  </PickupType>

  <Shipment>      <Shipper>      <Address>
          <City></City>
          <PostalCode>90034</PostalCode>
          <CountryCode>US</CountryCode>
          <StateProvinceCode>CA</StateProvinceCode>
      </Address>
    </Shipper>
    <ShipTo>
      <Address>
          <PostalCode>28607</PostalCode>
          <CountryCode>US</CountryCode>
          <ResidentialAddress>01</ResidentialAddress>
          <StateProvinceCode>CA</StateProvinceCode><ResidentialAddressIndicator>01</ResidentialAddressIndicator>      </Address>
    </ShipTo>


    <ShipFrom>
      <Address>
          <PostalCode>90034</PostalCode>
          <CountryCode>US</CountryCode>
          <StateProvinceCode>CA</StateProvinceCode>
      </Address>
    </ShipFrom>

    <Package>
      <PackagingType><Code>00</Code></PackagingType>
      <PackageWeight>
         <UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement>
        <Weight>8</Weight>
      </PackageWeight>
    </Package>  </Shipment>
</RatingServiceSelectionRequest>',
  'result' => '<?xml version="1.0"?>
<RatingServiceSelectionResponse><Response><TransactionReference><CustomerContext>Rating and Service</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><ResponseStatusCode>0</ResponseStatusCode><ResponseStatusDescription>Failure</ResponseStatusDescription><Error><ErrorSeverity>Hard</ErrorSeverity><ErrorCode>111285</ErrorCode><ErrorDescription>The postal code 28607 is invalid for CA United States.</ErrorDescription></Error></Response></RatingServiceSelectionResponse>

Best Answer

After more debugging and google searching. I came across this commit in the Magento 2 project: https://github.com/magento/magento2/commit/e788a2421f5cfe2df9d5028b08141d92f70819b8

It details javascript changes that need to occur in three files. This fix has not yet been included in any Magento Release and the changes should not be done in the core files. Instead as a temporary solution until the solution is released, I basically overwrote these files in my theme directory (app/design/frontend/{Vendor}/{Theme}/Magento_Checkout/web/js/model/ directory.

The following modifications have been made to the files:

Actual:

app/code/Magento/Checkout/view/frontend/web/js/model/address-converter.js

Theme Directory:

app/design/frontend/ZEndertech/MilkJarCookies/Magento_Checkout/web/js/model/address-converter.js

The formAddressDataToQuoteAddress() needs modifications` as so:

formAddressDataToQuoteAddress: function(formData) {
            // clone address form data to new object
            var addressData = $.extend(true, {}, formData),
                region,
                regionName = addressData.region;
            if (mageUtils.isObject(addressData.street)) {
                addressData.street = this.objectToArray(addressData.street);
            }

            addressData.region = {
                region_id: addressData.regionId,
                region_code: addressData.regionCode,
                region: regionName
            };

            if (addressData.region_id
                && countryData()[addressData.country_id]
                && countryData()[addressData.country_id]['regions']
            ) {
                region = countryData()[addressData.country_id]['regions'][addressData.region_id];
                if (region) {
                    addressData.region.region_id = addressData['region_id'];
                    addressData.region.region_code = region['code'];
                    addressData.region.region = region['name'];
                } else if (
                    !addressData.region_id
                    && countryData()[addressData.country_id]
                    && countryData()[addressData.country_id]['regions']
                ) {
                    addressData.region_code = '';
                    addressData.region.region = '';
                }
            }
            delete addressData.region_id;

            return address(addressData);
        }

Actual:

app/code/Magento/Checkout/view/frontend/web/js/model/new-customer-address.js

Theme Directory:

app/design/frontend/ZEndertech/MilkJarCookies/Magento_Checkout/web/js/model/new-customer-address.js

The changes in the new-customer-address.js file depends on the version of Magento 2 you are running. It seemed the fix in github dates back to September 28, 2016. My Magento version is 2.1.3. So I had to do a few more modifications to the file for it to work as detailed in the following snippet.

return function (addressData) {
        var identifier = Date.now();

        var regionId = null;

        if (addressData.region && addressData.region.region_id) {
            regionId = addressData.region.region_id;
        } else if (addressData.country_id && addressData.country_id == window.checkoutConfig.defaultCountryId) {
            regionId = window.checkoutConfig.defaultRegionId;
        }

        return {
            email: addressData.email,
            countryId: (addressData.country_id) ? addressData.country_id : window.checkoutConfig.defaultCountryId,
            regionId: regionId || addressData.regionId,

Actual:

app/code/Magento/Checkout/view/frontend/web/js/model/shipping-rate-validator.js

Theme Directory:

app/design/frontend/ZEndertech/MilkJarCookies/Magento_Checkout/web/js/model/shipping-rate-validator.js

Finally the validateFields() method is modified:

validateFields: function () {
            var addressFlat = addressConverter.formDataProviderToFlatData(
                    this.collectObservedData(),
                    'shippingAddress'
                ),
                address;

            if (this.validateAddressData(addressFlat)) {
                addressFlat = uiRegistry.get('checkoutProvider').shippingAddress;
                address = addressConverter.formAddressDataToQuoteAddress(addressFlat);
                selectShippingAddress(address);
            }
        }
Related Topic