wc_get_dimension( int|float $dimension, string $to_unit, string $from_unit = '' )
Normalise dimensions, unify to cm then convert to wanted unit value.
Description Description
Usage: wc_get_dimension( 55, ‘in’ ); wc_get_dimension( 55, ‘in’, ‘m’ );
Parameters Parameters
- $dimension
-
(Required) Dimension.
- $to_unit
-
(Required) Unit to convert to. Options: 'in', 'm', 'cm', 'm'.
- $from_unit
-
(Optional) Unit to convert from. Defaults to ''. Options: 'in', 'm', 'cm', 'm'.
Default value: ''
Return Return
(float)
Source Source
File: includes/wc-formatting-functions.php
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;
}