Displaying Category Items on Homepage

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 лет назад
Hi All,

I would like to display a number of category's featured products on the store front homepage. More specifcally I would like to display 3 categories on the homepage with the category title and then under each category Title display 4 of each of the categories featured products on the hompage.

Has anyone done this? Is this to be a future feature? Shall I just get on and do it myself?

Thanks

Jacques.
13 лет назад
OK so this turned out to be really simple to achieve.

Modified : /Modules/HomePageCategories.ascx - to inclued the following,


<asp:DataList ID="dlCategoriesWithProducts" runat="server" RepeatLayout=Flow ItemStyle-CssClass="item-box" RepeatDirection="Vertical" OnItemDataBound="dlCategoriesWithProducts_ItemDataBound">
            <ItemTemplate>
                <div class="category-item">
                    <h2 class="title">
                        <asp:HyperLink ID="hlCategory" runat="server" Text='<%#Server.HtmlEncode(Eval("LocalizedName").ToString()) %>' />
                    </h2>
                    <asp:DataList ID="dlProducts" runat="server" RepeatLayout=Flow ItemStyle-CssClass="item-box" RepeatDirection=Horizontal>
                        <ItemTemplate>
                            <nopCommerce:ProductBox1 ID="ctrlProductBox" Product='<%# Container.DataItem %>' runat="server" />
                        </ItemTemplate>
                    </asp:DataList>
                </div>
            </ItemTemplate>
    </asp:DataList>


Modified: /Modules/HomePageCategories.ascx.cs



protected void BindData()
        {
            if (SettingManager.GetSettingValueBoolean("Display.CategoriesOnHompageWithProducts") == false)
            {
                var subCategoryCollection = CategoryManager.GetAllCategoriesDisplayedOnHomePage();
                if (subCategoryCollection.Count > 0)
                {
                    dlCategories.DataSource = subCategoryCollection;
                    dlCategories.DataBind();
                }
                else
                    this.Visible = false;
            }
            else
            {
                var subCategoryCollection = CategoryManager.GetAllCategoriesDisplayedOnHomePage();
                if (subCategoryCollection.Count > 0)
                {
                    dlCategoriesWithProducts.DataSource = subCategoryCollection;
                    dlCategoriesWithProducts.DataBind();
                }
                else
                    this.Visible = false;
            }
        }


New Sub to handle DataBinding of new DataList:


protected void dlCategoriesWithProducts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var category = e.Item.DataItem as Category;
                var dlProducts = e.Item.FindControl("dlProducts") as DataList;
                string categoryURL = SEOHelper.GetCategoryUrl(category);
                if (dlProducts != null)
                {
                    int ProductLimit = SettingManager.GetSettingValueInteger("Display.CategoriesOnHompageWithProducts-PerCategory-ProductCount");
                        int TotalProducts;
                        dlProducts.DataSource = ProductManager.GetAllProducts(category.CategoryId, 0, 0, null, ProductLimit, 0, out TotalProducts);
                        dlProducts.DataBind();
                }

                HyperLink hlCategory = e.Item.FindControl("hlCategory") as HyperLink;
                if (hlCategory != null)
                {
                    hlCategory.NavigateUrl = categoryURL;
                }
            }
        }


I'm loving working with Nopcommerce!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.