You're probably going to want to get/set the mobile value in a variety of places in your storefront. Try using a helper class to make things easier on you as a programmer. Then you don't have to repeat your code so much throughout the application.
Here's a helper class file. Save it into your /App_Code/ folder with the name MyHelperClass.cs:
Code: Select all
using System;
using System.Collections.Generic;
using System.Web;
using CommerceBuilder.Users;
using CommerceBuilder.Stores;
using CommerceBuilder.Common;
/// <summary>
/// Summary description for MyHelperClass
/// </summary>
public class MyHelperClass
{
public static string GetMobile(Address _Address)
{
// set up return value
string _RetVal = "";
// check for valid
if (_Address == null)
return ""; // primary address does not exist
// find mobile custom field for this address ID
string _Query = "FieldName = 'MOBILE' AND StoreId={0} AND TableName='{1}' AND ForeignKeyId={2}";
_Query = String.Format(_Query, Token.Instance.StoreId, "ac_Addresses",_Address.AddressId);
CustomFieldCollection _CustomFields = CustomFieldDataSource.LoadForCriteria(_Query);
if (_CustomFields.Count == 0)
return ""; // no custom field for Mobile for this address
// got a mobile, pull in value and return it
_RetVal = _CustomFields[0].FieldValue;
// exit and return value
return _RetVal;
}
public static void SetMobile(Address _Address, string _MobileNumber)
{
// check for valid
if (_Address == null)
return; // primary address does not exist
// find mobile custom field for this address ID
string _Query = "FieldName = 'MOBILE' AND StoreId={0} AND TableName='{1}' AND ForeignKeyId={2}";
_Query = String.Format(_Query, Token.Instance.StoreId, "ac_Addresses", _Address.AddressId);
CustomFieldCollection _CustomFields = CustomFieldDataSource.LoadForCriteria(_Query);
CustomField _CustomField;
if (_CustomFields.Count == 0)
{
// have to make new custom field
_CustomField = new CustomField();
_CustomField.FieldName = "MOBILE";
_CustomField.FieldValue = _MobileNumber;
_CustomField.ForeignKeyId = _Address.AddressId;
_CustomField.TableName = "AC_Addresses";
_CustomField.StoreId = Token.Instance.StoreId;
}
else // already exists, just update it
{
_CustomField = _CustomFields[0];
_CustomField.FieldValue = _MobileNumber;
}
// either way, save it when we're done
_CustomField.Save();
// exit and return
return;
}
}
Once you have that class saved, now it's a very simple matter to set or get the mobile number for any address record at anywhere in your application.
Here's the InitAddressForm() routine with appropriate changes:
Code: Select all
protected void InitAddressForm()
{
Address address = this.Address;
string actionLabel = (address.AddressId == 0) ? "Create" : "Edit";
string typeLabel = (address.AddressId == Token.Instance.User.PrimaryAddress.AddressId) ? "Billing" : "Shipping";
EditAddressCaption.Text = string.Format(EditAddressCaption.Text, actionLabel, typeLabel);
FirstName.Text = address.FirstName;
LastName.Text = address.LastName;
Address1.Text = address.Address1;
Address2.Text = address.Address2;
City.Text = address.City;
PostalCode.Text = address.PostalCode;
InitCountryAndProvince();
Phone.Text = address.Phone;
Company.Text = address.Company;
Fax.Text = address.Fax;
Residence.SelectedIndex = (address.Residence ? 0 : 1);
// BEGIN MOD: AbleMods.com
// 7/15/2011
txt_Mobile.Text = MyHelperClass.GetMobile(address);
// END MOD: AbleMods.com
}
And here is the EditSaveButton_Click() routine updated to store the mobile value entered by the user:
Code: Select all
protected void EditSaveButton_Click(object sender, EventArgs e)
{
string provinceName;
if (ValidateProvince(out provinceName))
{
if (Page.IsValid)
{
Address address = this.Address;
address.FirstName = StringHelper.StripHtml(FirstName.Text);
address.LastName = StringHelper.StripHtml(LastName.Text);
address.Address1 = StringHelper.StripHtml(Address1.Text);
address.Address2 = StringHelper.StripHtml(Address2.Text);
address.Company = StringHelper.StripHtml(Company.Text);
address.City = StringHelper.StripHtml(City.Text);
address.Province = provinceName;
address.PostalCode = StringHelper.StripHtml(PostalCode.Text);
address.CountryCode = Country.SelectedValue;
address.Phone = StringHelper.StripHtml(Phone.Text);
address.Fax = StringHelper.StripHtml(Fax.Text);
address.Residence = (Residence.SelectedIndex == 0);
// BEGIN MOD: AbleMods.com
// 7/15/2011
MyHelperClass.SetMobile(address, txt_Mobile.Text);
// END MOD: AbleMods.com
address.Save();
ShowAddressBook();
}
}
else
{
Province2Invalid.IsValid = false;
UpdateCountry();
}
}
As you can see, it's much easier to read the code and far quicker to deploy the new field in other areas of the application. Plus, if you need additional changes to the Get or Set routine, now you only have to change it in one place.
Finally, always remember to comment your code. The more changes you make, the more likely you'll forget why you made those changes.
Enjoy!
