Display WooCommerce product attribute on shop page

I am trying to show the attribute of Applications with the word Applications in front of the items that it echos. The code is working for me to echo the items out but I need it so that it only shows the text Applications when there are some with that product.

add_action( 'woocommerce_after_shop_loop_item_title', 'display_applications_attribute', 5 );
function display_applications_attribute() {
    global $product;

    
        $taxonomy = 'pa_applications';
        echo 'span class=attribute-applicationsApplications: ' . $product-get_attribute($taxonomy) . '/span';
    }


Topic woocommerce-offtopic Wordpress

Category Web


Just make a check, if you have any applications available, and if so - echo them. Here is an example:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_applications_attribute', 5 );

function display_applications_attribute() {
    global $product;

    $applications = $product->get_attribute('pa_applications');
    
    if ( $applications ) {
        printf('<span class="attribute-applications">Applications: %1$s</span>', $applications );
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.