New address

The flow that I am faced with is that a user will add line items to a basket as they are picked from the catalog. When done the user "checks out" and places the order. As part of the checkout process I need to add shipping info to the basket (and each line item) and add payment information to the basket. First question, if I want to add shipping information (first shipping address) then how is the best way to "create" a new address There must be more to it than simply new Address(...) because I need the address id. There must be a "create address" function somewhere that creates the address along with the address id. I am assuming that once this "create" method is called the newly created address will be in the Addresses collection of the basket or do I need to add it

Thank you.

Kevin



Answer this question

New address

  • ag1206

    Kevin,

    Here is how I create an address profile and then use it to create an OrderAdress.

    public void CreateShipToAddress(string OrgId,
    string AddressName,
    string CompanyName,
    string AddressLine1,
    string AddressLine2,
    string City,
    string RegionCode,
    string RegionName,
    string PostalCode,
    string CountryCode,
    string CountryName,
    string Phone)
    {
    // Create a new Profile GUID using the required format
    StringBuilder sb = new StringBuilder();
    sb.Append("{");
    sb.Append(Guid.NewGuid().ToString());
    sb.Append("}");

    // Create a new Address Profile
    Profile address = CommerceContext.Current.ProfileSystem.CreateProfile(sb.ToString(), "Address");

    // Get the current user's email address from the ProfileContext
    string UserEmail = CommerceContext.Current.UserProfile.Properties["GeneralInfo.email_address"].Value.ToString();

    // Set the required property values
    address.Properties["GeneralInfo.last_name"].Value = CompanyName;
    address.Properties["GeneralInfo.address_name"].Value = AddressName;
    address.Properties["GeneralInfo.address_type"].Value = 1; // 1 = Shipping
    address.Properties["GeneralInfo.org_id"].Value = OrgId;
    address.Properties["GeneralInfo.address_code"].Value = "TEMP"; // Indicates a temporary address
    address.Properties["GeneralInfo.address_line1"].Value = AddressLine1;
    address.Properties["GeneralInfo.address_line2"].Value = AddressLine2;
    address.Properties["GeneralInfo.city"].Value = City;
    address.Properties["GeneralInfo.region_code"].Value = RegionCode;
    address.Properties["GeneralInfo.region_name"].Value = RegionName;
    address.Properties["GeneralInfo.postal_code"].Value = PostalCode;
    address.Properties["GeneralInfo.country_code"].Value = CountryCode;
    address.Properties["GeneralInfo.country_name"].Value = CountryName;
    address.Properties["GeneralInfo.tel_number"].Value = Phone;
    address.Properties["ProfileSystem.date_created"].Value = DateTime.Today;
    address.Properties["ProfileSystem.user_id_changed_by"].Value = UserEmail;

    //Update and save the profile
    address.Update();
    }

    public OrderAddress CreateOrderAddress(string AddressId)
    {
    // Get an Address Profile using the addressId
    Profile AddressProfile = CommerceContext.Current.ProfileSystem.GetProfile("address_id", AddressId, "Address");

    // Create a new OrderAddress object using the Address profile
    OrderAddress address = new OrderAddress(AddressProfile.Properties["GeneralInfo.address_name"].Value.ToString(), AddressProfile);

    // Map a few properties missing from standard OrderAddress created from Address Profile
    address.CountryCode = AddressProfile.Properties["GeneralInfo.country_code"].Value.ToString();
    address.RegionName = AddressProfile.Properties["GeneralInfo.region_name"].Value == null "" : AddressProfile.Properties["GeneralInfo.region_name"].Value.ToString();
    address.Email = CommerceContext.Current.UserProfile.Properties["GeneralInfo.email_address"].Value.ToString();
    address.DaytimePhoneNumber = CommerceContext.Current.UserProfile.Properties["GeneralInfo.tel_number"].Value == null "": CommerceContext.Current.UserProfile.Properties["GeneralInfo.tel_number"].Value.ToString();
    address.FirstName = CommerceContext.Current.UserProfile.Properties["GeneralInfo.first_name"].Value.ToString();
    address.LastName = CommerceContext.Current.UserProfile.Properties["GeneralInfo.last_name"].Value.ToString();

    // Add a weakly typed OrderAddress property
    address["address_code"] = AddressProfile.Properties["GeneralInfo.address_code"].Value.ToString();

    // return the OrderAddress object
    return address;
    }

    Let me know if you have any questions or need any more code samples and I'll send them via email.

    Jeff Lynch
    MVP Windows Server System - Commerce Server
    http://codebetter.com/blogs/jeff.lynch



  • bird.tw

    Kevin,

    It's important to draw a distinction between an OrderAddress object, which is what's added to a user's basket during checkout, and a Profile object which happens to be an address.

    The constructor you pointed to takes the address id of an existing address in the profile system, or an actual address profile. This sort of functionality is desirable for users who have saved addresses in the system, but is not required for creating an address during checkout.

    These address profiles are initially created by the following API command:

    Guid addressId = Guid.NewGuid;
    Profile addressProfile = CommerceContext.Current.ProfileSystem.CreateProfile(addressId.ToString(), "Address");

    The addressId, as well as the addressProfile object created as a result of that CreateProfile method are both acceptable inputs to the OrderAddress constructor. Again, you want to use this sort of functionality for logged in users, as you usually persist these profile objects back to the profiles database when you're done populating them.

    If you'r doing anonymous checkout, you want to avoid creating the profile object as listed above, and just set the properties of a blank OrderAddress object created using the default constructor. Creating the address profile obect without associating it to a registered user's profile could result in orphaned database records, which can negatively affect performance over time.

    To answer your second question, the Addresses property on the basket is an OrderAddressCollection, not a profile collection, so creating the Address profile object will not add anything to that collection.

    Hopefully this all makes sense, but feel free to ask for clarification on anything.



  • enric vives

    Now that I know about the distiction between a ProfileAddress and an OrderAddress, I am assuming that once I create and store a ProfileAddress (as a new profile) then I can use the AddressId that was used to create the profile as the AddressId for the shipping address or the billing address as appropriate. Or if I happen to create an OrderAddress first I can use the AddressId to create a ProfileAddress. If this is the case then there must be some central repository of "Addresses" that have a key of the AddressId. Right

    Thank you.

    Kevin


  • New address