admin管理员组

文章数量:1122832

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


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


Share Improve this question asked Oct 2, 2020 at 21:50 SdesignSdesign 657 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );
    }
}

本文标签: Display WooCommerce product attribute on shop page