Get products recursively with DataService from top category

Let’s say I want to get all products recursively with DataService, similar to setting “CategoryShowRecursively” to true in SearchQuery in ProductSearchService. Is this possible?

Litium version: 8.2.1

I’m not sure if there’s a way to do it in a single call, but there’s an extension for getting all categories recursively. You can then query for products from that result. In total, 2 direct calls to the database.

Something like:

using (var query = _dataService.CreateQuery<Category>())
{
    query.Filter(f => f.ParentCategorySystemId(parentCategorySystemId, true));
    using (var productQuery = _dataService.CreateQuery<BaseProduct>())
    {
        var products = productQuery.Filter(f => f.Bool(b => b.Should(x =>
        {
            foreach (var categorySystemId in query.ToSystemIdList())
            {
                x.ContainsInCategory(categorySystemId);
            }
        }))).ToList();
    }
} 

You can do the query on Variants as well by changing the type to Variant and using x.ActiveInCategory(...). This would exclude products where the URL is on the base product.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.