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!
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>';
}
}
Thank you, this was just what I was looking for.
Excellent
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
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!
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?
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!
Useful function. Thanks!
You’re welcome Slavi 🙂
It worked for me. Thank’s a lot!
🙂
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
Great to hear that Rubb 🙂
Oh please! Tell me how? I thought using ‘woocommerce_before_main_content’ instead of ‘woocommerce_after_shop_loop_item’ would do it, but no…
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/
How about variable products though?
That’s a little more complex. Also, which variation would you pick? Maybe it should be disabled for variable products 🙂
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
change to
reference for this is here
https://docs.woocommerce.com/document/display-product-dimensions-on-archive-pages/
Excellent, thank you so much Mark!
great, thanks!
You’re welcome Iggy!
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”
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?
Hey Donna, thanks for your comment! Check this out, it should help: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/
Perfect!! Thank you so much!
🙂
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
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