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