WC_REST_Customers_Controller

REST API Customers controller class.

Defined (1)

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

/includes/api/class-wc-rest-customers-controller.php  
  1. class WC_REST_Customers_Controller extends WC_REST_Customers_V1_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Get formatted item data. 
  11. * @since 3.0.0 
  12. * @param WC_Data $object WC_Data instance. 
  13. * @return array 
  14. */ 
  15. protected function get_formatted_item_data( $object ) { 
  16. $data = $object->get_data(); 
  17. $format_date = array( 'date_created', 'date_modified' ); 
  18.  
  19. // Format date values. 
  20. foreach ( $format_date as $key ) { 
  21. $datetime = $data[ $key ]; 
  22. $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); 
  23. $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); 
  24.  
  25. return array( 
  26. 'id' => $object->get_id(),  
  27. 'date_created' => $data['date_created'],  
  28. 'date_created_gmt' => $data['date_created_gmt'],  
  29. 'date_modified' => $data['date_modified'],  
  30. 'date_modified_gmt' => $data['date_modified_gmt'],  
  31. 'email' => $data['email'],  
  32. 'first_name' => $data['first_name'],  
  33. 'last_name' => $data['last_name'],  
  34. 'role' => $data['role'],  
  35. 'username' => $data['username'],  
  36. 'billing' => $data['billing'],  
  37. 'shipping' => $data['shipping'],  
  38. 'is_paying_customer' => $data['is_paying_customer'],  
  39. 'orders_count' => $object->get_order_count(),  
  40. 'total_spent' => $object->get_total_spent(),  
  41. 'avatar_url' => $object->get_avatar_url(),  
  42. 'meta_data' => $data['meta_data'],  
  43. ); 
  44.  
  45. /** 
  46. * Prepare a single customer output for response. 
  47. * @param WP_User $user_data User object. 
  48. * @param WP_REST_Request $request Request object. 
  49. * @return WP_REST_Response $response Response data. 
  50. */ 
  51. public function prepare_item_for_response( $user_data, $request ) { 
  52. $customer = new WC_Customer( $user_data->ID ); 
  53. $data = $this->get_formatted_item_data( $customer ); 
  54. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  55. $data = $this->add_additional_fields_to_object( $data, $request ); 
  56. $data = $this->filter_response_by_context( $data, $context ); 
  57. $response = rest_ensure_response( $data ); 
  58. $response->add_links( $this->prepare_links( $user_data ) ); 
  59.  
  60. /** 
  61. * Filter customer data returned from the REST API. 
  62. * @param WP_REST_Response $response The response object. 
  63. * @param WP_User $user_data User object used to create response. 
  64. * @param WP_REST_Request $request Request object. 
  65. */ 
  66. return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request ); 
  67.  
  68. /** 
  69. * Update customer meta fields. 
  70. * @param WC_Customer $customer 
  71. * @param WP_REST_Request $request 
  72. */ 
  73. protected function update_customer_meta_fields( $customer, $request ) { 
  74. parent::update_customer_meta_fields( $customer, $request ); 
  75.  
  76. // Meta data. 
  77. if ( isset( $request['meta_data'] ) ) { 
  78. if ( is_array( $request['meta_data'] ) ) { 
  79. foreach ( $request['meta_data'] as $meta ) { 
  80. $customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); 
  81.  
  82. /** 
  83. * Get the Customer's schema, conforming to JSON Schema. 
  84. * @return array 
  85. */ 
  86. public function get_item_schema() { 
  87. $schema = array( 
  88. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  89. 'title' => 'customer',  
  90. 'type' => 'object',  
  91. 'properties' => array( 
  92. 'id' => array( 
  93. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  94. 'type' => 'integer',  
  95. 'context' => array( 'view', 'edit' ),  
  96. 'readonly' => true,  
  97. ),  
  98. 'date_created' => array( 
  99. 'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ),  
  100. 'type' => 'date-time',  
  101. 'context' => array( 'view', 'edit' ),  
  102. 'readonly' => true,  
  103. ),  
  104. 'date_created_gmt' => array( 
  105. 'description' => __( 'The date the order was created, as GMT.', 'woocommerce' ),  
  106. 'type' => 'date-time',  
  107. 'context' => array( 'view', 'edit' ),  
  108. 'readonly' => true,  
  109. ),  
  110. 'date_modified' => array( 
  111. 'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ),  
  112. 'type' => 'date-time',  
  113. 'context' => array( 'view', 'edit' ),  
  114. 'readonly' => true,  
  115. ),  
  116. 'date_modified_gmt' => array( 
  117. 'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ),  
  118. 'type' => 'date-time',  
  119. 'context' => array( 'view', 'edit' ),  
  120. 'readonly' => true,  
  121. ),  
  122. 'email' => array( 
  123. 'description' => __( 'The email address for the customer.', 'woocommerce' ),  
  124. 'type' => 'string',  
  125. 'format' => 'email',  
  126. 'context' => array( 'view', 'edit' ),  
  127. ),  
  128. 'first_name' => array( 
  129. 'description' => __( 'Customer first name.', 'woocommerce' ),  
  130. 'type' => 'string',  
  131. 'context' => array( 'view', 'edit' ),  
  132. 'arg_options' => array( 
  133. 'sanitize_callback' => 'sanitize_text_field',  
  134. ),  
  135. ),  
  136. 'last_name' => array( 
  137. 'description' => __( 'Customer last name.', 'woocommerce' ),  
  138. 'type' => 'string',  
  139. 'context' => array( 'view', 'edit' ),  
  140. 'arg_options' => array( 
  141. 'sanitize_callback' => 'sanitize_text_field',  
  142. ),  
  143. ),  
  144. 'role' => array( 
  145. 'description' => __( 'Customer role.', 'woocommerce' ),  
  146. 'type' => 'string',  
  147. 'context' => array( 'view', 'edit' ),  
  148. 'readonly' => true,  
  149. ),  
  150. 'username' => array( 
  151. 'description' => __( 'Customer login name.', 'woocommerce' ),  
  152. 'type' => 'string',  
  153. 'context' => array( 'view', 'edit' ),  
  154. 'arg_options' => array( 
  155. 'sanitize_callback' => 'sanitize_user',  
  156. ),  
  157. ),  
  158. 'password' => array( 
  159. 'description' => __( 'Customer password.', 'woocommerce' ),  
  160. 'type' => 'string',  
  161. 'context' => array( 'edit' ),  
  162. ),  
  163. 'billing' => array( 
  164. 'description' => __( 'List of billing address data.', 'woocommerce' ),  
  165. 'type' => 'object',  
  166. 'context' => array( 'view', 'edit' ),  
  167. 'properties' => array( 
  168. 'first_name' => array( 
  169. 'description' => __( 'First name.', 'woocommerce' ),  
  170. 'type' => 'string',  
  171. 'context' => array( 'view', 'edit' ),  
  172. ),  
  173. 'last_name' => array( 
  174. 'description' => __( 'Last name.', 'woocommerce' ),  
  175. 'type' => 'string',  
  176. 'context' => array( 'view', 'edit' ),  
  177. ),  
  178. 'company' => array( 
  179. 'description' => __( 'Company name.', 'woocommerce' ),  
  180. 'type' => 'string',  
  181. 'context' => array( 'view', 'edit' ),  
  182. ),  
  183. 'address_1' => array( 
  184. 'description' => __( 'Address line 1', 'woocommerce' ),  
  185. 'type' => 'string',  
  186. 'context' => array( 'view', 'edit' ),  
  187. ),  
  188. 'address_2' => array( 
  189. 'description' => __( 'Address line 2', 'woocommerce' ),  
  190. 'type' => 'string',  
  191. 'context' => array( 'view', 'edit' ),  
  192. ),  
  193. 'city' => array( 
  194. 'description' => __( 'City name.', 'woocommerce' ),  
  195. 'type' => 'string',  
  196. 'context' => array( 'view', 'edit' ),  
  197. ),  
  198. 'state' => array( 
  199. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  200. 'type' => 'string',  
  201. 'context' => array( 'view', 'edit' ),  
  202. ),  
  203. 'postcode' => array( 
  204. 'description' => __( 'Postal code.', 'woocommerce' ),  
  205. 'type' => 'string',  
  206. 'context' => array( 'view', 'edit' ),  
  207. ),  
  208. 'country' => array( 
  209. 'description' => __( 'ISO code of the country.', 'woocommerce' ),  
  210. 'type' => 'string',  
  211. 'context' => array( 'view', 'edit' ),  
  212. ),  
  213. 'email' => array( 
  214. 'description' => __( 'Email address.', 'woocommerce' ),  
  215. 'type' => 'string',  
  216. 'format' => 'email',  
  217. 'context' => array( 'view', 'edit' ),  
  218. ),  
  219. 'phone' => array( 
  220. 'description' => __( 'Phone number.', 'woocommerce' ),  
  221. 'type' => 'string',  
  222. 'context' => array( 'view', 'edit' ),  
  223. ),  
  224. ),  
  225. ),  
  226. 'shipping' => array( 
  227. 'description' => __( 'List of shipping address data.', 'woocommerce' ),  
  228. 'type' => 'object',  
  229. 'context' => array( 'view', 'edit' ),  
  230. 'properties' => array( 
  231. 'first_name' => array( 
  232. 'description' => __( 'First name.', 'woocommerce' ),  
  233. 'type' => 'string',  
  234. 'context' => array( 'view', 'edit' ),  
  235. ),  
  236. 'last_name' => array( 
  237. 'description' => __( 'Last name.', 'woocommerce' ),  
  238. 'type' => 'string',  
  239. 'context' => array( 'view', 'edit' ),  
  240. ),  
  241. 'company' => array( 
  242. 'description' => __( 'Company name.', 'woocommerce' ),  
  243. 'type' => 'string',  
  244. 'context' => array( 'view', 'edit' ),  
  245. ),  
  246. 'address_1' => array( 
  247. 'description' => __( 'Address line 1', 'woocommerce' ),  
  248. 'type' => 'string',  
  249. 'context' => array( 'view', 'edit' ),  
  250. ),  
  251. 'address_2' => array( 
  252. 'description' => __( 'Address line 2', 'woocommerce' ),  
  253. 'type' => 'string',  
  254. 'context' => array( 'view', 'edit' ),  
  255. ),  
  256. 'city' => array( 
  257. 'description' => __( 'City name.', 'woocommerce' ),  
  258. 'type' => 'string',  
  259. 'context' => array( 'view', 'edit' ),  
  260. ),  
  261. 'state' => array( 
  262. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  263. 'type' => 'string',  
  264. 'context' => array( 'view', 'edit' ),  
  265. ),  
  266. 'postcode' => array( 
  267. 'description' => __( 'Postal code.', 'woocommerce' ),  
  268. 'type' => 'string',  
  269. 'context' => array( 'view', 'edit' ),  
  270. ),  
  271. 'country' => array( 
  272. 'description' => __( 'ISO code of the country.', 'woocommerce' ),  
  273. 'type' => 'string',  
  274. 'context' => array( 'view', 'edit' ),  
  275. ),  
  276. ),  
  277. ),  
  278. 'is_paying_customer' => array( 
  279. 'description' => __( 'Is the customer a paying customer?', 'woocommerce' ),  
  280. 'type' => 'bool',  
  281. 'context' => array( 'view', 'edit' ),  
  282. 'readonly' => true,  
  283. ),  
  284. 'orders_count' => array( 
  285. 'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ),  
  286. 'type' => 'integer',  
  287. 'context' => array( 'view', 'edit' ),  
  288. 'readonly' => true,  
  289. ),  
  290. 'total_spent' => array( 
  291. 'description' => __( 'Total amount spent.', 'woocommerce' ),  
  292. 'type' => 'string',  
  293. 'context' => array( 'view', 'edit' ),  
  294. 'readonly' => true,  
  295. ),  
  296. 'avatar_url' => array( 
  297. 'description' => __( 'Avatar URL.', 'woocommerce' ),  
  298. 'type' => 'string',  
  299. 'context' => array( 'view', 'edit' ),  
  300. 'readonly' => true,  
  301. ),  
  302. 'meta_data' => array( 
  303. 'description' => __( 'Meta data.', 'woocommerce' ),  
  304. 'type' => 'array',  
  305. 'context' => array( 'view', 'edit' ),  
  306. 'items' => array( 
  307. 'type' => 'object',  
  308. 'properties' => array( 
  309. 'id' => array( 
  310. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  311. 'type' => 'integer',  
  312. 'context' => array( 'view', 'edit' ),  
  313. 'readonly' => true,  
  314. ),  
  315. 'key' => array( 
  316. 'description' => __( 'Meta key.', 'woocommerce' ),  
  317. 'type' => 'string',  
  318. 'context' => array( 'view', 'edit' ),  
  319. ),  
  320. 'value' => array( 
  321. 'description' => __( 'Meta value.', 'woocommerce' ),  
  322. 'type' => 'string',  
  323. 'context' => array( 'view', 'edit' ),  
  324. ),  
  325. ),  
  326. ),  
  327. ),  
  328. ),  
  329. ); 
  330.  
  331. return $this->add_additional_fields_schema( $schema );