Max
Max

Reputation: 13388

How to get all Products within a Category or sub-category

I'm trying to get all the products within one Category, I want to search by CategoryId. So I want to get a list filled with all products where categoryId is for example 3.

How can I do this, I'm using NopCommerce 3.10.

Someone on the Nop forum achieved it using the following line:

_productService.SearchProductVariants(categoryId, 0, string.Empty, false, 0, int.MaxValue, false);

But since I use 3.10 and ProductVariants are replaced by Products, I can't use this.

Thanks in advance!

Upvotes: 2

Views: 1470

Answers (1)

Max
Max

Reputation: 13388

I figured it out myself:

For all products within 1 categoryid:

        NopEngine _engine;
        /// <summary>
        /// Returns IPagedList(Product) filled with all products from selected CategoryId
        /// </summary>
        /// <param name="Categoryid"></param>
        /// <returns></returns>
        public IPagedList<Product> GetAllProductsFromCategory(int Categoryid)
        {
            _engine = new NopEngine();
            var _productService = _engine.Resolve<IProductService>();
            List<int> CategoryIds = new List<int>();
            CategoryIds.Add(Categoryid);
            return _productService.SearchProducts(categoryIds: CategoryIds);
        }

For all products:

        NopEngine _engine;
        /// <summary>
        /// Returns IPagedList(Product) filled with all products, without selection
        /// </summary>
        /// <returns></returns>
        public IPagedList<Product> GetAllProducts()
        {
            _engine = new NopEngine();
            var _allService = _engine.Resolve<IProductService>();
            return _allService.SearchProducts();
        }

Upvotes: 1

Related Questions