In an Episerver e-commerce website, markets are the way you can split up your products so that they can be sold to different demographics. Obviously, all companies are different and each company will have its own business processes and its own unique sales funnels. This means that the exact criteria that determine a company's markets can potentially be very different between projects. Some companies might just have a single market, others can define multiple markets, each with its own product catalogue, language, currency, and promotions determined by factors such as brand, location, or locale. In this simple guide, I'm going to talk about how to create and query your own markets within Episerver commerce. In this post, I'll hopefully go over all the code you'll need to get up and running with markets!
How To Query Existing Markets
In Episerver you can view all the existing markets contained within your website, via code using the
IMarketService API. This can be done in code:
How To Get The Current Site Visitors Market
When a site visitor first hits your website, one of the first things you need to do within your request pipeline, is to look up the market ID they should be using. If a market has not been associated with a customer yet, you should associate one within a cookie or session. Failing to do this will mean they'll likely just see the default market and may not see the correct products. To get the current market and set the market you can use the
Get Current Market: To get the default current market you can use this code:
Set Current Market: To set the current market you can use this code:
Custom Market Handler
Depending on how you associate the market from which a customer is applicable to buy from, will determine a rule that the site should adhere to. If you have custom needs then simply calling
GetCurrentMarket() will not work. You will need to implement custom logic instead. How you determine this logic will be up to you. Some readers might want to store the users current market ID in a cookie, some in session. Some logic might be based on the current language or the user's location. As the market logic can be custom you can also create a custom current market handler to cater for this.
To do this, you'll need to create a new class that implements
ICurrentMarket interface and implements the
SetCurrentMarket() with their own implements.
NOTE: Don't forget to update your structure map container to use this new class, otherwise it will likely default to the out-of-the-box implementation !!!
The code below is based on a requirement I had on a past project. The logic you end up writing will likely be different than this, however, hopefully, it'll give you an idea about how to create a custom market handler. In this example, on each page request, a cookie value to read if the market has been previously set. If it hasn't we'll query the current language API to get the culture code. Using the current language code, we can query the current markets API to get the matching market.
I Need To Store Custom Data Within My Markets, Is This Possible?
Yes, like most things Episerver, markets are defined by an interface,
IMarket. Out-of-the-box, Episerver gives you the
MarketImpl class. You can use this class to define your markets and pass it into the
IMarketService to create a new market within your commerce database. If you want to add in your own custom fields, all you need to do is create your own class with your own custom properties, like so:
If you have a need to sell your products to different markets, based on either country, language, brands, currency etc.. then markets are the place to start. Building markets in Episerver hopefully shouldn't be too painful. All the API's and entities all are interface based, so unit testing isn't too bad. Happy Coding 🤘