WC_Correios_Package

WC_Correios_Package class.

Defined (1)

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

/includes/class-wc-correios-package.php  
  1. class WC_Correios_Package { 
  2.  
  3. /** 
  4. * Order package. 
  5. * @var array 
  6. */ 
  7. protected $package = array(); 
  8.  
  9. /** 
  10. * Sets the package. 
  11. * @param array $package Package to calcule. 
  12. * @return array 
  13. */ 
  14. public function __construct( $package = array() ) { 
  15. $this->package = $package; 
  16.  
  17. /** 
  18. * Extracts the weight and dimensions from the package. 
  19. * @return array 
  20. */ 
  21. protected function get_package_data() { 
  22. $count = 0; 
  23. $height = array(); 
  24. $width = array(); 
  25. $length = array(); 
  26. $weight = array(); 
  27.  
  28. // Shipping per item. 
  29. foreach ( $this->package['contents'] as $item_id => $values ) { 
  30. $product = $values['data']; 
  31. $qty = $values['quantity']; 
  32.  
  33. if ( $qty > 0 && $product->needs_shipping() ) { 
  34.  
  35. $_height = wc_get_dimension( (float) $product->get_length(), 'cm' ); 
  36. $_width = wc_get_dimension( (float) $product->get_width(), 'cm' ); 
  37. $_length = wc_get_dimension( (float) $product->get_height(), 'cm' ); 
  38. $_weight = wc_get_weight( (float) $product->get_weight(), 'kg' ); 
  39.  
  40. $height[ $count ] = $_height; 
  41. $width[ $count ] = $_width; 
  42. $length[ $count ] = $_length; 
  43. $weight[ $count ] = $_weight; 
  44.  
  45. if ( $qty > 1 ) { 
  46. $n = $count; 
  47. for ( $i = 0; $i < $qty; $i++ ) { 
  48. $height[ $n ] = $_height; 
  49. $width[ $n ] = $_width; 
  50. $length[ $n ] = $_length; 
  51. $weight[ $n ] = $_weight; 
  52. $n++; 
  53. $count = $n; 
  54.  
  55. $count++; 
  56.  
  57. return array( 
  58. 'height' => array_values( $height ),  
  59. 'length' => array_values( $length ),  
  60. 'width' => array_values( $width ),  
  61. 'weight' => array_sum( $weight ),  
  62. ); 
  63.  
  64. /** 
  65. * Calculates the cubage of all products. 
  66. * @param array $height Package height. 
  67. * @param array $width Package width. 
  68. * @param array $length Package length. 
  69. * @return int 
  70. */ 
  71. protected function cubage_total( $height, $width, $length ) { 
  72. // Sets the cubage of all products. 
  73. $total = 0; 
  74. $total_items = count( $height ); 
  75.  
  76. for ( $i = 0; $i < $total_items; $i++ ) { 
  77. $total += $height[ $i ] * $width[ $i ] * $length[ $i ]; 
  78.  
  79. return $total; 
  80.  
  81. /** 
  82. * Get the max values. 
  83. * @param array $height Package height. 
  84. * @param array $width Package width. 
  85. * @param array $length Package length. 
  86. * @return array 
  87. */ 
  88. protected function get_max_values( $height, $width, $length ) { 
  89. $find = array( 
  90. 'height' => max( $height ),  
  91. 'width' => max( $width ),  
  92. 'length' => max( $length ),  
  93. ); 
  94.  
  95. return $find; 
  96.  
  97. /** 
  98. * Calculates the square root of the scaling of all products. 
  99. * @param array $height Package height. 
  100. * @param array $width Package width. 
  101. * @param array $length Package length. 
  102. * @param array $max_values Package bigger values. 
  103. * @return float 
  104. */ 
  105. protected function calculate_root( $height, $width, $length, $max_values ) { 
  106. $cubage_total = $this->cubage_total( $height, $width, $length ); 
  107. $root = 0; 
  108. $biggest = max( $max_values ); 
  109.  
  110. if ( 0 !== $cubage_total && 0 < $biggest ) { 
  111. // Dividing the value of scaling of all products. 
  112. // With the measured value of greater. 
  113. $division = $cubage_total / $biggest; 
  114. // Total square root. 
  115. $root = round( sqrt( $division ), 1 ); 
  116.  
  117. return $root; 
  118.  
  119. /** 
  120. * Sets the final cubage. 
  121. * @param array $height Package height. 
  122. * @param array $width Package width. 
  123. * @param array $length Package length. 
  124. * @return array 
  125. */ 
  126. protected function get_cubage( $height, $width, $length ) { 
  127. $cubage = array(); 
  128. $max_values = $this->get_max_values( $height, $width, $length ); 
  129. $root = $this->calculate_root( $height, $width, $length, $max_values ); 
  130. $greatest = array_search( max( $max_values ), $max_values, true ); 
  131.  
  132. switch ( $greatest ) { 
  133. case 'height' : 
  134. $cubage = array( 
  135. 'height' => max( $height ),  
  136. 'width' => $root,  
  137. 'length' => $root,  
  138. ); 
  139. break; 
  140. case 'width' : 
  141. $cubage = array( 
  142. 'height' => $root,  
  143. 'width' => max( $width ),  
  144. 'length' => $root,  
  145. ); 
  146. break; 
  147. case 'length' : 
  148. $cubage = array( 
  149. 'height' => $root,  
  150. 'width' => $root,  
  151. 'length' => max( $length ),  
  152. ); 
  153. break; 
  154.  
  155. default : 
  156. $cubage = array( 
  157. 'height' => 0,  
  158. 'width' => 0,  
  159. 'length' => 0,  
  160. ); 
  161. break; 
  162.  
  163. return $cubage; 
  164.  
  165. /** 
  166. * Get the package data. 
  167. * @return array 
  168. */ 
  169. public function get_data() { 
  170. // Get the package data. 
  171. $data = apply_filters( 'woocommerce_correios_default_package', $this->get_package_data() ); 
  172.  
  173. if ( ! empty( $data['height'] ) && ! empty( $data['width'] ) && ! empty( $data['length'] ) ) { 
  174. $cubage = $this->get_cubage( $data['height'], $data['width'], $data['length'] ); 
  175. } else { 
  176. $cubage = array( 
  177. 'height' => 0,  
  178. 'width' => 0,  
  179. 'length' => 0,  
  180. ); 
  181.  
  182. return array( 
  183. 'height' => apply_filters( 'woocommerce_correios_package_height', $cubage['height'] ),  
  184. 'width' => apply_filters( 'woocommerce_correios_package_width', $cubage['width'] ),  
  185. 'length' => apply_filters( 'woocommerce_correios_package_length', $cubage['length'] ),  
  186. 'weight' => apply_filters( 'woocommerce_correios_package_weight', $data['weight'] ),  
  187. );