Always display products instead of subcategories

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 13 ans
Hi all,
the navigation in nopcommerce works like this: for each category that contains subcategories, a grind of subcategories is displayed. For each category that has no child categories, a grind of products that belong to this category is displayed.

Is it possible to always show product grinds, no matter if there are subcategories or not?

-----------------------------------------
Category 1
     subcategory 1.1
     subcategory 1.2
Category 2

What I mean is that if user selects "Category 1", there should be displayed alla products that belong to subcategory 1.1 AND products that belong to subcategory 1.2.
------------------------------------------

Can you help me in this?
Il y a 13 ans
I made some modifications on ProductsInGrid.ascx.cs. For now, I have only managed to get the first page of products for each subcategory. I need to get ALL products from each subcategory though, not just the set of products of first page from each subcategory. This is the code I'm using:

if (subCategories.Count > 0)
            {
                var productCollection = ProductManager.GetAllProducts(this.CategoryId,
                    0, 0, false, minPriceConverted, maxPriceConverted,
                    string.Empty, false, pageSize, this.CurrentPageIndex,
                    psoFilterOption, orderBy, out totalRecords);

                foreach (var cat in subCategories)
                {
                    var subcatProducts = ProductManager.GetAllProducts(cat.CategoryId,
                    0, 0, false, minPriceConverted, maxPriceConverted,
                    string.Empty, false, pageSize, this.CurrentPageIndex,
                    psoFilterOption, orderBy, out totalRecords);

                    productCollection.AddRange(subcatProducts);
                }
                

                if (productCollection.Count > 0)
                {
                    this.productsPager.PageSize = pageSize;
                    this.productsPager.TotalRecords = totalRecords;
                    this.productsPager.PageIndex = this.CurrentPageIndex;

                    this.dlProducts.DataSource = productCollection;
                    this.dlProducts.DataBind();
                }
                else
                {
                    this.dlProducts.Visible = false;
                    this.pnlSorting.Visible = false;


                }
                if (productCollection.Count == 0)
                {
                    productpager.Attributes.Add("style", "display:none");
                }
                
            }
Il y a 13 ans
Is there any way to fix the previous piece of code? Can you help me?
Il y a 12 ans
Hi ktsixit,

Do you find solution for your idea?
Thank's for reply.

Kurko
Il y a 12 ans
Hi Kurko,
It's been a lot time since I created this post. I have found a solution but I don't remember all the details:) I'm posting the code I created anyway, you could test it and see how it goes.

file: Templates/Categories/ProductsInGrid.ascx. Replace dlProducts datalist with the following:
<div class="product-grid">
        <asp:DataList ID="dlProducts" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
            RepeatLayout="Table" ItemStyle-CssClass="item-box">
            <ItemTemplate>
                <nopCommerce:ProductBox1 ID="ctrlProductBox" Product='<%# Container.DataItem %>'
                    runat="server" />
            </ItemTemplate>
        </asp:DataList>
    </div>
    <div class="clear"></div>



