Search code examples
phpwordpresswoocommerceattributesproduct-variations

Display the variations weight on a variable product page in Woocommerce?


When the user clicks on a product variant of a dropdown of my products' variants, I would like to also have the weight of each variant. How would I do this? I have been trying to do it like this, but it is not working:

add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
function wb_change_product_html( $price ) {
    global $product;
    $weight = $product->get_weight();
    $price_html = '<span class="amount">' . $price . $weight . '</span>';
    return $price_html;
}

Solution

  • The following code will append to the variation formatted price, the variation formatted weight:

    // Append the formatted variation weight to the variation formatted price
    add_filter('woocommerce_available_variation', 'display_variation_weight', 10, 3 );
    function display_variation_weight( $variation_data, $product, $variation ) {
        $variation_data['price_html'] .= '<span class="weight">' . $variation_data['weight_html'] . '</span>';
    
        return $variation_data;
    }
    

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

    enter image description here