pull store name

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
How do I pull store name on news item?
6 years ago
paul.g wrote:
How do I pull store name on news item?


You can get it from IStoreMappingService. Here is an example of how you could get it (if there are more than one store mapping the first one will be retrieved):


if (newsItem.LimitedToStores)
{
    var storeMapping = _storeMappingService.GetStoreMappings(newsItem).FirstOrDefault();
    if (storeMapping != null)
    {
        var storeName = storeMapping.Store.Name;
    }
}
6 years ago
mgustafsson wrote:
How do I pull store name on news item?

You can get it from IStoreMappingService. Here is an example of how you could get it (if there are more than one store mapping the first one will be retrieved):


if (newsItem.LimitedToStores)
{
    var storeMapping = _storeMappingService.GetStoreMappings(newsItem).FirstOrDefault();
    if (storeMapping != null)
    {
        var storeName = storeMapping.Store.Name;
    }
}


It doesn't really work what is exactly newsItem ? also do you refer to v4.0 ?

Thanks
6 years ago
paul.g wrote:
How do I pull store name on news item?

You can get it from IStoreMappingService. Here is an example of how you could get it (if there are more than one store mapping the first one will be retrieved):


if (newsItem.LimitedToStores)
{
    var storeMapping = _storeMappingService.GetStoreMappings(newsItem).FirstOrDefault();
    if (storeMapping != null)
    {
        var storeName = storeMapping.Store.Name;
    }
}


It doesn't really work what is exactly newsItem ? also do you refer to v4.0 ?

Thanks


newsItem is the NewsItem domain entity, I assumed that was what you referred to with "news item". Yes, v4.0, should be same API in v3.x though.

Where are you trying to get hold of the store name: in a controller, razor view? Could you explain little more what you are trying to achieve.
6 years ago
mgustafsson wrote:

newsItem is the NewsItem domain entity, I assumed that was what you referred to with "news item". Yes, v4.0, should be same API in v3.x though.

Where are you trying to get hold of the store name: in a controller, razor view? Could you explain little more what you are trying to achieve.


Ok what I want todo is to add OG metadata to news view, so on the NewsItem.cshtml I need to pull store name.
6 years ago
paul.g wrote:

newsItem is the NewsItem domain entity, I assumed that was what you referred to with "news item". Yes, v4.0, should be same API in v3.x though.

Where are you trying to get hold of the store name: in a controller, razor view? Could you explain little more what you are trying to achieve.

Ok what I want todo is to add OG metadata to news view, so on the NewsItem.cshtml I need to pull store name.


Got ya! That you can achieve by following steps (not tested):

1. Create a new string property StoreName on NewsItemModel (either on the existing model or create a new partial NewsItemModel)


public partial class NewsItemModel
{
public string StoreName { get; set; }
}


2. Set the StoreName property in PrepareNewsItemModel in the NewsModelFactory

private readonly IStoreMappingService _storeMappingService;

public NewsModelFactory(
        ...
        IStoreMappingService storeMappingService)
{
   ...
    _storeMappingService = storeMappingService;
}

public virtual NewsItemModel PrepareNewsItemModel(NewsItemModel model, NewsItem newsItem, bool prepareComments)
{
   ...
   var storeMapping = _storeMappingService.GetStoreMappings(newsItem).FirstOrDefault();
   if (storeMapping != null)
   {
      model.StoreName = storeMapping.Store.Name;
   }
   ...
}


3. Use the StoreName propery in NewsItem.cshtml

@Model.StoreName


Is that what you are looking for?
6 years ago
Seems exactly what I want, did all that  you wrote, but it doesn't print anything just empty space :(
6 years ago
paul.g wrote:
Seems exactly what I want, did all that  you wrote, but it doesn't print anything just empty space :(


I just pasted the code into a project and it worked for me. Are you sure the news item has any store mappings? What do you see if you set a breakpoint on the controller code?
6 years ago
Oh also if you don't want to manipulate nopCommerce core you could subscribe to the ModelPrepared event from a plugin and in there look up the store name.


public class ModelPreparedEventConsumer : IConsumer<ModelPrepared<BaseNopModel>>
    {
        private readonly IStoreMappingService _storeMappingService;
        private readonly INewsService _newsService;

        public ModelPreparedEventConsumer(
            IStoreMappingService storeMappingService,
            INewsService  newsService
            )
        {
            _storeMappingService = storeMappingService;
            _newsService = newsService;
        }

        public void HandleEvent(ModelPrepared<BaseNopModel> eventMessage)
        {
            if (!(eventMessage.Model is NewsItemModel model)) return;
            var newsItem = _newsService.GetNewsById(model.Id);
            var storeMapping = _storeMappingService.GetStoreMappings(newsItem).FirstOrDefault();
            if (storeMapping != null)
            {
                model.StoreName = storeMapping.Store.Name;
            }
        }
    }
6 years ago
Thanks!
First solution seems always returning "null" no errors no other problems, not sure what I'm doing wrong.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.