Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Markets (dev)

Describes the Market concept in Optimizely Commerce Connect.

A single site can have multiple markets, each with its own product catalog, language, currency, and promotions.

Classes in this topic are available in the following namespaces:

  • Mediachase.Commerce
  • Mediachase.Commerce.Markets

ICurrentMarket interface

Sites that implement multi-market functionality must also implement the ICurrentMarket interface. The ICurrentMarket implementation must return the market of the current request, and is called to get the appropriate market for the execution of most market-sensitive business logic.

Register custom ICurrentMarket implementations in the ConfigureServices method of the Startup class.

A business's market segmentation strategy determines its implementation of the ICurrentMarket interface. If your business segments markets geographically, use a prestitial page, an IP geolocation service, or a drop-down list to determine a user's region and market.

Markets are not constrained to a region. A business-to-business site typically permits only authenticated users to use the system and assigns a market to each customer when the customer is created. In this case, the ICurrentMarket implementation's retrieval of the current market is based on the current authenticated user.

The default implementation of ICurrentMarket returns the default market. This is the correct behavior for sites that do not implement multi-market functionality.

The following code shows the ICurrentMarket interface:

public interface ICurrentMarket {
  // Gets the current market.
  IMarket GetCurrentMarket();

  // Sets the current market.
  void SetCurrentMarket(MarketId marketId);
}

using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.ServiceLocation;
using Mediachase.Commerce;

// The custom implementation of ICurrentMarket
public class MyCurrentMarketImplementation: ICurrentMarket {
  public IMarket GetCurrentMarket() {
    ...implementation...
  }

  public void SetCurrentMarket(MarketId marketId) {
    ...implementation...
  }
}

public class Startup {
  public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton<ICurrentMarket, MyCurrentMarketImplementation>();
  }
}

Orders, carts, and wish lists include a market ID so processing continues when the ICurrentMarket implementation is not available.

Carts are market-dependent, so they are unique to a user and market, instead of just a user. If a site lets a user switch markets, switching the market may also switch associated carts to new or different ones. Because wish lists are carts, the same behavior applies to wish lists.

IMarket interface

An instance of the IMarket interface represents a market. The data associated with each market is typically used as a set of guidelines for regional markets. However, the market system is not tied to a region. Languages and currencies are not filtered based on the value of the current market. Use the MarketImpl class as a default implementation of the IMarket interface.

A default market with ID MarketId.Default is available. Disable this market for sites that implement multi-market functionality, but do not delete it.

The following code shows the IMarket interface:

public interface IMarket {
  // Gets the market's unique identifier.
  MarketId MarketId {
    get;
  }

  // Gets a value indicating if the the market is enabled.
  bool IsEnabled {
    get;
  }

  // Gets the market's name.
  string MarketName {
    get;
  }

  // Gets the market's description.
  string MarketDescription {
    get;
  }

  // Gets the default language for the market.
  CultureInfo DefaultLanguage {
    get;
  }

  // Gets the available languages for the market.
  IEnumerable<CultureInfo> Languages {
    get;
  }

  // Gets the default currency for the market.
  Currency DefaultCurrency {
    get;
  }

  // Gets the available currencies for the market.
  IEnumerable<Currency> Currencies {
    get;
  }

  // Gets the countries associated with the market.
  IEnumerable<string> Countries {
    get;
  }

  // Gets the value indicating if the price of the market includes tax or not.
  bool PricesIncludeTax {
    get;
  }
}

IMarketService interface

Access market data with the IMarketService API, which exposes create, replace, update, and delete operations for market data. The following code shows the interface:

public interface IMarketService {
  // Gets all markets in the system.
  IEnumerable<IMarket> GetAllMarkets();

  // Get a single market by ID, or null if the market ID is not found.
  IMarket GetMarket(MarketId marketId);

  // Adds a new market to the market system.
  void CreateMarket(IMarket market);

  // Updates an existing market in the market system.
  void UpdateMarket(IMarket market);

  // Deletes a market by ID.
  void DeleteMarket(MarketId marketId);
}