Close

New Field

[email protected]
10 years ago
#3840 Quote
Avatar
  • 8
Hi,

I'm having trouble with a retrieving value of a field
I need to add categories in menu in additional "Categories Menu", i'm explain.


By Default it's possible to do :
Home, Categories
              -Categ1
              -Categ2
              -Categ3
or
Home, Categ1, Categ2, Categ3

But i need to do that :
Home, Categories, Categ1
           -Categ2
           -Categ3


So, I added a new boolean field "IsInMenu" to exclude categories in sub "Categories" and Include it in First "<LI>" .

I edited Views to add my loop and my restriction but my field is always always "false" when I see in my administration this field is "true".


Do you have somes Ideas, to get my field's value and why is't not getting?
Support
10 years ago
#3841 Quote
Avatar
  • Moderator
  • 1044
[email protected] wrote:
Hi,

I'm having trouble with a retrieving value of a field
I need to add categories in menu in additional "Categories Menu", i'm explain.


By Default it's possible to do :
Home, Categories
              -Categ1
              -Categ2
              -Categ3
or
Home, Categ1, Categ2, Categ3

But i need to do that :
Home, Categories, Categ1
           -Categ2
           -Categ3


So, I added a new boolean field "IsInMenu" to exclude categories in sub "Categories" and Include it in First "<LI>" .

I edited Views to add my loop and my restriction but my field is always always "false" when I see in my administration this field is "true".


Do you have somes Ideas, to get my field's value and why is't not getting?


Hi,

As far as I understand you have made customization to the CategoriesModel, admin view for categories, admin catalog controller and etc. I am not sure how you keep your new field in the database(probably as a entity attribute), but you should check your logic for setting this field in the administration and how you retrieve it in the public part.

Hope that helps!
[email protected]
10 years ago
#3844 Quote
Avatar
  • 8
Hi indeed,

My new field is called "isInMegaMenu", and it's meant to provide a footer area for the Home page categories, similar to the way the Featured Products show on the home page.

I added the field to:
- Database
- Core.Domain.Catalog.Category
- Web.Models.Catalog.CategoryModel
- Admin.Models.Catalog.CategoryModel
      public partial class CategoryModel : BaseNopEntityModel, ILocalizedModel<CategoryLocalizedModel>
      public partial class CategoryLocalizedModel : ILocalizedModelLocal

- Libraries\Nop.Data\Mapping\Catalog\CategoryMap.cs
adding the line this.Property(c => c.isInMegaMenu) in the CategoryMap class

- Added the field to the Admin View:
       _CreateOrUpdate.cshtml
- Added a resource for the field header


elsewhere my field get a goog value but not in MegaMenuView.

So i created a Html Extension to do that, i know that it's a good way to do but it's working, so if someone have another idea, i take it :)


        /// <summary>
        /// Test if CategoryId is enable to add in MegaMenu
        /// </summary>
        /// <param name="html">HTML helper</param>
        /// <param name="CategoryId">CategoryId</param>
        /// <returns>bool</returns>
        public static bool IsInMegaMenu(this HtmlHelper html, int CategoryId)
        {
            bool ret = false;
            var RepoCateg = EngineContext.Current.Resolve<IRepository<Category>>();
            var cat = RepoCateg.Table.SingleOrDefault(x => x.Id == CategoryId);
            if (cat != null)
            {
                return cat.isInMegaMenu;
            }
            return ret;
        }


And My Call is :

//ToInclude
@foreach (var category in Model.CategoriesModels.Where(x => Html.IsInMegaMenu(x.CategoryModel.Id)) {
}
//ToExclude
@foreach (var category in Model.CategoriesModels.Where(x => !Html.IsInMegaMenu(x.CategoryModel.Id)) {
}
Support
10 years ago
#3849 Quote
Avatar
  • Moderator
  • 1044
Hi,

The HTML helper is a good way to do it, as the Nop Mega Menu uses its own MegaMenuCategoryModel which is optimized in terms of performance. In the beginning we used the nopCommerce's category model, but there were a lot of properties and models that were built as part of the category, which were not used by the Nop Mega Menu. That is why we use our own model that contains only the properties we need for the Nop Mega Menu. This is the reason why your new field is not populated correctly in the Nop Mega Menu, because we use our own model and controller which does not set anything extra.

Hope that makes sense!