Show only products in stock

Is there an easy way to hide products that’s not in stock and just display in stock products in product lists and search?

Litium version: 7.2

Yes, you could modify the search by adding filters that any connected inventory needs to have at least 1 in stock. Try the below, I took it from something similar so it’s not properly tested, but should be a starting point if not.

Add this method to SearchQueryBuilder:

public void ApplyInStockFilterTags()
{
	var country = _requestModelAccessor.RequestModel.CountryModel.Country;

	var inventorySystemIds = _inventoryService.GetAll()
		.Where(inventory => inventory.CountryLinks
			.Any(x => x.CountrySystemId == country.SystemId))
		.Select(x => x.SystemId)
		.ToList();

	var inventoryQuantityClause = new OptionalTagClause();

	foreach (var inventory in inventorySystemIds)
	{
		inventoryQuantityClause.Tags.Add(new RangeTag(TagNames.GetTagNameForWarehouseQuantity(inventory), 1, 999999));
	}
	
	_request.FilterTags.Add(inventoryQuantityClause);
}

And then in ProductSearchService, for the Search method add:

searchQueryBuilder.ApplyInStockFilterTags();
1 Like

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