How to target all woocommerce categories that don’t have any subcategories in them?


I want to target all pages (product categories) that are displaying the products themselves.

My woocommerce settings set to show only categories/subcategories (unless there are none) and then by default the products grid is displayed on the page.

is_product_category() function doesn’t help since it also targets the parent categories which don’t have direct products in them.

Most of my categories that don’t any subcategories in them are grandchildren categories if it matters for the answer.

How can I achieve it and to use it in woocommerce hooks?