WooCommerce: Display Height, Length, Width @ Shop Page

Another interesting snippet that could come very handy. How do we show the product dimensions (height, width, length) in the shop / category / tag / loop pages? This could be a handy trick for shops that calculate shipping rates based on volume, or when the volume is a vital piece of data customers need to know before proceeding further. Either way, enjoy!

WooCommerce: show product height, width, length @ loop

PHP Snippet: Show Product Height, Length, Width @ WooCommerce Shop Page

/**
 * @snippet       Display Product Height, Length, Width @ Shop Page - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 3.9
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_show_product_dimensions_loop', 20 );
 
function bbloomer_show_product_dimensions_loop() {
   global $product;
   $dimensions = $product->get_dimensions();
   if ( ! empty( $dimensions ) ) {
      echo '<div class="dimensions"><b>Height:</b> ' . $product->get_height() . get_option( 'woocommerce_dimension_unit' );
      echo '<br><b>Width:</b> ' . $product->get_width() . get_option( 'woocommerce_dimension_unit' );
      echo '<br><b>Length:</b> ' . $product->get_length() . get_option( 'woocommerce_dimension_unit' );
      echo '</div>';        
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

27 thoughts on “WooCommerce: Display Height, Length, Width @ Shop Page

  1. Thank you, this was just what I was looking for.

  2. Hey there, is it possible to show dimensions in CM and Inches at the same time? So ideal enter in CM but convert and show in CM and Inches in the front end?

    Thanks,
    Darren

    1. Hi Darren, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi,

    This is great. How can we display attributes? For example, I have a “Size” attribute that I would like to display in the loop.

    I guess small changes in your code would do the job?

    1. Hi Ahmed, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Useful function. Thanks!

    1. You’re welcome Slavi 🙂

  5. It worked for me. Thank’s a lot!

  6. Great, just what I needed to show the dimensions on the short description without using the tabs and it also fixed the direction problem with the units

    1. Great to hear that Rubb 🙂

    2. Oh please! Tell me how? I thought using ‘woocommerce_before_main_content’ instead of ‘woocommerce_after_shop_loop_item’ would do it, but no…

    3. Oh wow, nevermind… I just found a very useful hook visual guide right here. That’s awesome Rodolfo!

      https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

    4. How about variable products though?

      1. That’s a little more complex. Also, which variation would you pick? Maybe it should be disabled for variable products 🙂

  7. Hi,
    I found the answer to my own question. In woocommerce 3 get_dimensions() is deprecated. Besides throwing an error the old way is not empty anymore when there are no dimensions, they actually return N/A which means that displays. Hope this helps others. In your code example

    the lines

    $dimensions = $product->get_dimensions();
            if ( ! empty( $dimensions ) )

    change to

    $dimensions = wc_format_dimensions($product->get_dimensions(false));
    
            if ( $product->has_dimensions() )

    reference for this is here
    https://docs.woocommerce.com/document/display-product-dimensions-on-archive-pages/

    1. Excellent, thank you so much Mark!

      1. You’re welcome Iggy!

  8. Hi, I have been using $product->get_dimensions(); in my functions file for a long time. I turned on debug recently when working on something else on got this notice…
    “Notice: WC_Product::get_dimensions was called with an argument that is deprecated since version 3.0! By default, get_dimensions has an argument set to true so that HTML is returned. This is to support the legacy version of the method. To get HTML dimensions, instead use wc_format_dimensions() function”

  9. This works great for my shop but how would I apply it to only one specific category and not all products? For example if I only want to show dimensions for baths and not chemicals?

      1. Perfect!! Thank you so much!

  10. Hi,

    The snippet works beautifully.I would like know how to do like if we click on an icon it would display the dimensions or hover on icon it would display the dimensions. instead of showing it directly. i guess it would add more style to the shop page as well.

    thanks kavi

    1. Hey KAVIARASU, thanks for your comment! You can do that with some CSS styling, unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *