wc_get_dimension()WC 1.0

Normalise dimensions, unify to cm then convert to wanted unit value.

Usage: wc_get_dimension( 55, 'in' ); wc_get_dimension( 55, 'in', 'm' );

No Hooks.

Return

float.

Usage

wc_get_dimension( $dimension, $to_unit, $from_unit );
$dimension(int|float) (required)
Dimension.
$to_unit(string) (required)
Unit to convert to. Options: 'in', 'mm', 'cm', 'm'.
$from_unit(string)
Unit to convert from. Options: 'in', 'mm', 'cm', 'm'.
Default: ''

wc_get_dimension() code WC 8.6.1

function wc_get_dimension( $dimension, $to_unit, $from_unit = '' ) {
	$to_unit = strtolower( $to_unit );

	if ( empty( $from_unit ) ) {
		$from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) );
	}

	// Unify all units to cm first.
	if ( $from_unit !== $to_unit ) {
		switch ( $from_unit ) {
			case 'in':
				$dimension *= 2.54;
				break;
			case 'm':
				$dimension *= 100;
				break;
			case 'mm':
				$dimension *= 0.1;
				break;
			case 'yd':
				$dimension *= 91.44;
				break;
		}

		// Output desired unit.
		switch ( $to_unit ) {
			case 'in':
				$dimension *= 0.3937;
				break;
			case 'm':
				$dimension *= 0.01;
				break;
			case 'mm':
				$dimension *= 10;
				break;
			case 'yd':
				$dimension *= 0.010936133;
				break;
		}
	}

	return ( $dimension < 0 ) ? 0 : $dimension;
}