MarkAsNewProduct filter is not working in nopcommerce 4.2.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
I am added checkbox for filtering "MarkAsNewProduct" in product category page of nopcommerce4.2.

let see the image first,



In this image I am filtering three thing on it. i.e. filtering vendor => category => markasnewproduct

Now vendor and category filter is happening perfectly while when I check the checkbox for the filter of markasnewproduct at that time filter is not working as per the new product.

Here is my controller code,

[HttpsRequirement(SslRequirement.No)]
public virtual IActionResult Vendor(int vendorId, CatalogPagingFilteringModel command, int categoryId, bool markAsNewProduct)        
{
    //int abc = Convert.ToInt32(ViewBag.CategoryId);
    var vendor = _vendorService.GetVendorById(vendorId);
    if (vendor == null || vendor.Deleted || !vendor.Active)
        return InvokeHttp404();

    //'Continue shopping' URL
    _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer,
        NopCustomerDefaults.LastContinueShoppingPageAttribute,
        _webHelper.GetThisPageUrl(false),
        _storeContext.CurrentStore.Id);

    //display "edit" (manage) link
    if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageVendors))
        DisplayEditLink(Url.Action("Edit", "Vendor", new { id = vendor.Id, area = AreaNames.Admin }));

    //model
    var model = _catalogModelFactory.PrepareVendorModel(vendor, command, categoryId, markAsNewProduct);
    return View(model);
}


Here is the view,

@model VendorModel
@using Nop.Core.Domain.Seo
@inject Nop.Core.IWebHelper webHelper
@inject SeoSettings seoSettings
@{
    Layout = "_ColumnsTwo";

    //title
    Html.AddTitleParts(!string.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Name);
    //meta
    Html.AddMetaDescriptionParts(Model.MetaDescription);
    Html.AddMetaKeywordParts(Model.MetaKeywords);
    //page class
    Html.AppendPageCssClassParts("html-vendor-page");

    if (seoSettings.CanonicalUrlsEnabled)
    {
        var vendorUrl = Url.RouteUrl("Vendor", new { SeName = Model.SeName }, webHelper.CurrentRequestProtocol).ToLowerInvariant();
        Html.AddCanonicalUrlParts(vendorUrl, seoSettings.QueryStringInCanonicalUrlsEnabled);
    }
}
<form asp-route="Vendor" method="post">
    <input type="hidden" value="@Model.CategoryId" id="CategoryId" name="categoryId" />

    <input type="submit" value="SUBMIT" id="submit" style="display: none;" />

    <div class="page vendor-page">
        <div class="page-title">
            <h1>@Model.Name</h1>
        </div>
        <div class="page-body">
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.VendorDetailsTop, additionalData = Model })
            <div class="contact-vendor">

                <input type="button" value="@T("ContactVendor")" class="button-2 contact-vendor-button"
                       onclick="setLocation('@Url.RouteUrl("Vendordetail", new { vendorId = Model.Id })')" />
                <input asp-for="markAsNewProduct" onclick="MarkAsNewProductCheck()" /> MarkAsNewProduct

            </div>

            @if (Model.Products.Count > 0)
            {
                @await Html.PartialAsync("_CatalogSelectors", Model.PagingFilteringContext)
            }
            @*product list*@
            @if (Model.Products.Count > 0)
            {
                <div class="@(Model.PagingFilteringContext.ViewMode == "list" ? "product-list" : "product-grid")">
                    <div class="item-grid">
                        @foreach (var product in Model.Products)
                        {
                            <div class="item-box">
                                @await Html.PartialAsync("_ProductBox", product)
                            </div>
                        }
                    </div>
                </div>
            }
            @{
                var pager = Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber");
            }
            @if (!pager.IsEmpty())
            {
                <div class="pager">
                    @pager
                </div>
            }
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.VendorDetailsBottom, additionalData = Model })
        </div>
    </div>
</form>
<script asp-location="Footer">
    function MarkAsNewProductCheck() {
        $('#submit').trigger('click');
    }
</script>


Now, in this code controller code i.e.

public virtual IActionResult Vendor(int vendorId, CatalogPagingFilteringModel command, int categoryId, bool markAsNewProduct)

in the parameter markasnewproduct value is right coming. For ex: if I uncheck the checkbox then it shows the false value while when I check the checkbox it shows me the true value.
4 years ago
But, you've not shown us the code for PrepareVendorModel, where you've added new parameters:

var model = _catalogModelFactory.PrepareVendorModel(vendor, command, categoryId, markAsNewProduct);

That's where you should debug your query.
4 years ago
Here is the PrepareVendorModel Code

public virtual VendorModel PrepareVendorModel(Vendor vendor, CatalogPagingFilteringModel command, int categoryId, bool markAsNewProduct = false)
{
  if (vendor == null)
    throw new ArgumentNullException(nameof(vendor));

  var model = new VendorModel
  {
    Id = vendor.Id,
    Name = _localizationService.GetLocalized(vendor, x => x.Name),
    Description = _localizationService.GetLocalized(vendor, x => x.Description),
    MetaKeywords = _localizationService.GetLocalized(vendor, x => x.MetaKeywords),
    MetaDescription = _localizationService.GetLocalized(vendor, x => x.MetaDescription),
    MetaTitle = _localizationService.GetLocalized(vendor, x => x.MetaTitle),
    SeName = _urlRecordService.GetSeName(vendor),
    AllowCustomersToContactVendors = _vendorSettings.AllowCustomersToContactVendors,
    CategoryId = categoryId,
    markAsNewProduct = markAsNewProduct
  };

  //sorting
  PrepareSortingOptions(model.PagingFilteringContext, command);
  //view mode
  PrepareViewModes(model.PagingFilteringContext, command);
  //page size
  PreparePageSizeOptions(model.PagingFilteringContext, command,
    vendor.AllowCustomersToSelectPageSize,
    vendor.PageSizeOptions,
    vendor.PageSize);
  
  //products
  var products = _productService.SearchProducts(out IList<int> filterableSpecificationAttributeOptionIds,
    true,
    categoryIds: new List<int> { categoryId },                
    vendorId: vendor.Id,
    storeId: _storeContext.CurrentStore.Id,
    visibleIndividuallyOnly: true,
    orderBy: (ProductSortingEnum)command.OrderBy,
    pageIndex: command.PageNumber - 1,
    pageSize: command.PageSize,
    markedAsNewOnly:markAsNewProduct);
  model.Products = _productModelFactory.PrepareProductOverviewModels(products).ToList();

  model.PagingFilteringContext.LoadPagedList(products);

  return model;
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.