wc_get_dimension

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

Description

(float) wc_get_dimension( (int|float) $dimension, (string) $to_unit, (string) $from_unit = '' ); 

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

Returns (float)

Parameters (3)

0. $dimension (int|float)
The dimension.
1. $to_unit (string)
'in', m,, cm, m,
2. $from_unit — Optional. (string) => ''
in,, m, cm, m

Usage

  1. if ( !function_exists( 'wc_get_dimension' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php'; 
  3.  
  4. // The dimension. 
  5. $dimension = null; 
  6.  
  7. // 'in', 'm', 'cm', 'm' 
  8. $to_unit = ''; 
  9.  
  10. // (optional) 'in', 'm', 'cm', 'm' 
  11. $from_unit = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_get_dimension($dimension, $to_unit, $from_unit); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-formatting-functions.php  
  1. function wc_get_dimension( $dimension, $to_unit, $from_unit = '' ) { 
  2. $to_unit = strtolower( $to_unit ); 
  3.  
  4. if ( empty( $from_unit ) ) { 
  5. $from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) ); 
  6.  
  7. // Unify all units to cm first. 
  8. if ( $from_unit !== $to_unit ) { 
  9. switch ( $from_unit ) { 
  10. case 'in' : 
  11. $dimension *= 2.54; 
  12. break; 
  13. case 'm' : 
  14. $dimension *= 100; 
  15. break; 
  16. case 'mm' : 
  17. $dimension *= 0.1; 
  18. break; 
  19. case 'yd' : 
  20. $dimension *= 91.44; 
  21. break; 
  22.  
  23. // Output desired unit. 
  24. switch ( $to_unit ) { 
  25. case 'in' : 
  26. $dimension *= 0.3937; 
  27. break; 
  28. case 'm' : 
  29. $dimension *= 0.01; 
  30. break; 
  31. case 'mm' : 
  32. $dimension *= 10; 
  33. break; 
  34. case 'yd' : 
  35. $dimension *= 0.010936133; 
  36. break; 
  37.  
  38. return ( $dimension < 0 ) ? 0 : $dimension;