WC_POS_Admin_Orders

POS Order Admin Class - only active for $screen-id = edit-shop_order or shop_order - allow users to change order type: POS/Online - allow users to switch POS payment method - filter orders by POS vs Online.

Defined (1)

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

/includes/admin/class-wc-pos-orders.php  
  1. class WC_POS_Admin_Orders { 
  2.  
  3. private $pos_order; 
  4.  
  5. public function __construct() { 
  6.  
  7. // option for order type 
  8. add_action( 'woocommerce_admin_order_data_after_order_details', array($this, 'order_details') ); 
  9. add_action( 'woocommerce_process_shop_order_meta', array($this, 'save'), 10, 2 ); 
  10.  
  11. // payment method dropdown 
  12. if( version_compare( WC()->version, '2.3', '>' ) ) { 
  13. add_filter( 'woocommerce_payment_gateways', array($this, 'payment_gateways'), 20, 1 ); 
  14.  
  15. // pos vs online filter on edit-shop_order page 
  16. add_filter( 'views_edit-shop_order', array( $this, 'pos_order_filters' ), 10, 1 ); 
  17. add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 ); 
  18.  
  19. // add class to pos order rows 
  20. add_filter( 'post_class', array( $this, 'post_class' ), 10, 3 ); 
  21. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); 
  22.  
  23.  
  24. /** 
  25. * Add select dropdown 
  26. * @param $order 
  27. */ 
  28. public function order_details($order) { 
  29. $id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 
  30. $this->pos_order = get_post_meta( $id, '_pos', true ); 
  31. include 'views/order-details.php'; 
  32.  
  33. /** 
  34. * Save the order type 
  35. * @param $post_id 
  36. * @param $post 
  37. */ 
  38. public function save( $post_id, $post ) { 
  39. if( isset($_POST['wc_pos_order_type']) ) { 
  40. update_post_meta($post_id, '_pos', $_POST['wc_pos_order_type']); 
  41.  
  42. /** 
  43. * Show POS enabled gateways 
  44. * note: only available in WC > 2.3 
  45. * @param $gateways 
  46. * @return array 
  47. */ 
  48. public function payment_gateways($gateways) { 
  49. // get checkout settings data 
  50. $settings = WC_POS_Admin_Settings_Checkout::get_instance(); 
  51. $enabled_ids = $settings->get_enabled_gateway_ids(); 
  52.  
  53. $loaded_gateways = array(); 
  54.  
  55. foreach($gateways as $Gateway) { 
  56. $gateway = new $Gateway(); 
  57. if( in_array( $gateway->id, $enabled_ids ) ) { 
  58. $gateway->enabled = 'yes'; 
  59. $loaded_gateways[] = $gateway; 
  60.  
  61. return $loaded_gateways; 
  62.  
  63. /** 
  64. * Order admin filter links 
  65. * @param array $views 
  66. * @return array 
  67. */ 
  68. public function pos_order_filters( $views ) { 
  69. global $wpdb; 
  70.  
  71. $visibility_filters = array( 
  72. 'yes' => __( 'POS', 'woocommerce-pos' ),  
  73. 'no' => __( 'Online', 'woocommerce-pos' ) 
  74. ); 
  75.  
  76. if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) { 
  77. $views['all'] = str_replace( 'class="current"', '', $views['all'] ); 
  78.  
  79. foreach( $visibility_filters as $key => $label ) { 
  80. $sql = "SELECT count(DISTINCT pm.post_id) 
  81. FROM $wpdb->postmeta pm 
  82. JOIN $wpdb->posts p ON (p.ID = pm.post_id) 
  83. WHERE "; 
  84. $sql .= $key == 'no' ? " pm.post_id NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pos' ) " : " pm.meta_key = '_pos' AND pm.meta_value = '1' "; 
  85. // if( version_compare( WC()->version, '2.2.0' ) >= 0 ) { 
  86. $sql .= "AND p.post_type = 'shop_order'"; 
  87. // } else { 
  88. // $sql .= "AND p.post_type = 'shop_order' AND p.post_status = 'publish'"; 
  89. // } 
  90.  
  91. $count = $wpdb->get_var($sql); 
  92. $class = ( isset( $_GET['pos_order'] ) && $_GET['pos_order'] == $key ) ? 'current' : ''; 
  93. $query_string = remove_query_arg(array( 'pos_order' )); 
  94. if( $class == '' ) $query_string = remove_query_arg(array( 'paged' )); 
  95. $query_string = add_query_arg( 'pos_order', urlencode($key), $query_string ); 
  96. $views[$key] = '<a href="'. $query_string . '" class="' . esc_attr( $class ) . '">' . $label . ' <span class="count">(' . number_format_i18n( $count ) . ')</a>'; 
  97.  
  98. return $views; 
  99.  
  100. /** 
  101. * Order admin filter 
  102. * @param $query 
  103. */ 
  104. function pre_get_posts( $query ) { 
  105.  
  106. if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) { 
  107. if( $_GET['pos_order'] == 'yes' ) { 
  108. $meta_query = array( 
  109. array( 
  110. 'key' => '_pos',  
  111. 'value' => '1',  
  112. 'compare' => '==' 
  113. ); 
  114. else { 
  115. $meta_query = array( 
  116. 'relation' => 'OR',  
  117. array( 
  118. 'key' => '_pos',  
  119. 'value' => '0',  
  120. 'compare' => '==' 
  121. ),  
  122. array( 
  123. 'key' => '_pos',  
  124. 'compare' => 'NOT EXISTS' 
  125. ); 
  126.  
  127. $query->set( 'meta_query', $meta_query ); 
  128.  
  129.  
  130. /** 
  131. * Add type-wc_pos_order class to pos order rows 
  132. * @param $classes 
  133. * @param $class 
  134. * @param $id 
  135. * @return array 
  136. */ 
  137. public function post_class($classes, $class, $id) { 
  138. if( get_post_meta( $id, '_pos', true ) ) { 
  139. $classes[] = 'type-wc_pos_order'; 
  140. return $classes; 
  141.  
  142. /** 
  143. * CSS 
  144. */ 
  145. public function enqueue_admin_styles() { 
  146. $css = ' 
  147. .widefat .type-wc_pos_order .column-order_status { 
  148. background: url( '. WC_POS_PLUGIN_URL .'assets/logo.svg ) no-repeat 75% 9px; 
  149. background-size: 18px; 
  150. fill: #94d31b; 
  151. .widefat .type-wc_pos_order .column-order_status mark { 
  152. margin: 0 auto 0 0; 
  153. '; 
  154. wp_add_inline_style( 'wp-admin', $css ); 
  155.