Documentation for Merch's CustomerContext

Merch's CustomerContext is responsible for keeping track website user's with respect to the what Merch views as the customer state. Customers will either be anonymous customers or customers depending on whether or not they have authenticated (logged in).

The default CustomerContext uses Umbraco's membership to determine whether or not a customer is an anonymous or known customer by first determining if the website user has authenticated and, if true, then asserts that Umbraco member type of the logged in member is listed in the allowed member types configured in the merchello.config file.

<!-- A comma delimited list of Umbraco MemberTypes to be considered as Merchello Customers -->
<customer memberTypes="Customer,MerchelloCustomer" />


The CustomerContext accepts the current UmbracoContext which it uses to get information about the current HTTP request and current user.

The default CustomerContext can be instantiated simply as follows:

var customerContext = new CustomerContext(UmbracoContext);

The more formal way is to use the PluggableObjectHelper to instantiate the type value configured in the merchello.config.

var customerContext = PluggableObjectHelper.GetInstance<CustomerContextBase>("CustomerContext", UmbracoContext);
<!-- Merchello.config element -->  
    <object alias="CustomerContext" type="Merchello.Web.CustomerContext, Merchello.Web" />

Accessing the CurrentCustomer

// Access the customer's basket

var currentCustomer = CustomerContext.CurrentCustomer;

var basket = currentCustomer.Basket();
// customer is ICustomerBase
var currentCustomer = CustomerContext.CurrentCustomer;

// check if the customer is anonymous
if (!currentCustomer.IsAnonymous) 
  var customer = (ICustomer)currentCustomer;

Public Methods

SetValue(string, string)Stores a string value with key in the "Merchello" cookie.
GetValue(string)Gets a value stored in the "Merchello" cookie.
Reinitialize()Forces the CustomerContext to setup again.


Storing values in the Merchello cookie

Remember to keep in mind that values stored in the Merchello cookie should be relatively small as the maximum size of ALL cookies should not exceed 4093 bytes.

The Merchello Cookie

The CustomerContext persists values in the "merchello" cookie.


Cookie encryption

The value of the cookie is encrypted using Umbraco's `EncryptWithMachineKey()' string extension. Sites running on server farms should include a machineKey element in the web.config.

Custom CustomerContext

In cases where there is the need to use a user provider other than the Umbraco Membership provider, the CustomerContext can be overridden by creating a custom CustomerContext class inheriting from Merchello.Web.Pluggable.CustomerContextBase.

In the customer class, three methods need to be defined.

/// <summary>
/// Returns true or false indicating whether or not the current membership user is logged in.
/// </summary>
/// <returns>
/// The <see cref="bool"/> indicating whether the current user is logged in.
/// </returns>
protected abstract bool GetIsCurrentlyLoggedIn();

/// <summary>
/// Gets the member/user login or user name used to sign in
/// </summary>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
/// <remarks>
/// Merchello makes the association between membership provider users and Merchello customers by username
/// </remarks>
protected abstract string GetMembershipProviderUserName();

/// <summary>
/// Gets the unique ID from the Membership Provider
/// </summary>
/// <returns>
/// The ID or key from the Membership provider as a string value 
/// </returns>
protected abstract string GetMembershipProviderKey();

Finally, the merchello.config needs to be updated to reflect the new class reference.

<!-- Merchello.config element -->  
    <object alias="CustomerContext" type="[YOUR.TYPE.FULLNAME], [YOUR.Library]" />