0

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-applications">Applications: ' . $product->get_attribute($taxonomy) . '</span>';
    }


1 Answer 1

0

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 );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.