WC_Order_Factory

Order Factory Class.

Defined (1)

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

/includes/class-wc-order-factory.php  
  1. class WC_Order_Factory { 
  2.  
  3. /** 
  4. * Get order. 
  5. * @param mixed $order_id (default: false) 
  6. * @return WC_Order|bool 
  7. */ 
  8. public static function get_order( $order_id = false ) { 
  9. $order_id = self::get_order_id( $order_id ); 
  10.  
  11. if ( ! $order_id ) { 
  12. return false; 
  13.  
  14. $order_type = WC_Data_Store::load( 'order' )->get_order_type( $order_id ); 
  15. if ( $order_type_data = wc_get_order_type( $order_type ) ) { 
  16. $classname = $order_type_data['class_name']; 
  17. } else { 
  18. $classname = false; 
  19.  
  20. // Filter classname so that the class can be overridden if extended. 
  21. $classname = apply_filters( 'woocommerce_order_class', $classname, $order_type, $order_id ); 
  22.  
  23. if ( ! class_exists( $classname ) ) { 
  24. return false; 
  25.  
  26. try { 
  27. return new $classname( $order_id ); 
  28. } catch ( Exception $e ) { 
  29. return false; 
  30.  
  31. /** 
  32. * Get order item. 
  33. * @param int 
  34. * @return WC_Order_Item|false if not found 
  35. */ 
  36. public static function get_order_item( $item_id = 0 ) { 
  37. global $wpdb; 
  38.  
  39. if ( is_numeric( $item_id ) ) { 
  40. $item_type = WC_Data_Store::load( 'order-item' )->get_order_item_type( $item_id ); 
  41. $id = $item_id; 
  42. } elseif ( $item_id instanceof WC_Order_Item ) { 
  43. $item_type = $item_id->get_type(); 
  44. $id = $item_id->get_id(); 
  45. } elseif ( is_object( $item_id ) && ! empty( $item_id->order_item_type ) ) { 
  46. $id = $item_id->order_item_id; 
  47. $item_type = $item_id->order_item_type; 
  48. } else { 
  49. $item_data = false; 
  50. $item_type = false; 
  51. $id = false; 
  52.  
  53. if ( $id && $item_type ) { 
  54. $classname = false; 
  55. switch ( $item_type ) { 
  56. case 'line_item' : 
  57. case 'product' : 
  58. $classname = 'WC_Order_Item_Product'; 
  59. break; 
  60. case 'coupon' : 
  61. $classname = 'WC_Order_Item_Coupon'; 
  62. break; 
  63. case 'fee' : 
  64. $classname = 'WC_Order_Item_Fee'; 
  65. break; 
  66. case 'shipping' : 
  67. $classname = 'WC_Order_Item_Shipping'; 
  68. break; 
  69. case 'tax' : 
  70. $classname = 'WC_Order_Item_Tax'; 
  71. break; 
  72. default : 
  73. $classname = apply_filters( 'woocommerce_get_order_item_classname', $classname, $item_type, $id ); 
  74. break; 
  75.  
  76. if ( $classname && class_exists( $classname ) ) { 
  77. try { 
  78. return new $classname( $id ); 
  79. } catch ( Exception $e ) { 
  80. return false; 
  81. return false; 
  82.  
  83. /** 
  84. * Get the order ID depending on what was passed. 
  85. * @since 3.0.0 
  86. * @param mixed $order 
  87. * @return int|bool false on failure 
  88. */ 
  89. public static function get_order_id( $order ) { 
  90. global $post; 
  91.  
  92. if ( false === $order && is_a( $post, 'WP_Post' ) && 'shop_order' === get_post_type( $post ) ) { 
  93. return $post->ID; 
  94. } elseif ( is_numeric( $order ) ) { 
  95. return $order; 
  96. } elseif ( $order instanceof WC_Abstract_Order ) { 
  97. return $order->get_id(); 
  98. } elseif ( ! empty( $order->ID ) ) { 
  99. return $order->ID; 
  100. } else { 
  101. return false;