In woocommerce each product attribute is a custom taxonomy and are recorded in database adding pa_
to the beginning of their slugs…
That taxonomy name is to be used with WC_Product get_attribute()
method.
So so your code should be instead:
add_action('woocommerce_after_shop_loop_item','displaying_product_attributes');
function displaying_product_attributes() {
global $product;
$weigth_val = $product->get_attribute('pa_weight');
$quant_val = $product->get_attribute('pa_quantity');
$length_val = $product->get_attribute('pa_length');
echo $weigth_val;
echo $quant_val;
echo $length_val;
}
It should work now…
To get the product attribute name label and with the corresponding name value for products you will use:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
$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('pa_weight');
if( ! empty($value) ){
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.