Override PrepareCustomerNavigationModel

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
So was able to override the PrepareCustomerNavigationModel in one plugin with the code below. But what If I have two plugins and they both want to add a Customer Navigation Menu item.
I played with the Order in the DependencyRegistrar and the Plugin with the highest order gets called to add the menu Item and the other Plugin's PrepareCustomerNavigationModel does not get called. Is there a way to do get both override methods to be called or another way to add Customer Navigation Menu items?

public override CustomerNavigationModel PrepareCustomerNavigationModel(int selectedTabId = 0)
{

    var model = base.PrepareCustomerNavigationModel(selectedTabId);
    var navItems = model.CustomerNavigationItems;

    //Get settings for current store
    var storeScope = _storeContext.CurrentStore.Id;
    var managerSettings = _settingService.LoadSetting<ManagerSettings>(storeScope);

    if (managerSettings.Enabled)
    {
        navItems.Insert(
            navItems.IndexOf(navItems.SingleOrDefault(n => n.Tab == CustomerNavigationEnum.Orders)),
            new CustomerNavigationItemModel
            {
                RouteName = "Nop.Plugin.Scheduler.CustomerCalendar",
                Title = _localizationService.GetLocaleStringResourceByName("Plugin.Scheduler.Account.CustomerCalendar")?.ResourceValue,
                Tab = (CustomerNavigationEnum)CustomCustomerNavigationEnum.CustomerCalendar,
                ItemClass = "customer-calendar"
            });
    }

    model.SelectedTab = (CustomerNavigationEnum)(CustomCustomerNavigationEnum)selectedTabId;

    return model;
}

I saw this post where there are other methods suggested
https://www.nopcommerce.com/en/boards/topic/45694/adding-menu-items-to-customer-navigation
but without trying to work out what they are suggesting it would be good to know if it will it solve my problem for two plugins both trying to add a Customer Navigation Menu item ?
3 years ago
Hello Yidna
Please use  ModelPreparedEvent for PrepareCustomerNavigationModel.
For e.g:
1. Plugin1

public class CustomEvent : IConsumer<ModelPreparedEvent<BaseNopModel>>
    {
        public CustomEvent(){}
        public void HandleEvent(ModelPreparedEvent<BaseNopModel> eventMessage)
        {
            if (eventMessage?.Model is CustomerNavigationModel customerNavigationModel)
            {
                customerNavigationModel.CustomerNavigationItems.Add(new CustomerNavigationItemModel
                {
                    RouteName = "test1",
                    Title = "Test1",
                    Tab = CustomerNavigationEnum.Test1,
                    ItemClass = "customer-test1"
                });
            }
        }
    }


2. Plugin2

public class CustomEvent : IConsumer<ModelPreparedEvent<BaseNopModel>>
    {
        public CustomEvent(){}
        public void HandleEvent(ModelPreparedEvent<BaseNopModel> eventMessage)
        {
            if (eventMessage?.Model is CustomerNavigationModel customerNavigationModel)
            {
                customerNavigationModel.CustomerNavigationItems.Add(new CustomerNavigationItemModel
                {
                    RouteName = "test2",
                    Title = "Test2",
                    Tab = CustomerNavigationEnum.Test2,
                    ItemClass = "customer-test2"
                });
            }
        }
    }

I think this solution will be help you.
3 years ago
Thank you so much Sagar
It worked perfectly :)
1 year ago
Hello,
How you managed to change the CustomerNavigationEnum? as you are not allowed to add values to enum in subclasses..so you have to chnage the core which i don't want to..

How to add to this enum   values without changing the core.
  public enum CustomerNavigationEnum
    {
        Info = 0,
        Addresses = 10,
        Orders = 20,
        BackInStockSubscriptions = 30,
        ReturnRequests = 40,
        DownloadableProducts = 50,
        RewardPoints = 60,
        ChangePassword = 70,
        Avatar = 80,
        ForumSubscriptions = 90,
        ProductReviews = 100,
        VendorInfo = 110,
        GdprTools = 120,
        CheckGiftCardBalance = 130,
        MultiFactorAuthentication = 140,
       Test1 = 150,
        ....

    }
1 year ago
In 4.50 CustomerNavigation model uses int for Tab property instead of an enum value, so that it can be easily used in plugins. See changes here.
1 year ago
Hi
How can I return my custom view ( or view component) by clicking on my custom Customer Navigation Menu item.

I wrote the code below:

MyCustomPluginEvents.cs

public class CustomEvent : IConsumer<ModelPreparedEvent<BaseNopModel>>
{
    public Task HandleEventAsync(ModelPreparedEvent<BaseNopModel> eventMessage)
    {
        if (eventMessage?.Model is CustomerNavigationModel customerNavigationModel)
            customerNavigationModel.CustomerNavigationItems.Add(new CustomerNavigationItemModel
            {
                RouteName = "CustomItem",
                Title = "Customer Item",
                Tab = (int)CustomerNavigationEnum.Info + 1,
                ItemClass = "customer-item"
            });

        return Task.CompletedTask;
    }
}


but its just generate an item without any link and my question is how can I return my custom view component like other items (like as Info or Addresses or ...) from my custom plugin?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.