WC_GZD_REST_Products_Controller

Class WC_GZD_REST_Products_Controller.

Defined (1)

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

/includes/api/class-wc-gzd-rest-products-controller.php  
  1. class WC_GZD_REST_Products_Controller { 
  2.  
  3. public function __construct() { 
  4.  
  5. // v3 
  6. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  7. add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'prepare' ), 10, 3 ); 
  8. add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'prepare' ), 10, 3 ); 
  9.  
  10. add_filter( 'woocommerce_rest_pre_insert_product_object', array( $this, 'insert_update_v3' ), 10, 3 ); 
  11. add_filter( 'woocommerce_rest_pre_insert_product_variation_object', array( $this, 'insert_update_v3' ), 10, 3 ); 
  12. } else { 
  13. add_filter( 'woocommerce_rest_prepare_product', array( $this, 'prepare' ), 10, 3 ); 
  14. add_action( 'woocommerce_rest_insert_product', array( $this, 'insert_update' ), 10, 3 ); 
  15. add_action( 'woocommerce_rest_save_product_variation', array( $this, 'save_variation' ), 10, 3 ); 
  16.  
  17. add_filter( 'woocommerce_rest_product_schema', array( $this, 'schema' ) ); 
  18.  
  19. /** 
  20. * Extend schema. 
  21. * @wp-hook woocommerce_rest_customer_schema 
  22. * @param array $schema_properties Data used to create the customer. 
  23. * @return array 
  24. */ 
  25. public function schema( $schema_properties ) { 
  26.  
  27. $schema_properties['delivery_time'] = array( 
  28. 'description' => __( 'Delivery Time', 'woocommerce-germanized' ),  
  29. 'type' => 'object',  
  30. 'context' => array( 'view', 'edit' ),  
  31. 'properties' => array( 
  32. 'id' => array( 
  33. 'description' => __( 'Delivery Time ID', 'woocommerce-germanized' ),  
  34. 'type' => 'integer',  
  35. 'context' => array( 'view', 'edit' ),  
  36. ),  
  37. 'name' => array( 
  38. 'description' => __( 'Delivery Time Name', 'woocommerce-germanized' ),  
  39. 'type' => 'string',  
  40. 'context' => array( 'view', 'edit' ),  
  41. 'readonly' => true,  
  42. ),  
  43. 'slug' => array( 
  44. 'description' => __( 'Delivery Time Slug', 'woocommerce-germanized' ),  
  45. 'type' => 'string',  
  46. 'context' => array( 'view', 'edit' ) 
  47. ),  
  48. 'html' => array( 
  49. 'description' => __( 'Delivery Time HTML', 'woocommerce-germanized' ),  
  50. 'type' => 'string',  
  51. 'context' => array( 'view', 'edit' ),  
  52. 'readonly' => true,  
  53. ),  
  54. ); 
  55. $schema_properties['sale_price_label'] = array( 
  56. 'description' => __( 'Price Label', 'woocommerce-germanized' ),  
  57. 'type' => 'object',  
  58. 'context' => array( 'view', 'edit' ),  
  59. 'properties' => array( 
  60. 'id' => array( 
  61. 'description' => __( 'Price Label ID', 'woocommerce-germanized' ),  
  62. 'type' => 'integer',  
  63. 'context' => array( 'view', 'edit' ),  
  64. ),  
  65. 'name' => array( 
  66. 'description' => __( 'Price Label Name', 'woocommerce-germanized' ),  
  67. 'type' => 'string',  
  68. 'context' => array( 'view', 'edit' ),  
  69. 'readonly' => true,  
  70. ),  
  71. 'slug' => array( 
  72. 'description' => __( 'Price Label Slug', 'woocommerce-germanized' ),  
  73. 'type' => 'string',  
  74. 'context' => array( 'view', 'edit' ) 
  75. ); 
  76. $schema_properties['sale_price_regular_label'] = array( 
  77. 'description' => __( 'Price Label', 'woocommerce-germanized' ),  
  78. 'type' => 'object',  
  79. 'context' => array( 'view', 'edit' ),  
  80. 'properties' => array( 
  81. 'id' => array( 
  82. 'description' => __( 'Price Label ID', 'woocommerce-germanized' ),  
  83. 'type' => 'integer',  
  84. 'context' => array( 'view', 'edit' ),  
  85. ),  
  86. 'name' => array( 
  87. 'description' => __( 'Price Label Name', 'woocommerce-germanized' ),  
  88. 'type' => 'string',  
  89. 'context' => array( 'view', 'edit' ),  
  90. 'readonly' => true,  
  91. ),  
  92. 'slug' => array( 
  93. 'description' => __( 'Price Label Slug', 'woocommerce-germanized' ),  
  94. 'type' => 'string',  
  95. 'context' => array( 'view', 'edit' ) 
  96. ); 
  97. $schema_properties['unit'] = array( 
  98. 'description' => __( 'Unit', 'woocommerce-germanized' ),  
  99. 'type' => 'object',  
  100. 'context' => array( 'view', 'edit' ),  
  101. 'properties' => array( 
  102. 'id' => array( 
  103. 'description' => __( 'Unit ID', 'woocommerce-germanized' ),  
  104. 'type' => 'integer',  
  105. 'context' => array( 'view', 'edit' ),  
  106. ),  
  107. 'name' => array( 
  108. 'description' => __( 'Unit Name', 'woocommerce-germanized' ),  
  109. 'type' => 'string',  
  110. 'context' => array( 'view', 'edit' ),  
  111. 'readonly' => true,  
  112. ),  
  113. 'slug' => array( 
  114. 'description' => __( 'Unit Slug', 'woocommerce-germanized' ),  
  115. 'type' => 'string',  
  116. 'context' => array( 'view', 'edit' ) 
  117. ); 
  118. $schema_properties['unit_price'] = array( 
  119. 'description' => __( 'Unit Price', 'woocommerce-germanized' ),  
  120. 'type' => 'object',  
  121. 'context' => array( 'view', 'edit' ),  
  122. 'properties' => array( 
  123. 'base' => array( 
  124. 'description' => __( 'Unit Base', 'woocommerce-germanized' ),  
  125. 'type' => 'string',  
  126. 'context' => array( 'view', 'edit' ) 
  127. ),  
  128. 'product' => array( 
  129. 'description' => __( 'Unit Product', 'woocommerce-germanized' ),  
  130. 'type' => 'string',  
  131. 'context' => array( 'view', 'edit' ) 
  132. ),  
  133. 'price_auto' => array( 
  134. 'description' => __( 'Unit Auto Calculation', 'woocommerce-germanized' ),  
  135. 'type' => 'boolean',  
  136. 'default' => false,  
  137. 'context' => array( 'view', 'edit' ) 
  138. ),  
  139. 'price' => array( 
  140. 'description' => __( 'Current Unit Price', 'woocommerce-germanized' ),  
  141. 'type' => 'string',  
  142. 'context' => array( 'view', 'edit' ) 
  143. ),  
  144. 'price_regular' => array( 
  145. 'description' => __( 'Unit Regular Price', 'woocommerce-germanized' ),  
  146. 'type' => 'string',  
  147. 'context' => array( 'view', 'edit' ) 
  148. ),  
  149. 'price_sale' => array( 
  150. 'description' => __( 'Unit Sale Price', 'woocommerce-germanized' ),  
  151. 'type' => 'string',  
  152. 'context' => array( 'view', 'edit' ) 
  153. ),  
  154. 'price_html' => array( 
  155. 'description' => __( 'Unit Price HTML', 'woocommerce-germanized' ),  
  156. 'type' => 'string',  
  157. 'context' => array( 'view', 'edit' ),  
  158. 'readonly' => true,  
  159. ),  
  160. ); 
  161. $schema_properties['mini_desc'] = array( 
  162. 'description' => __( 'Small Cart Product Description', 'woocommerce-germanized' ),  
  163. 'type' => 'string',  
  164. 'context' => array( 'view', 'edit' ),  
  165. ); 
  166. $schema_properties['free_shipping'] = array( 
  167. 'description' => __( 'Deactivate the hint for additional shipping costs', 'woocommerce-germanized' ),  
  168. 'type' => 'boolean',  
  169. 'default' => false,  
  170. 'context' => array( 'view', 'edit' ),  
  171. ); 
  172. $schema_properties['variations']['items']['properties'][ 'delivery_time' ] = array( 
  173. 'description' => __( 'Delivery Time', 'woocommerce-germanized' ),  
  174. 'type' => 'object',  
  175. 'context' => array( 'view', 'edit' ),  
  176. 'properties' => array( 
  177. 'id' => array( 
  178. 'description' => __( 'Delivery Time ID', 'woocommerce-germanized' ),  
  179. 'type' => 'integer',  
  180. 'context' => array( 'view', 'edit' ),  
  181. ),  
  182. 'name' => array( 
  183. 'description' => __( 'Delivery Time Name', 'woocommerce-germanized' ),  
  184. 'type' => 'string',  
  185. 'context' => array( 'view', 'edit' ),  
  186. 'readonly' => true,  
  187. ),  
  188. 'slug' => array( 
  189. 'description' => __( 'Delivery Time Slug', 'woocommerce-germanized' ),  
  190. 'type' => 'string',  
  191. 'context' => array( 'view', 'edit' ) 
  192. ),  
  193. 'html' => array( 
  194. 'description' => __( 'Delivery Time HTML', 'woocommerce-germanized' ),  
  195. 'type' => 'string',  
  196. 'context' => array( 'view', 'edit' ),  
  197. 'readonly' => true,  
  198. ),  
  199. ); 
  200. $schema_properties['variations']['items']['properties'][ 'sale_price_label' ] = array( 
  201. 'description' => __( 'Price Label', 'woocommerce-germanized' ),  
  202. 'type' => 'object',  
  203. 'context' => array( 'view', 'edit' ),  
  204. 'properties' => array( 
  205. 'id' => array( 
  206. 'description' => __( 'Price Label ID', 'woocommerce-germanized' ),  
  207. 'type' => 'integer',  
  208. 'context' => array( 'view', 'edit' ),  
  209. ),  
  210. 'name' => array( 
  211. 'description' => __( 'Price Label Name', 'woocommerce-germanized' ),  
  212. 'type' => 'string',  
  213. 'context' => array( 'view', 'edit' ),  
  214. 'readonly' => true,  
  215. ),  
  216. 'slug' => array( 
  217. 'description' => __( 'Price Label Slug', 'woocommerce-germanized' ),  
  218. 'type' => 'string',  
  219. 'context' => array( 'view', 'edit' ) 
  220. ); 
  221. $schema_properties['variations']['items']['properties'][ 'sale_price_regular_label' ] = array( 
  222. 'description' => __( 'Price Label', 'woocommerce-germanized' ),  
  223. 'type' => 'object',  
  224. 'context' => array( 'view', 'edit' ),  
  225. 'properties' => array( 
  226. 'id' => array( 
  227. 'description' => __( 'Price Label ID', 'woocommerce-germanized' ),  
  228. 'type' => 'integer',  
  229. 'context' => array( 'view', 'edit' ),  
  230. ),  
  231. 'name' => array( 
  232. 'description' => __( 'Price Label Name', 'woocommerce-germanized' ),  
  233. 'type' => 'string',  
  234. 'context' => array( 'view', 'edit' ),  
  235. 'readonly' => true,  
  236. ),  
  237. 'slug' => array( 
  238. 'description' => __( 'Price Label Slug', 'woocommerce-germanized' ),  
  239. 'type' => 'string',  
  240. 'context' => array( 'view', 'edit' ) 
  241. ); 
  242. $schema_properties['variations']['items']['properties'][ 'mini_desc' ] = array( 
  243. 'description' => __( 'Small Cart Product Description', 'woocommerce-germanized' ),  
  244. 'type' => 'string',  
  245. 'context' => array( 'view', 'edit' ),  
  246. ); 
  247. $schema_properties['variations']['items']['properties'][ 'unit_price' ] = array( 
  248. 'description' => __( 'Unit Price', 'woocommerce-germanized' ),  
  249. 'type' => 'object',  
  250. 'context' => array( 'view', 'edit' ),  
  251. 'properties' => array( 
  252. 'base' => array( 
  253. 'description' => __( 'Unit Base', 'woocommerce-germanized' ),  
  254. 'type' => 'string',  
  255. 'context' => array( 'view', 'edit' ) 
  256. ),  
  257. 'product' => array( 
  258. 'description' => __( 'Unit Product', 'woocommerce-germanized' ),  
  259. 'type' => 'string',  
  260. 'context' => array( 'view', 'edit' ) 
  261. ),  
  262. 'price_auto' => array( 
  263. 'description' => __( 'Unit Auto Calculation', 'woocommerce-germanized' ),  
  264. 'type' => 'boolean',  
  265. 'default' => false,  
  266. 'context' => array( 'view', 'edit' ) 
  267. ),  
  268. 'price' => array( 
  269. 'description' => __( 'Current Unit Price', 'woocommerce-germanized' ),  
  270. 'type' => 'string',  
  271. 'context' => array( 'view', 'edit' ) 
  272. ),  
  273. 'price_regular' => array( 
  274. 'description' => __( 'Unit Regular Price', 'woocommerce-germanized' ),  
  275. 'type' => 'string',  
  276. 'context' => array( 'view', 'edit' ) 
  277. ),  
  278. 'price_sale' => array( 
  279. 'description' => __( 'Unit Sale Price', 'woocommerce-germanized' ),  
  280. 'type' => 'string',  
  281. 'context' => array( 'view', 'edit' ) 
  282. ),  
  283. 'price_html' => array( 
  284. 'description' => __( 'Unit Price HTML', 'woocommerce-germanized' ),  
  285. 'type' => 'string',  
  286. 'context' => array( 'view', 'edit' ),  
  287. 'readonly' => true,  
  288. ),  
  289. ); 
  290.  
  291. return $schema_properties; 
  292.  
  293. public function prepare( $response, $post, $request ) { 
  294.  
  295. $product = wc_get_product( $post ); 
  296.  
  297. // Add variations to variable products. 
  298. if ( $product->is_type( 'variable' ) && $product->has_child() ) { 
  299.  
  300. $data = $response->data; 
  301. $data[ 'variations' ] = $this->set_product_variation_fields( $response->data[ 'variations' ], $product ); 
  302. $response->set_data( $data ); 
  303.  
  304.  
  305. $response->set_data( array_merge( $response->data, $this->get_product_data( $product ) ) ); 
  306.  
  307. return apply_filters( 'woocommerce_gzd_rest_prepare_product', $response, $product, $request ); 
  308.  
  309.  
  310. public function insert_update_v3( $product, $request, $inserted ) { 
  311.  
  312. $product = $this->save_update_product_data( $request, $product ); 
  313.  
  314. return $product; 
  315.  
  316. public function insert_update( $post, $request, $inserted ) { 
  317.  
  318. $product = wc_get_product( $post ); 
  319. $product = $this->save_update_product_data( $request, $product ); 
  320.  
  321. return $product; 
  322.  
  323. public function save_variation( $variation_id, $menu_order, $request ) { 
  324.  
  325. $product = wc_get_product( $variation_id ); 
  326. $product = $this->save_update_product_data( $request, $product ); 
  327.  
  328. return $product; 
  329.  
  330. public function get_product_saveable_data( $request, $product ) { 
  331.  
  332. $data_saveable = WC_Germanized_Meta_Box_Product_Data::get_fields(); 
  333. $data = array(); 
  334.  
  335. $data[ 'product-type' ] = $product->get_type(); 
  336.  
  337. if ( isset( $request['delivery_time'] ) && is_array( $request['delivery_time'] ) ) { 
  338. if ( isset( $request['delivery_time']['id'] ) ) { 
  339. $data[ 'delivery_time' ] = intval( $request['delivery_time']['id'] ); 
  340. } elseif ( isset( $request['delivery_time']['slug'] ) ) { 
  341. $data[ 'delivery_time' ] = sanitize_text_field( $request['delivery_time']['id'] ); 
  342.  
  343. // Price Labels + Unit 
  344. $meta_data = array( 
  345. 'sale_price_label' => WC_germanized()->price_labels,  
  346. 'sale_price_regular_label' => WC_germanized()->price_labels,  
  347. 'unit' => WC_germanized()->units,  
  348. ); 
  349.  
  350. foreach ( $meta_data as $meta => $taxonomy_obj ) { 
  351. if ( isset( $request[$meta] ) && is_array( $request[$meta] ) ) { 
  352. $term = null; 
  353. if ( isset( $request[$meta]['id'] ) ) { 
  354. $term = $taxonomy_obj->get_term_object( absint( $request[$meta]['id'] ), 'id' ); 
  355. elseif ( isset( $request[$meta]['slug'] ) ) { 
  356. $term = $taxonomy_obj->get_term_object( sanitize_text_field( $request[$meta]['slug'] ) ); 
  357. if ( $term ) { 
  358. $data[ '_' . $meta ] = $term->slug; 
  359.  
  360. if ( isset( $request['unit_price'] ) && is_array( $request['unit_price'] ) ) { 
  361.  
  362. foreach ( $request['unit_price'] as $key => $val ) { 
  363.  
  364. if ( isset( $data_saveable[ '_unit_' . $key ] ) ) { 
  365. $data[ '_unit_' . $key ] = sanitize_text_field( $val ); 
  366.  
  367. if ( isset( $data[ '_unit_price_auto' ] ) && ! empty( $data[ '_unit_price_auto' ] ) ) { 
  368. $data['_unit_price_auto'] = true; 
  369. } elseif ( empty( $data[ '_unit_price_auto' ] ) ) { 
  370. unset( $data['_unit_price_auto'] ); 
  371. } else { 
  372. $data['_unit_price_auto'] = wc_gzd_get_crud_data( $product, '_unit_price_auto' ); 
  373.  
  374. if ( isset( $data['_unit_price_sale'] ) ) { 
  375. $data['_sale_price'] = wc_gzd_get_crud_data( $product, 'sale_price' ); 
  376.  
  377. if ( isset( $request['free_shipping'] ) ) { 
  378. if ( ! empty( $request['free_shipping'] ) ) 
  379. $data[ '_free_shipping' ] = true; 
  380. } else { 
  381. $data[ '_free_shipping' ] = wc_gzd_get_crud_data( $product, '_free_shipping', true ); 
  382.  
  383. // Do only add free_shipping if is set so saving works (checkbox-style). 
  384. if ( empty( $data[ '_free_shipping' ] ) || ! $data[ '_free_shipping' ] ) 
  385. unset( $data[ '_free_shipping' ] ); 
  386.  
  387. return $data; 
  388.  
  389. public function save_update_product_data( $request, $product ) { 
  390.  
  391. $data = $this->get_product_saveable_data( $request, $product ); 
  392. $data['save'] = false; 
  393.  
  394. $product = WC_Germanized_Meta_Box_Product_Data::save_product_data( $product, $data ); 
  395.  
  396. return $product; 
  397.  
  398. private function set_product_variation_fields( $variations, $product ) { 
  399.  
  400. foreach( $variations as $key => $variation ) { 
  401. $variations[ $key ] = array_merge( $variation, $this->get_product_data( wc_get_product( $variation[ 'id' ] ) ) ); 
  402.  
  403. return $variations; 
  404.  
  405. private function get_product_data( $product ) { 
  406.  
  407. $product = wc_gzd_get_gzd_product( $product ); 
  408.  
  409. $data = array(); 
  410.  
  411. if ( ! $product->is_type( 'variation' ) ) { 
  412. $data[ 'unit' ] = $this->prepare_term( WC_germanized()->units->get_term_object( wc_gzd_get_crud_data( $product, 'unit' ) ) ); 
  413.  
  414. // Unit Price 
  415. $data[ 'unit_price' ] = array( 
  416. 'base' => $product->get_unit_base(),  
  417. 'product' => $product->get_unit_products(),  
  418. 'price_auto' => $product->is_unit_price_calculated_automatically(),  
  419. 'price' => $product->get_unit_price(),  
  420. 'price_regular' => $product->get_unit_regular_price(),  
  421. 'price_sale' => $product->get_unit_sale_price(),  
  422. 'price_html' => $product->get_unit_html(),  
  423. ); 
  424.  
  425. // Cart Mini Description 
  426. $data[ 'mini_desc' ] = $product->get_mini_desc() ? $product->get_mini_desc() : ''; 
  427.  
  428. // Sale Labels 
  429. $data[ 'sale_price_label' ] = $this->prepare_term( WC_germanized()->price_labels->get_term_object( $product->get_sale_price_label() ) ); 
  430. $data[ 'sale_price_regular_label' ] = $this->prepare_term( WC_germanized()->price_labels->get_term_object( $product->get_sale_price_regular_label() ) ); 
  431.  
  432. // Delivery Time 
  433. $data[ 'delivery_time' ] = $this->prepare_term( $product->get_delivery_time_term() ); 
  434.  
  435. if ( ! empty( $data[ 'delivery_time' ] ) ) { 
  436. $data[ 'delivery_time' ][ 'html' ] = $product->get_delivery_time_html(); 
  437.  
  438. // Shipping costs hidden? 
  439. $data[ 'free_shipping' ] = $product->has_free_shipping(); 
  440.  
  441. return $data; 
  442.  
  443. private function prepare_term( $term ) { 
  444.  
  445. if ( ! empty( $term ) && is_object( $term ) && ! is_wp_error( $term ) ) { 
  446. return array( 
  447. 'id' => $term->term_id,  
  448. 'name' => $term->name,  
  449. 'slug' => $term->slug,  
  450. ); 
  451.  
  452. return array(); 
  453.