Display WooCommerce size product attribute on shop page

Your code is outdated since WooCommerce 3.
First, you need to target variable products type only to avoid errors on other products types and also $product is already the product object.

Also you can also directly use the WC_Product method get_attribute() and your code will be much more simpler, compact and efficient:

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

    if ( $product->is_type('variable') ) {
        $taxonomy = 'pa_size';
        echo '<span class="attribute-size">' . $product->get_attribute($taxonomy) . '</span>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Leave a Comment