WC_Order_Item_Shipping

Order Line Item (shipping).

Defined (1)

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

/includes/class-wc-order-item-shipping.php  
  1. class WC_Order_Item_Shipping extends WC_Order_Item { 
  2.  
  3. /** 
  4. * Order Data array. This is the core order data exposed in APIs since 3.0.0. 
  5. * @since 3.0.0 
  6. * @var array 
  7. */ 
  8. protected $extra_data = array( 
  9. 'method_title' => '',  
  10. 'method_id' => '',  
  11. 'total' => 0,  
  12. 'total_tax' => 0,  
  13. 'taxes' => array( 
  14. 'total' => array(),  
  15. ),  
  16. ); 
  17.  
  18. /** 
  19. |-------------------------------------------------------------------------- 
  20. | Setters 
  21. |-------------------------------------------------------------------------- 
  22. */ 
  23.  
  24. /** 
  25. * Set order item name. 
  26. * @param string $value 
  27. * @throws WC_Data_Exception 
  28. */ 
  29. public function set_name( $value ) { 
  30. $this->set_method_title( $value ); 
  31.  
  32. /** 
  33. * Set method title. 
  34. * @param string $value 
  35. * @throws WC_Data_Exception 
  36. */ 
  37. public function set_method_title( $value ) { 
  38. $this->set_prop( 'name', wc_clean( $value ) ); 
  39. $this->set_prop( 'method_title', wc_clean( $value ) ); 
  40.  
  41. /** 
  42. * Set shipping method id. 
  43. * @param string $value 
  44. * @throws WC_Data_Exception 
  45. */ 
  46. public function set_method_id( $value ) { 
  47. $this->set_prop( 'method_id', wc_clean( $value ) ); 
  48.  
  49. /** 
  50. * Set total. 
  51. * @param string $value 
  52. * @throws WC_Data_Exception 
  53. */ 
  54. public function set_total( $value ) { 
  55. $this->set_prop( 'total', wc_format_decimal( $value ) ); 
  56.  
  57. /** 
  58. * Set total tax. 
  59. * @param string $value 
  60. * @throws WC_Data_Exception 
  61. */ 
  62. protected function set_total_tax( $value ) { 
  63. $this->set_prop( 'total_tax', wc_format_decimal( $value ) ); 
  64.  
  65. /** 
  66. * Set taxes. 
  67. * This is an array of tax ID keys with total amount values. 
  68. * @param array $raw_tax_data 
  69. * @throws WC_Data_Exception 
  70. */ 
  71. public function set_taxes( $raw_tax_data ) { 
  72. $raw_tax_data = maybe_unserialize( $raw_tax_data ); 
  73. $tax_data = array( 
  74. 'total' => array(),  
  75. ); 
  76. if ( ! empty( $raw_tax_data['total'] ) ) { 
  77. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); 
  78. } elseif ( ! empty( $raw_tax_data ) && is_array( $raw_tax_data ) ) { 
  79. // Older versions just used an array. 
  80. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data ); 
  81. $this->set_prop( 'taxes', $tax_data ); 
  82. $this->set_total_tax( array_sum( $tax_data['total'] ) ); 
  83.  
  84. /** 
  85. * Set properties based on passed in shipping rate object. 
  86. * @param WC_Shipping_Rate $tax_rate_id 
  87. * @throws WC_Data_Exception 
  88. */ 
  89. public function set_shipping_rate( $shipping_rate ) { 
  90. $this->set_method_title( $shipping_rate->label ); 
  91. $this->set_method_id( $shipping_rate->id ); 
  92. $this->set_total( $shipping_rate->cost ); 
  93. $this->set_taxes( $shipping_rate->taxes ); 
  94. $this->set_meta_data( $shipping_rate->get_meta_data() ); 
  95.  
  96. /** 
  97. |-------------------------------------------------------------------------- 
  98. | Getters 
  99. |-------------------------------------------------------------------------- 
  100. */ 
  101.  
  102. /** 
  103. * Get order item type. 
  104. * @return string 
  105. */ 
  106. public function get_type() { 
  107. return 'shipping'; 
  108.  
  109. /** 
  110. * Get order item name. 
  111. * @param string $context 
  112. * @return string 
  113. */ 
  114. public function get_name( $context = 'view' ) { 
  115. return $this->get_method_title( $context ); 
  116.  
  117. /** 
  118. * Get title. 
  119. * @param string $context 
  120. * @return string 
  121. */ 
  122. public function get_method_title( $context = 'view' ) { 
  123. $method_title = $this->get_prop( 'method_title', $context ); 
  124. if ( 'view' === $context ) { 
  125. return $method_title ? $method_title : __( 'Shipping', 'woocommerce' ); 
  126. } else { 
  127. return $method_title; 
  128.  
  129. /** 
  130. * Get method ID. 
  131. * @param string $context 
  132. * @return string 
  133. */ 
  134. public function get_method_id( $context = 'view' ) { 
  135. return $this->get_prop( 'method_id', $context ); 
  136.  
  137. /** 
  138. * Get total cost. 
  139. * @param string $context 
  140. * @return string 
  141. */ 
  142. public function get_total( $context = 'view' ) { 
  143. return $this->get_prop( 'total', $context ); 
  144.  
  145. /** 
  146. * Get total tax. 
  147. * @param string $context 
  148. * @return string 
  149. */ 
  150. public function get_total_tax( $context = 'view' ) { 
  151. return $this->get_prop( 'total_tax', $context ); 
  152.  
  153. /** 
  154. * Get taxes. 
  155. * @param string $context 
  156. * @return array 
  157. */ 
  158. public function get_taxes( $context = 'view' ) { 
  159. return $this->get_prop( 'taxes', $context ); 
  160.  
  161. /** 
  162. |-------------------------------------------------------------------------- 
  163. | Array Access Methods 
  164. |-------------------------------------------------------------------------- 
  165. | For backwards compat with legacy arrays. 
  166. */ 
  167.  
  168. /** 
  169. * offsetGet for ArrayAccess/Backwards compatibility. 
  170. * @deprecated Add deprecation notices in future release. 
  171. * @param string $offset 
  172. * @return mixed 
  173. */ 
  174. public function offsetGet( $offset ) { 
  175. if ( 'cost' === $offset ) { 
  176. $offset = 'total'; 
  177. return parent::offsetGet( $offset ); 
  178.  
  179. /** 
  180. * offsetSet for ArrayAccess/Backwards compatibility. 
  181. * @deprecated Add deprecation notices in future release. 
  182. * @param string $offset 
  183. * @param mixed $value 
  184. */ 
  185. public function offsetSet( $offset, $value ) { 
  186. if ( 'cost' === $offset ) { 
  187. $offset = 'total'; 
  188. parent::offsetSet( $offset, $value ); 
  189.  
  190. /** 
  191. * offsetExists for ArrayAccess 
  192. * @param string $offset 
  193. * @return bool 
  194. */ 
  195. public function offsetExists( $offset ) { 
  196. if ( in_array( $offset, array( 'cost' ) ) ) { 
  197. return true; 
  198. return parent::offsetExists( $offset );