Sort of ProductsList in Admin panel

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Hello everyone!
I'm new on nopcommerce.I'm trying to change the sort of products in admin area.
I want them to sort by their ADD date. means the product added recently show at the top of the list.

Appreciate any help
3 years ago
You need to change the Code - Can you use Visual Studio ?
3 years ago
sure
3 years ago
Have a look in the
    src\Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs
the routine
    public virtual ProductListModel PrepareProductListModel(ProductSearchModel searchModel)

The data comes from the ProductLoadAllPaged Store Procedure via the SearchProducts Routine there is an OrderBy parameter but it is not passed in so have a look at how that works

Looking in the stored procedure the values for the OrderBy are
  @OrderBy      
int = 0, --0 - position, 5 - Name: A to Z, 6 - Name: Z to A, 10 - Price: Low to High, 11 - Price: High to Low, 15 - creation date
3 years ago
Thank you so much it worked!
3 years ago
Can you please post how you made it work?
Also it will only solve , if it did , only for poduct page not other pages, like manufacutures, categories etc..
3 years ago
Yidna wrote:
The data comes from the ProductLoadAllPaged Store Procedure via the SearchProducts Routine there is an OrderBy parameter. Looking in the stored procedure the values for the OrderBy are @OrderBy      
int = 0, --0 - position, 5 - Name: A to Z, 6 - Name: Z to A, 10 - Price: Low to High, 11 - Price: High to Low, 15 - creation date

You need to pass in the Orderby parameter to the routine

For Manufacturers
See GetAllManufacturers(...) in
source43\Libraries\Nop.Services\Catalog\ManufacturerService.cs
Currently
            query = query.OrderBy(m => m.DisplayOrder).ThenBy(m => m.Id);
and
            query = query.Distinct().OrderBy(m => m.DisplayOrder).ThenBy(m => m.Id);

Change to
             query = query.OrderBy(m => m.CreatedOnUtc).ThenBy(m => m.Id);
and
            query = query.Distinct().OrderBy(m => m.CreatedOnUtc).ThenBy(m => m.Id);

For Categories
See GetAllCategories(...) in
source\Libraries\Nop.Services\Catalog\CategoryService.cs

Do the similar changes for the query as for Manufacturers
3 years ago
thanks for the quick reply. Sorry, I wasnt clear enough. That will only for one column.

Here is the thread which i am trying to solve the problem with:
https://www.nopcommerce.com/en/boards/topic/46824/how-to-sort-admin-grid

Anyways ,How do i pass that param to the searchModel , asc and desc value for the column header that is clicked.

Thanks again for prompt reply.
1 year ago
Yidna wrote:
Have a look in the
    src\Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs
the routine
    public virtual ProductListModel PrepareProductListModel(ProductSearchModel searchModel)

The data comes from the ProductLoadAllPaged Store Procedure via the SearchProducts Routine there is an OrderBy parameter but it is not passed in so have a look at how that works

Looking in the stored procedure the values for the OrderBy are
  @OrderBy      
int = 0, --0 - position, 5 - Name: A to Z, 6 - Name: Z to A, 10 - Price: Low to High, 11 - Price: High to Low, 15 - creation date


hello, I couldn't do what you said, I'm using version 4.30, but I can't bring the ranking section on the admin page like on the normal product page. Can you give a clearer example?
1 year ago
HoLyDream wrote:
hello, I couldn't do what you said, I'm using version 4.30, but I can't bring the ranking section on the admin page like on the normal product page. Can you give a clearer example?


Please try this....
1. Presentation\Nop.Web\Areas\Admin\Models\Catalog\ProductSearchModel.cs
 public partial record ProductSearchModel : BaseSearchModel
    {
        public ProductSearchModel()
        {
            ........
            AvailableProductTypes = new List<SelectListItem>();
            AvailablePublishedOptions = new List<SelectListItem>();

         //add
           OrderByOptions = new List<SelectListItem>();

        }
        [NopResourceDisplayName("Admin.Catalog.Products.List.SearchProductName")]
        public string SearchProductName { get; set; }
       ............................
        [NopResourceDisplayName("Admin.Catalog.Products.List.GoDirectlyToSku")]
        public string GoDirectlyToSku { get; set; }

      //add
       [NopResourceDisplayName("Admin.Catalog.Products.List.OrderBy")]
        public int OrderBy { get; set; }

        ...................
        public IList<SelectListItem> AvailableProductTypes { get; set; }

        public IList<SelectListItem> AvailablePublishedOptions { get; set; }

       //add
       public IList<SelectListItem> OrderByOptions { get; set; }
    }


2: add
Task PrepareProductOrdersAsync(IList<SelectListItem> items, bool withSpecialDefaultItem = true, string defaultItemText = null);