file: Templates/Categories/ProductsInGrid.ascx.cs Replace BindData() and dlSubCategories_ItemDataBound() functions, using the following:
protected void BindData()
{
  var category = CategoryManager.GetCategoryById(this.CategoryId);


  //breadcrumb
  rptrCategoryBreadcrumb.DataSource = CategoryManager.GetBreadCrumb(this.CategoryId);
  rptrCategoryBreadcrumb.DataBind();

  lDescription.Text = category.LocalizedDescription;

  //subcategories
  var subCategories = CategoryManager.GetAllCategoriesByParentCategoryId(category.CategoryId);
  if (subCategories.Count > 0)
  {
    dlSubCategories.Visible = false;
  }
  else
    dlSubCategories.Visible = false;

  //featured products
  var featuredProducts = category.FeaturedProducts;
  if (featuredProducts.Count > 0)
  {
    dlFeaturedProducts.DataSource = featuredProducts;
    dlFeaturedProducts.DataBind();
  }
  else
  {
    pnlFeaturedProducts.Visible = false;
  }

  //price ranges
  this.ctrlPriceRangeFilter.PriceRanges = category.PriceRanges;

  //page size
  int totalRecords = 0;
  int pageSize = 10;
  if (category.PageSize > 0)
  {
    pageSize = category.PageSize;
  }

  //price ranges
  decimal? minPrice = null;
  decimal? maxPrice = null;
  decimal? minPriceConverted = null;
  decimal? maxPriceConverted = null;
  if (ctrlPriceRangeFilter.SelectedPriceRange != null)
  {
    minPrice = ctrlPriceRangeFilter.SelectedPriceRange.From;
    if (minPrice.HasValue)
    {
      minPriceConverted = CurrencyManager.ConvertCurrency(minPrice.Value, NopContext.Current.WorkingCurrency, CurrencyManager.PrimaryStoreCurrency);
    }

    maxPrice = ctrlPriceRangeFilter.SelectedPriceRange.To;
    if (maxPrice.HasValue)
    {
      maxPriceConverted = CurrencyManager.ConvertCurrency(maxPrice.Value, NopContext.Current.WorkingCurrency, CurrencyManager.PrimaryStoreCurrency);
    }
  }

  //specification filter
  var psoFilterOption = ctrlProductSpecificationFilter.GetAlreadyFilteredSpecOptionIds();

  //sorting

  ProductSortingEnum orderBy = ProductSortingEnum.CreatedOn;
  if (SettingManager.GetSettingValueBoolean("Common.AllowProductSorting"))
  {
    CommonHelper.SelectListItem(this.ddlSorting, CommonHelper.QueryStringInt("orderby"));
    orderBy = (ProductSortingEnum)Enum.ToObject(typeof(ProductSortingEnum), int.Parse(ddlSorting.SelectedItem.Value));
  }


  var productCollection = ProductManager.GetAllProducts(this.CategoryId,
    0, 0, false, minPriceConverted, maxPriceConverted,
    string.Empty, false, pageSize, this.CurrentPageIndex,
    psoFilterOption, orderBy, out totalRecords);

  if (productCollection.Count > 0)
  {
    this.productsPager.PageSize = pageSize;
    this.productsPager.TotalRecords = totalRecords;
    this.productsPager.PageIndex = this.CurrentPageIndex;

    this.dlProducts.DataSource = productCollection;
    this.dlProducts.DataBind();
  }
  else
  {
    this.dlProducts.Visible = false;
    this.pnlSorting.Visible = false;
  }


}
protected void dlSubCategories_ItemDataBound(object sender, DataListItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
    var category = e.Item.DataItem as Category;
    string categoryURL = SEOHelper.GetCategoryUrl(category);

    var hlImageLink = e.Item.FindControl("hlImageLink") as HyperLink;
    if (hlImageLink != null)
    {
      hlImageLink.ImageUrl = PictureManager.GetPictureUrl(category.PictureId, SettingManager.GetSettingValueInteger("Media.Category.ThumbnailImageSize", 125), true);
      hlImageLink.NavigateUrl = categoryURL;
      hlImageLink.ToolTip = String.Format(GetLocaleResourceString("Media.Category.ImageLinkTitleFormat"), category.LocalizedName);
      hlImageLink.Text = String.Format(GetLocaleResourceString("Media.Category.ImageAlternateTextFormat"), category.LocalizedName);
    }

    var hlCategory = e.Item.FindControl("hlCategory") as HyperLink;
    if (hlCategory != null)
    {
      hlCategory.NavigateUrl = categoryURL;
      hlCategory.ToolTip = String.Format(GetLocaleResourceString("Media.Category.ImageLinkTitleFormat"), category.LocalizedName);
      hlCategory.Text = Server.HtmlEncode(category.LocalizedName);
    }
  }
}

I think that's all you need. Let me know if that worked for you.
Il y a 12 ans
Version Nopcommerce 1.8? I have tested Nop 1.9 .... but error! Please help me for this.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.