Try our conversational search powered by Generative AI!

cannot change Default Market in the website backend

Vote:
 

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 !

#88511
Jul 15, 2014 14:45
Vote:
 

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?

#88514
Jul 15, 2014 16:43
Vote:
 

Hi Johan,

   My implementation looks like:

       ///


        /// Gets the selected in the current user profile, if the value is
        /// set and the indicated market is valid; ; otherwise, gets the default .
        ///

        /// The current .
        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 represents a valid market;
        /// otherwise, performs no action.
        ///

        /// The market id.
        /// This will also set the current currency for the ECF context.
        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.

#88528
Jul 16, 2014 8:50
Vote:
 

Ok, try to register your implementation with the code I posted above.

#88552
Jul 16, 2014 16:57
Vote:
 

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 selected in the current user profile, if the value is
        /// set and the indicated market is valid; ; otherwise, gets the default .
        ///

        /// The current .
        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

#88576
Edited, Jul 17, 2014 15:03
Vote:
 

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:



  
    
      
        ...
        
      
    
  



#88587
Jul 17, 2014 22:41
Vote:
 

Hi Cris,

  That was the setting indeed. Registering the implementation of the ICurrentMarket is indeed the key solutionto this matter.

  Thanks  a lot for you feedback !

Regards,

Liviu

 

 

#88601
Jul 18, 2014 11:26
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.