Hi!
First, have you registered your implementation, so it's in use?
[InitializableModule]
public class CurrentMarketModule : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Container.Configure(ce =>
{
ce.For().Singleton().Use();
});
}
}
And how does your implementation look like? GetCurrentMarket() and SetCurrentMarket(MarketId marketId) more specifically?
Hi Johan,
My implementation looks like:
///
/// Gets the
/// set and the indicated market is valid; ; otherwise, gets the default
///
///
public IMarket GetCurrentMarket()
{
var profileStorage = this.GetProfileStorage();
var profileMarketId = profileStorage == null ? null : profileStorage[MarketIdKey] as string;
var marketId = string.IsNullOrEmpty(profileMarketId) ? MarketId.Default : new MarketId(profileMarketId);
var market = _marketService.GetMarket(marketId);
if (market == null && marketId != MarketId.Default)
{
market = _marketService.GetMarket(MarketId.Default);
}
UpdateProfile(market);
return market;
}
///
/// Sets the current market, if
/// otherwise, performs no action.
///
/// The market id.
///
public void SetCurrentMarket(MarketId marketId)
{
var market = _marketService.GetMarket(marketId);
if (market != null)
{
UpdateProfile(market);
SiteContext.Current.Currency = market.DefaultCurrency;
EPiServer.Globalization.ContentLanguage.PreferredCulture = market.DefaultLanguage;
}
}
private void UpdateProfile(IMarket market)
{
var profileStorage = this.GetProfileStorage();
if (profileStorage != null)
{
var originalMarketId = profileStorage[MarketIdKey] as string;
var currentMarketId = market == null || market.MarketId == MarketId.Default ? string.Empty : market.MarketId.Value;
if (!string.Equals(originalMarketId, currentMarketId, StringComparison.Ordinal))
{
profileStorage[MarketIdKey] = currentMarketId;
profileStorage.Save();
}
}
}
private ProfileBase GetProfileStorage()
{
var httpContext = HttpContext.Current;
return httpContext == null ? null : httpContext.Profile;
}
No, i do not do any registering for my Market Storage. Thanks.
Hi,
I try to do this set this on my configuration class InitializationModule (inherits IConfigurableModule) but it seems that crashes my application with the error the getter of the current market : "The settings property 'MarketId' was not found."
I'm doing this as in the Alloy demo site project and also following the directions from: http://kkhan-episerver.blogspot.ro/2013/12/working-with-markets-in-episerver.html
The method looks looks like:
///
/// Gets the
/// set and the indicated market is valid; ; otherwise, gets the default
///
///
public IMarket GetCurrentMarket()
{
var profileStorage = this.GetProfileStorage();
var profileMarketId = profileStorage == null ? null : profileStorage[MarketIdKey] as string;
var marketId = string.IsNullOrEmpty(profileMarketId) ? MarketId.Default : new MarketId(profileMarketId);
var market = _marketService.GetMarket(marketId);
if (market == null && marketId != MarketId.Default)
{
market = _marketService.GetMarket(MarketId.Default);
}
UpdateProfile(market);
return market;
}
Probably I am missing something around here.
Thanks,
Liviu
If you are getting the "The settings property 'MarketId' was not found." error, then you need to update your web.config with the relevant information in the profile:
...
I am working on Episerver Commerce 7.9.0.126 based project.
I am trying to set inside another market than default market. Am am using the ultility service interface service ICurrentMarket to get or set the current market object. Am am doing that like in the control MarketSelector of the Allooy demo website where everything is working fine.
On out project, the Default Market cannot be changed with another one any abordation I tried. The other markets are created in the Commerce and they can be retrieved in the code, but they cannot override the default.
What am I missing?
I am using the code below to set the market:
///
/// Set the current market.
///
public static void SetCurrentMarketToCatering()
{
IMarket cateringMarket = AllMarketsGetter.FirstOrDefault(m => m.MarketName == CateringMarketName);
if (cateringMarket != null)();
{
ICurrentMarket cMarket = ServiceLocator.Current.GetInstance
if (cMarket.GetCurrentMarket().MarketId != cateringMarket.MarketId)
{
cMarket.SetCurrentMarket(cateringMarket.MarketId);
}
}
}
where cateringMarket instance is not null. After asignement, the instance cMarket remains Default even though the cateringMarket.MarketId is provided.
Please ask me for any additional information needed.
Thanks a lot in advance !