to   Presentation\Nop.Web\Areas\Admin\Factories\IBaseAdminModelFactory.cs

3: add
    
 public virtual async Task PrepareProductOrdersAsync(IList<SelectListItem> items, bool withSpecialDefaultItem = true, string defaultItemText = null)
        {
            if (items == null)
                throw new ArgumentNullException(nameof(items));

            //prepare available product orders
            var availableProductOrderItems = await ProductSortingEnum.Position.ToSelectListAsync(false);
            foreach (var productOrderItem in availableProductOrderItems)
            {
                items.Add(productOrderItem);
            }

            //insert special item for the default value
            await PrepareDefaultItemAsync(items, withSpecialDefaultItem, defaultItemText);
        
}

to  Presentation\Nop.Web\Areas\Admin\Factories\BaseAdminModelFactory.cs

4.  modify
public virtual async Task<ProductSearchModel> PrepareProductSearchModelAsync(ProductSearchModel searchModel)
        {
            if (searchModel == null)
                throw new ArgumentNullException(nameof(searchModel));
             ......................
            //prepare available vendors
            await _baseAdminModelFactory.PrepareVendorsAsync(searchModel.AvailableVendors);

            //prepare available product types
            await  _baseAdminModelFactory.PrepareProductTypesAsync(searchModel.AvailableProductTypes);
              
             //add
            //prepare available product orders
            await _baseAdminModelFactory.PrepareProductOrdersAsync(searchModel.OrderByOptions, false);


            //prepare available warehouses
            await _baseAdminModelFactory.PrepareWarehousesAsync(searchModel.AvailableWarehouses);
           ....................
            return searchModel;
        }

to  Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs

5. modify Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs
public virtual async Task<ProductListModel> PrepareProductListModelAsync(ProductSearchModel searchModel)
        {
            if (searchModel == null)
                throw new ArgumentNullException(nameof(searchModel));
            ....................
            if (searchModel.SearchIncludeSubCategories && searchModel.SearchCategoryId > 0)
            {
                var childCategoryIds = await _categoryService.GetChildCategoryIdsAsync(parentCategoryId: searchModel.SearchCategoryId, showHidden: true);
                categoryIds.AddRange(childCategoryIds);
            }

            //get products
            var products = await _productService.SearchProductsAsync(showHidden: true,
                categoryIds: categoryIds,
                manufacturerIds: new List<int> { searchModel.SearchManufacturerId },
                storeId: searchModel.SearchStoreId,
                vendorId: searchModel.SearchVendorId,
                warehouseId: searchModel.SearchWarehouseId,
                productType: searchModel.SearchProductTypeId > 0 ? (ProductType?)searchModel.SearchProductTypeId : null,
                keywords: searchModel.SearchProductName,
                pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize,
            orderBy: (ProductSortingEnum)searchModel.OrderBy,
                overridePublished: overridePublished);
            .........................


to  Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs

6.  modify
    <div class="form-group row" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchManufacturerId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                            </div>
                                        </div>
                                        <div class="form-group row" @(Model.AvailableVendors.SelectionIsNotPossible() || Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="SearchVendorId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <div class="col-md-4">
                                                <nop-label asp-for="OrderBy" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="OrderBy" asp-items="Model.OrderByOptions" />
                                            </div>
                                        </div>
                                

                          .......................................
                          <div class="card-body">
                            <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.Products", Docs.Products + Utm.OnAdmin)" />

                              @await Html.PartialAsync("Table", new DataTablesModel
                         {
                             Name = "products-grid",
                             UrlRead = new DataUrl("ProductList", "Product", null),
                             SearchButtonId = "search-products",
                             Length = Model.PageSize,
                             LengthMenu = Model.AvailablePageSizes,
                             Filters = new List<FilterParameter>
                                  {
                                      new FilterParameter(nameof(Model.SearchProductName)),
                                      new FilterParameter(nameof(Model.SearchCategoryId)),
                                      new FilterParameter(nameof(Model.SearchIncludeSubCategories), typeof(bool)),
                                      new FilterParameter(nameof(Model.SearchManufacturerId)),
                                      new FilterParameter(nameof(Model.SearchStoreId)),
                                      new FilterParameter(nameof(Model.SearchWarehouseId)),
                                      new FilterParameter(nameof(Model.SearchVendorId)),
                                      new FilterParameter(nameof(Model.SearchProductTypeId)),
                                      new FilterParameter(nameof(Model.SearchPublishedId)),
                                     new FilterParameter(nameof(Model.OrderBy))
                                  },
                         ....................................

to  Presentation\Nop.Web\Areas\Admin\Views\Product\List.cshtml
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.