Display Woocommerce variable product dimensions

Have Woocommerce setup with a range of variable products. In the variable tab I’ve setup a unique price, image, description, weight & dimensions for each item.

All variable data displays as expected on the front-end except the dimensions & weight.

Read More

Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.

Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.

Anyone know how to get each variable’s dimensions & weight to display beneath the variable description?

Related posts

2 comments

  1. If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under “Magic Properties”).

    To get the variations for a given product, you can use the global $product and then the get_available_variations() function.

    global $product
    $variations = $product->get_available_variations();
    foreach ( $variations as $variable_array ){
        $variation = new WC_Product( $variable_array['variation_id'] );
        echo "The length is {$variation->length}.";
    }
    
  2. If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:

    add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
    
    function tim_additional_tab( $product ){
    
        $variations = $product->get_available_variations();
        //print the whole array in additional tab and examine it
        //echo '<pre>';
        //print_r($variations);
        //echo '</pre>';
        //html and style to your likings
        foreach ( $variations as $key ){
            echo $key['image']['title'].'<br>';
            echo $key['weight_html'].'<br>';
            echo $key['dimensions_html'].'<br>';
        }
    
    }
    

Comments are closed.