WC_POS_API_Customers

POS Customer Class duck punches the WC REST API.

Defined (1)

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

/includes/api/class-wc-pos-customers.php  
  1. class WC_POS_API_Customers extends WC_POS_API_Abstract { 
  2.  
  3. /** 
  4. * Constructor 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'pre_get_users', array( $this, 'pre_get_users' ) ); 
  8. add_action( 'pre_user_query', array( $this, 'pre_user_query' ) ); 
  9. add_filter( 'woocommerce_api_customer_response', array( $this, 'customer_response' ), 10, 4 ); 
  10.  
  11. /** 
  12. * Removes the role='customer' restraint 
  13. * todo: add this option to settings 
  14. * @param $wp_user_query 
  15. */ 
  16. public function pre_get_users( $wp_user_query ) { 
  17.  
  18. $wp_user_query->query_vars[ 'role' ] = ''; 
  19.  
  20. if ( isset( $_GET[ 'filter' ] ) ) { 
  21.  
  22. // add support for filter[in] 
  23. if ( isset( $_GET[ 'filter' ][ 'in' ] ) ) { 
  24. $wp_user_query->query_vars[ 'include' ] = explode( ', ', $_GET[ 'filter' ][ 'in' ] ); 
  25.  
  26. // add support for filter[not_in] 
  27. if ( isset( $_GET[ 'filter' ][ 'not_in' ] ) ) { 
  28. $wp_user_query->query_vars[ 'exclude' ] = explode( ', ', $_GET[ 'filter' ][ 'not_in' ] ); 
  29.  
  30.  
  31.  
  32. /** 
  33. * @param $wp_user_query 
  34. */ 
  35. public function pre_user_query( $wp_user_query ) { 
  36.  
  37. // customer search 
  38. $term = $wp_user_query->query_vars['search']; 
  39. if (!empty($term)) { 
  40. $this->customer_search($term, $wp_user_query); 
  41.  
  42. /** 
  43. * Extends customer search 
  44. * @param $term 
  45. * @param $wp_user_query 
  46. */ 
  47. private function customer_search($term, $wp_user_query) { 
  48. global $wpdb; 
  49.  
  50. // search usermeta table 
  51. $usermeta_ids = $wpdb->get_col(" 
  52. SELECT DISTINCT user_id 
  53. FROM $wpdb->usermeta 
  54. WHERE (meta_key='first_name' OR meta_key='last_name') 
  55. AND LOWER(meta_value) 
  56. LIKE '%".$term."%' 
  57. "); 
  58.  
  59. // search users table 
  60. $users_ids = $wpdb->get_col(" 
  61. SELECT DISTINCT ID 
  62. FROM $wpdb->users 
  63. WHERE LOWER(user_nicename) 
  64. LIKE '%".$term."%' 
  65. OR LOWER(user_login) 
  66. LIKE '%".$term."%' 
  67. "); 
  68.  
  69. $ids = array_unique(array_merge($usermeta_ids, $users_ids)); 
  70. $ids_str = implode(', ', $ids); 
  71.  
  72. if (!empty($ids_str)) { 
  73. $wp_user_query->query_where = str_replace( 
  74. "user_nicename LIKE '".$term."'",  
  75. "ID IN(".$ids_str.")",  
  76. $wp_user_query->query_where 
  77. ); 
  78.  
  79. /** 
  80. * Returns array of all user ids 
  81. * @param $updated_at_min 
  82. * @return array 
  83. */ 
  84. public function get_ids($updated_at_min) { 
  85. $args = array( 
  86. 'fields' => 'ID' 
  87. ); 
  88.  
  89. if($updated_at_min) { 
  90. $args['meta_key'] = '_user_modified_gmt'; 
  91. $args['meta_value'] = $this->parse_datetime( $updated_at_min ); 
  92. $args['meta_compare'] = '>'; 
  93.  
  94. $query = new WP_User_Query( $args ); 
  95. return array_map( 'intval', $query->results ); 
  96.  
  97.  
  98. /** 
  99. * - add `updated_at` to customer data 
  100. * @param $customer_data 
  101. * @param $customer 
  102. * @param $fields 
  103. * @param $server 
  104. * @return mixed 
  105. */ 
  106. public function customer_response($customer_data, $customer, $fields, $server) { 
  107. $timestamp = get_user_meta($customer->ID, '_user_modified_gmt', true); 
  108. $customer_data['updated_at'] = $server->format_datetime( $timestamp ); 
  109. return $customer_data; 
  110.  
  111.