Original code with error in values on <https://stackoverflow.com/questions/49630032/display-specific-product-attributes-on-woocommerce-archives-pages>
Fixed code with correct values and added classes. In the array, you need to list the attributes that you want to see. This is pa_ + attribute slug.
// Add product attributes to the shop loop.
add_action('woocommerce_after_shop_loop_item','add_attribute',
41);
function add_attribute() {
global $product;
$product_attributes = array( 'pa_weight', 'pa_dimensions',
'pa_electric-power', 'pa_gas-power' );
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute($taxonomy);
if( ! empty($value) ){
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy. '"><span
class="attribute_label">' . $label_name . ':</span> <span
class="attribute_value">' . $value . '</span></span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode( '<br>',
$attr_output ).'</div>';
}
Code language: PHP (php)