get_dimensions() for displaying dimensions deprecated in woocommerce 3

Posted by on Jun 15, 2017 in Blog, PHP, Woocommerce | No Comments

The code to display product dimensions has changed in woocommerce 3

This is my old code which was in my child themes functions.php file. It displayed the product dimensions next to the label ‘Dimensions:’ on the single product page only if dimensions had been entered in the backend. If no dimensions were entered nothing appeared not even the label. I had some more stuff going on in this function but this is stripped down to just displaying the dimensions in a div with a label and some classes applied.

add_action( 'woocommerce_single_product_summary', 'baba_spec', 35 );

function baba_spec() { 
	print '<div class="baba-specifications">';
	global $product;
	$dimensions = $product->get_dimensions();
 		if ( ! empty( $dimensions ) ) {
			$label = __( 'Dimensions: ', 'babasouktextdomain' );
			print '<span class="wpcf-field-name">' . $label . '</span>'; 
			echo '<span class="dimensions">' . $dimensions . '</span>';
		}
			print '</div>';
}

The problem

The original method ‘get_dimensions()’ is deprecated in woocommerce 3, it will still work for a while although it throws an error in debug mode. In the code above I say if ‘if ( ! empty( $dimensions ) )’ which basically says if dimensions is NOT empty show the label and the contents of woocommerce dimensions. This worked fine till woocommerce 3 but now when there are no dimensions ‘get_dimensions’ returns ‘N/A’. That means it is not empty therefor the label appears on the frontend and the N/A displays, not what I wanted.

The solution

It took me a while to track down and figure out the correct syntax for my situation so I am posting it for others who may come searching. As you can see below you now fetch the dimensions slightly differently (line 5) but you also need the second change. To check if there are dimensions you now use ‘if ( $product->has_dimensions() )’ instead of ‘if ( ! empty( $dimensions ) )’ (line 6).

add_action( 'woocommerce_single_product_summary', 'baba_spec', 35 );

function baba_spec() { 
	global $product;
	$dimensions = wc_format_dimensions($product->get_dimensions(false));
 		if ( $product->has_dimensions() ) {
			$label = __( 'Dimensions: ', 'babasouktextdomain' );
			print '<span class="wpcf-field-name">' . $label . '</span>'; 
			echo '<span class="dimensions">' . $dimensions . '</span>';
		}
			print '</div>';
}

Tools:

You may edit your PHP outside WordPress and upload it to your server. You must use an appropriate code editor NOT microsoft word or anything like that which adds a lot of invisible formatting and will be a disaster.
If you do not have one, 2 excellent free ones are…
TextWrangler for Mac, http://www.barebones.com/products/textwrangler/
Notepad++ for the PC http://notepad-plus-plus.org/

Child Themes

You want to change code in a way that will not be over-written when you update your theme. So if you do not already have one you should setup a child theme. I always do this if I am making any changes to the main theme, even if it is only CSS. That is not the topic of this post so here is a link to a tutorial on creating a child theme, you will find the WordPress codex article here.

Leave a Reply