WPOWCPDF_InvoicesCompatibilityOrder

WooCommerce order compatibility class.

Defined (1)

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

/includes/compatibility/class-wc-order-compatibility.php  
  1. class Order extends Data { 
  2.  
  3.  
  4. /** @var array mapped compatibility properties, as `$new_prop => $old_prop` */ 
  5. protected static $compat_props = array( 
  6. 'date_completed' => 'completed_date',  
  7. 'date_paid' => 'paid_date',  
  8. 'date_modified' => 'modified_date',  
  9. 'date_created' => 'order_date',  
  10. 'customer_id' => 'customer_user',  
  11. 'discount' => 'cart_discount',  
  12. 'discount_tax' => 'cart_discount_tax',  
  13. 'shipping_total' => 'total_shipping',  
  14. 'type' => 'order_type',  
  15. 'currency' => 'order_currency',  
  16. 'version' => 'order_version',  
  17. ); 
  18.  
  19.  
  20. /** 
  21. * Backports WC_Order::get_id() method to pre-2.6.0 
  22. * @since 4.2.0 
  23. * @param \WC_Order $order order object 
  24. * @return string|int order ID 
  25. */ 
  26. public static function get_id( $order ) { 
  27.  
  28. if ( method_exists( $order, 'get_id' ) ) { 
  29.  
  30. return $order->get_id(); 
  31.  
  32. } else { 
  33.  
  34. return isset($order->id) ? $order->id : false; 
  35.  
  36.  
  37. /** 
  38. * Gets an order property. 
  39. * @since 4.6.0-dev 
  40. * @param \WC_Order $object the order object 
  41. * @param string $prop the property name 
  42. * @param string $context if 'view' then the value will be filtered 
  43. * @return mixed 
  44. */ 
  45. public static function get_prop( $object, $prop, $context = 'edit', $compat_props = array() ) { 
  46.  
  47. // backport a few specific properties to pre-3.0 
  48. if ( WC_Core::is_wc_version_lt_3_0() ) { 
  49.  
  50. // converge the shipping total prop for the raw context 
  51. if ( 'shipping_total' === $prop && 'view' !== $context ) { 
  52.  
  53. $prop = 'order_shipping'; 
  54.  
  55. // get the post_parent and bail early 
  56. } elseif ( 'parent_id' === $prop ) { 
  57.  
  58. return $object->post->post_parent; 
  59.  
  60. $value = parent::get_prop( $object, $prop, $context, self::$compat_props ); 
  61.  
  62. // 3.0+ date getters return a DateTime object, where previously MySQL date strings were returned 
  63. if ( WC_Core::is_wc_version_lt_3_0() && in_array( $prop, array( 'date_completed', 'date_paid', 'date_modified', 'date_created' ), true ) && !empty( $value ) ) { 
  64. if ( is_numeric( $value ) ) { 
  65. $value = new WC_DateTime( "@{$value}", new \DateTimeZone( 'UTC' ) ); 
  66. $value->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); 
  67. } else { 
  68. $value = new WC_DateTime( $value, new \DateTimeZone( wc_timezone_string() ) ); 
  69. $value->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); 
  70.  
  71. return $value; 
  72.  
  73.  
  74. /** 
  75. * Sets an order's properties. 
  76. * Note that this does not save any data to the database. 
  77. * @since 4.6.0-dev 
  78. * @param \WC_Order $object the order object 
  79. * @param array $props the new properties as $key => $value 
  80. * @return \WC_Order 
  81. */ 
  82. public static function set_props( $object, $props, $compat_props = array() ) { 
  83.  
  84. return parent::set_props( $object, $props, self::$compat_props ); 
  85.  
  86. /** 
  87. * Backports WC_Order::set_address_prop() to pre-3.0 
  88. * Saves by default. 
  89. * @since 4.6.0-dev 
  90. * @param \WC_Order $order the order object 
  91. * @param string $prop Name of prop to set. 
  92. * @param string $address Name of address to set. billing or shipping. 
  93. * @param mixed $value Value of the prop. 
  94. * @param bool $save whether to save the order/property 
  95. * @return \WC_Order 
  96. */ 
  97. public static function set_address_prop( \WC_Order $order, $prop, $address = 'billing', $value, $save = true ) { 
  98. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  99. if ( is_callable( array( $order, "set_{$address}_{$prop}" ) ) ) { 
  100. $order->{"set_{$address}_{$prop}"}( $value ); 
  101. if ($save === true) { 
  102. $order->save(); 
  103. } else { 
  104. // wc 2.6 or older 
  105. if ($save === true) { 
  106. // store directly in postmeta 
  107. update_post_meta( $order->id, "_{$address}_{$prop}", $value ); 
  108. } else { 
  109. // only change property in the order 
  110. $order->$prop = $value; 
  111.  
  112. return $order; 
  113.  
  114. /** 
  115. * Implements WC_Order::get_item_meta for 3.0+ 
  116. * @param \WC_Order $order the order object 
  117. * @param int $item_id the item id 
  118. * @param int $key the meta key 
  119. * @param boolean $single single or multiple 
  120. * @return mixed item meta 
  121. */ 
  122. public static function get_item_meta( $object, $item_id, $key = '', $single = false ) { 
  123. if (function_exists('wc_get_order_item_meta')) { 
  124. $item_meta = wc_get_order_item_meta( $item_id, $key, $single ); 
  125. } else { 
  126. $item_meta = $object->get_item_meta( $item_id, $key, $single ); 
  127. return $item_meta; 
  128.  
  129. /** 
  130. * Backports WC_Order::get_status() to pre-3.0.0 
  131. * @since 4.6.0-dev 
  132. * @param \WC_Order $order the order object 
  133. * @return string order status 
  134. */ 
  135. public static function get_status( \WC_Order $order ) { 
  136.  
  137. if ( method_exists( $order, 'get_status' ) ) { 
  138. return $order->get_status(); 
  139. } else { 
  140. return $order->status; 
  141.  
  142. /** 
  143. * Order item CRUD compatibility method to add a coupon to an order. 
  144. * @since 4.6.0-dev 
  145. * @param \WC_Order $order the order object 
  146. * @param array $code the coupon code 
  147. * @param int $discount the discount amount. 
  148. * @param int $discount_tax the discount tax amount. 
  149. * @return int the order item ID 
  150. */ 
  151. public static function add_coupon( \WC_Order $order, $code = array(), $discount = 0, $discount_tax = 0 ) { 
  152.  
  153. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  154.  
  155. $item = new \WC_Order_Item_Coupon(); 
  156.  
  157. $item->set_props( array( 
  158. 'code' => $code,  
  159. 'discount' => $discount,  
  160. 'discount_tax' => $discount_tax,  
  161. 'order_id' => $order->get_id(),  
  162. ) ); 
  163.  
  164. $item->save(); 
  165.  
  166. $order->add_item( $item ); 
  167.  
  168. return $item->get_id(); 
  169.  
  170. } else { 
  171.  
  172. return $order->add_coupon( $code, $discount, $discount_tax ); 
  173.  
  174.  
  175. /** 
  176. * Order item CRUD compatibility method to add a fee to an order. 
  177. * @since 4.6.0-dev 
  178. * @param \WC_Order $order the order object 
  179. * @param object $fee the fee to add 
  180. * @return int|\WC_Order_Item the order item ID 
  181. */ 
  182. public static function add_fee( \WC_Order $order, $fee ) { 
  183.  
  184. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  185.  
  186. $item = new \WC_Order_Item_Fee(); 
  187.  
  188. $item->set_props( array( 
  189. 'name' => $fee->name,  
  190. 'tax_class' => $fee->taxable ? $fee->tax_class : 0,  
  191. 'total' => $fee->amount,  
  192. 'total_tax' => $fee->tax,  
  193. 'taxes' => array( 
  194. 'total' => $fee->tax_data,  
  195. ),  
  196. 'order_id' => $order->get_id(),  
  197. ) ); 
  198.  
  199. $item->save(); 
  200.  
  201. $order->add_item( $item ); 
  202.  
  203. return $item->get_id(); 
  204.  
  205. } else { 
  206.  
  207. return $order->add_fee( $fee ); 
  208.  
  209.  
  210. /** 
  211. * Order item CRUD compatibility method to update an order coupon. 
  212. * @since 4.6.0-dev 
  213. * @param \WC_Order $order the order object 
  214. * @param int|\WC_Order_Item $item the order item ID 
  215. * @param array $args { 
  216. * The coupon item args. 
  217. * @type string $code the coupon code 
  218. * @type float $discount the coupon discount amount 
  219. * @type float $discount_tax the coupon discount tax amount 
  220. * } 
  221. * @return int|bool the order item ID or false on failure 
  222. */ 
  223. public static function update_coupon( \WC_Order $order, $item, $args ) { 
  224.  
  225. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  226.  
  227. if ( is_numeric( $item ) ) { 
  228. $item = $order->get_item( $item ); 
  229.  
  230. if ( ! is_object( $item ) || ! $item->is_type( 'coupon' ) ) { 
  231. return false; 
  232.  
  233. if ( ! $order->get_id() ) { 
  234. $order->save(); 
  235.  
  236. $item->set_order_id( $order->get_id() ); 
  237. $item->set_props( $args ); 
  238. $item->save(); 
  239.  
  240. return $item->get_id(); 
  241.  
  242. } else { 
  243.  
  244. // convert 3.0+ args for backwards compatibility 
  245. if ( isset( $args['discount'] ) ) { 
  246. $args['discount_amount'] = $args['discount']; 
  247. if ( isset( $args['discount_tax'] ) ) { 
  248. $args['discount_amount_tax'] = $args['discount_tax']; 
  249.  
  250. return $order->update_coupon( $item, $args ); 
  251.  
  252.  
  253. /** 
  254. * Order item CRUD compatibility method to update an order fee. 
  255. * @since 4.6.0-dev 
  256. * @param \WC_Order $order the order object 
  257. * @param int $item the order item ID 
  258. * @param array $args { 
  259. * The fee item args. 
  260. * @type string $name the fee name 
  261. * @type string $tax_class the fee's tax class 
  262. * @type float $line_total the fee total amount 
  263. * @type float $line_tax the fee tax amount 
  264. * } 
  265. * @return int|bool the order item ID or false on failure 
  266. */ 
  267. public static function update_fee( \WC_Order $order, $item, $args ) { 
  268.  
  269. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  270.  
  271. if ( is_numeric( $item ) ) { 
  272. $item = $order->get_item( $item ); 
  273.  
  274. if ( ! is_object( $item ) || ! $item->is_type( 'fee' ) ) { 
  275. return false; 
  276.  
  277. if ( ! $order->get_id() ) { 
  278. $order->save(); 
  279.  
  280. $item->set_order_id( $order->get_id() ); 
  281. $item->set_props( $args ); 
  282. $item->save(); 
  283.  
  284. return $item->get_id(); 
  285.  
  286. } else { 
  287.  
  288. return $order->update_fee( $item, $args ); 
  289.  
  290.  
  291. /** 
  292. * Backports wc_reduce_stock_levels() to pre-3.0.0 
  293. * @since 4.6.0-dev 
  294. * @param \WC_Order $order the order object 
  295. */ 
  296. public static function reduce_stock_levels( \WC_Order $order ) { 
  297.  
  298. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  299. wc_reduce_stock_levels( $order->get_id() ); 
  300. } else { 
  301. $order->reduce_order_stock(); 
  302.  
  303.  
  304. /** 
  305. * Backports wc_update_total_sales_counts() to pre-3.0.0 
  306. * @since 4.6.0-dev 
  307. * @param \WC_Order $order the order object 
  308. */ 
  309. public static function update_total_sales_counts( \WC_Order $order ) { 
  310.  
  311. if ( WC_Core::is_wc_version_gte_3_0() ) { 
  312. wc_update_total_sales_counts( $order->get_id() ); 
  313. } else { 
  314. $order->record_product_sales(); 
  315.  
  316.