Collect postal address for new users
Posted
20 Sep 2008 4:00 PM
by
SeanWinstead
Over the past months, we've been helping various people install and use Four Roads Commerce in their Community Server websites. One of the features people have asked for is the ability to collect a person's postal address at the time they create their account. Later the address will serve as the default billing address for the person's first purchase.
Out of the box, Commerce does not modify the Join screen in Community Server. But Commerce does provide support for this kind of scenario.
In this blog post, I'll show you how to collect your new member's address at the time they create their account. If you'd like to jump straight into the example source code, you can download it here. If you'd like to see an example of this in action, see NJection, a community for drivers and experts in the automotive field.
Compatibility
The code in this example works with both Four Roads Commerce for CS 2007 and Commerce for CS 2008. The example download contains customized CreateUser.aspx forms for the CS 2007 default theme and for the Calypso and Hawaii themes in CS 2008. This example does not yet contain a theme page for CS 2008.5.
Subclass the CreateUserForm
There are different ways to approach this problem. After some trial and error, I chose to subclass the existing CreateUser form that comes with Community Server. Other than address fields, it has everything needed. In the example download, the custom CreateUserForm class is in the FourRoads.Commerce.User project.
The meat of the class is in the overridden ProcessNewUser method, as shown below.
protected override void ProcessNewUser(CommunityServer.Components.User user)
{
Address address = GetData();
if (address.ValidateAddress(""))
{
address.UserID = user.UserID;
// Switch the context to the user just created so that the address
// security checks will succeed.
User oldUser = CSContext.Current.User;
try
{
CSContext.Current.User = user;
IAddressCRUD crud = ObjectFactoryProvider.Instance().GetInstance<IAddressCRUD>();
crud.CreateUpdateAddress(address, this.CurrentShop);
}
finally
{
CSContext.Current.User = oldUser;
}
}
base.ProcessNewUser(user);
}
The method retrieves the address from the input fields using the GetData method. This example assumes the address is optional and checks for an optional address by checking its validity. Commerce considers an address to be valid if it contains a street, city, postal code, and country. This example doesn't handle the case where the user enters a partial address. If they enter a partial address, the address will not be saved. I'll address that in a future iteration.
If the address is valid, the method switches the context to the current user. This is required due to the strict security checks employed by Commerce. At the time this method is being called, the current user is anonymous. Anonymous users are not allowed to change another person's address. So the code temporarily switches context to the new user so that they can save their own address.
The method then retrieves an object implementing the IAddressCRUD interface and calls its CreateUpdateAddress method. This interface knows how to create, read, update, and delete address instances.
Tweak the markup in CreateUser.aspx
With the custom form in place, we can change the CreateUser.aspx page located in the User subfolder of your Themes directory. As mentioned previously, the example download contains sample CreateUser.aspx pages for both CS 2007 and CS 2008.
Because we subclassed the CreateUser form, we need to change the <CSControl:CreateUserForm> tag to a <FRU:CreateUserForm> tag as shown below:
<FRU:CreateUserForm runat="server" ID="ShopCreateUserForm"
...
...
Note that ASP.NET will not recognize the FRU tag prefix. To make it work, we add the definition for the tag prefix into web.config. Copy the following element from web.merge.config (located in the example download) at the end of the other tag prefixes in your web.config file:
<
add tagPrefix="FRU" namespace="FourRoads.CsShop.Core.Controls" assembly="FourRoads.Commerce.User"/>
And be sure to add a reference to the FourRoads.Commerce.User assembly in your Community Server web project.
Also please notice that I added an ID attribute to the CreateUserForm element. That's required for an interesting reason. When dealing with addresses, Four Roads Commerce always does so in the context of a "shop" (i.e., your online store). Well, there is no current shop when we're creating a new account. In order to set a current shop, I added a CurrentShop property to the custom CreateUser form. Then I added some code in OnInit to set the CurrentShop on the form. Following is the relevant code from the example theme pages.
<%
@ Import Namespace="FourRoads.CsShop.Core" %>
<%@ Import Namespace="FourRoads.CsShop.Core.Controls" %><asp:Content ContentPlaceHolderID="tr" runat="server">
<script language="c#" runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ShopCreateUserForm.CurrentShop = Shops.GetShop("siteshop");
}
</script>
...
...
In this example, my CS installation has a shop with application key "siteshop". Replace "siteshop" with the application key of your own shop.
If you want to use this example in your own site, do the following:
1. Download the example
2. From the example, copy the file FourRoads.Commerce.User.dll into your Web\bin directory.
3. Make a backup of file CreateUser.aspx in your CS theme directory. For example, if you are using the Hawaii theme in CS 2008, make a backup of Web\Themes\Hawaii\User\CreateUser.aspx.
4. From the example, copy the appropriate CreateUser.aspx file into your CS theme directory. For example, if you are using the Hawaii theme in CS 2008 then copy \Web.CS2008\Themes\Hawaii\CreateUser.aspx into the directory Web\Themes\Hawaii\User.
5. From the example, copy the following element from web.merge.config at the end of the other tag prefixes in your CS web.config file:
<
add tagPrefix="FRU" namespace="FourRoads.CsShop.Core.Controls" assembly="FourRoads.Commerce.User"/>
I hope this has been a useful exercise for you. If you have any feedback or questions about this example, please leave them on this blog post or in the Commerce forum.
Filed under: Commerce, address, user, join, example
Rate: