Filter to show only categories and products, that have products marked "for sale"

I am doing product catalog. All products will be for rent, and some products - for sale.

Here is structure Šildytuvai, drėgmės surinkėjai | Easy Rent :

  • Categories
    – Subcategories
    — Products

What would be the solution to do filter, that would limit categories and subcategories to only those, that have products, marked with some field (“for sale”)

Example for categories:

$categories = page('whateverTheCategoriesParentIsCalled')->children();
$forSaleCategories = $categories->filter(function($item) {
  return $item->index()->template('product')->filterBy('someField', 'for-sale')->isNotEmpty();
});

Subcategories works similarly.

Thank you, do I understand right: I still have to create new templates for this? Just the idea was to use same templates. Now I have: products.php (showing categories), category.php (showing categories or products) and product.php

I just proposed how you can filter your categories. You don’t need new templates.