WC_POS_APIv2_Customers

POS Customer Class duck punches the WC REST API.

Defined (1)

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

/includes/apiv2/class-wc-pos-customers.php  
  1. class WC_POS_APIv2_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_rest_prepare_customer', array( $this, 'customer_response' ), 10, 3 ); 
  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 $date_modified 
  82. * @return array 
  83. */ 
  84. public function get_ids($date_modified) { 
  85. $args = array( 
  86. 'fields' => 'ID' 
  87. ); 
  88.  
  89. if($date_modified) { 
  90. $args['meta_key'] = '_user_modified_gmt'; 
  91. $args['meta_value'] = $this->parse_datetime( $date_modified ); 
  92. $args['meta_compare'] = '>'; 
  93.  
  94. $query = new WP_User_Query( $args ); 
  95. return array_map( 'intval', $query->results ); 
  96.  
  97.  
  98. /** 
  99. * @param WP_REST_Response $response The response object. 
  100. * @param WP_User $user_data User object used to create response. 
  101. * @param WP_REST_Request $request Request object. 
  102. * @return array 
  103. */ 
  104. public function customer_response( $response, $user_data, $request ) { 
  105. $data = $response->get_data(); 
  106.  
  107. // backwards compat 
  108. if( isset($data['date_modified']) ) { 
  109. $data['updated_at'] = $data['date_modified']; 
  110.  
  111. if( isset($data['billing']) ) { 
  112. $data['billing_address'] = $data['billing']; 
  113.  
  114. if( isset($data['shipping']) ) { 
  115. $data['shipping_address'] = $data['shipping']; 
  116.  
  117. $response->set_data($data); 
  118. return $response; 
  119.  
  120.