Hide heading according to query loop

I have a heading “SUBCATEGORIES” above a query loop container. In the container product subcategories are shown. I there aren’t any subcategories how can I hide the heading?

You write a function for yourself.
On the section, on the div, on the title, you set the display condition as you like.
For example:

{echo:bl_publish()}
=
1

And two function options:

function bl_publish() {

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => array(505, 506) 
            )
        )
    );
    $query = new WP_Query($args);

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            // Проверка статуса поста
            $post_status = get_post_status();

            if (in_array($post_status, array('received_pvz', 'delivered', 'in_transit', 'arrived'))) {
                wp_reset_postdata();
                return 1;
            }


        }

    }

    wp_reset_postdata();

}

OR

function bl_publish(): int
{
    global $wpdb;

    $category_ids = array(505, 506); // Замените 505, 506 на фактические ID категорий WooCommerce, которые вы хотите включить

    $term_taxonomy_ids = $wpdb->get_col($wpdb->prepare(
        "SELECT tt.term_taxonomy_id
        FROM $wpdb->term_taxonomy AS tt
        INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN $wpdb->posts AS p ON p.ID = tr.object_id
        WHERE tt.taxonomy = 'product_cat'
        AND tt.term_id IN (%s)
        AND p.post_type = 'product'
        AND p.post_status IN ('received_pvz', 'delivered', 'in_transit', 'arrived')",
        implode(',', $category_ids)
    ));

    return count($term_taxonomy_ids) > 0 ? 1 : 0;
}