Using country-specific address formats

There is no universally accepted address format. In nearly every country, the address format differs – in the US, the house number comes before the street name, in Denmark it’s opposite. In Hungary, the city or locality is written before the street name and number. In Ethiopia, the name of the building is written before the street and house number. Even if these differences seem small, they can play a significant role in whether or not a package makes it to the intended recipient.

To use country-specific address formats you can define frontend address formats on each ecommerce country. You can then use our api to fetch and render addresses using the approproate format – typically this will be in a shopping cart template.

In this example we do the following:

  1. Fetch the address format based on a country code. In this case we retrieve it from the Customer.Country.Code property of an on order object
  2. Run through the address format list and replace each placeholder with a property from the order object
  3. Render the formatted address
RAZOR
@using System.Text.RegularExpressions @using System.Collections.Generic @{ <!--Fetch address format information based on country code--> List<List<string>> addressFormat = Dynamicweb.Ecommerce.International.Country.GetDisplayAddress(GetString("Ecom:Order.Customer.Country.Code")); } @foreach (List<string> addressLine in addressFormat) { for (var i = 0; i < addressLine.Count; i++) { addressLine[i] = Regex.Replace(addressLine[i], "{name}", GetString("Ecom:Order.Customer.Name"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{firstName}", GetString("Ecom:Order.Customer.FirstName"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{lastName}", GetString("Ecom:Order.Customer.LastName"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{company}", GetString("Ecom:Order.Customer.Company"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{address}", GetString("Ecom:Order.Customer.Address"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{address1}", GetString("Ecom:Order.Customer.Address"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{address2}", GetString("Ecom:Order.Customer.Address2"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{zip}", GetString("Ecom:Order.Customer.Zip"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{city}", GetString("Ecom:Order.Customer.City"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{regionCode}", GetString("Ecom:Order.Customer.RegionCode"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{country}", GetString("Ecom:Order.Customer.Country"), RegexOptions.IgnoreCase); addressLine[i] = Regex.Replace(addressLine[i], "{phone}", GetString("Ecom:Order.Customer.Phone"), RegexOptions.IgnoreCase); } var completeLine = string.Join(string.Empty, addressLine.ToArray()); if (!string.IsNullOrWhiteSpace(completeLine)) { <span>@completeLine</span> } }