WC_REST_Legacy_Products_Controller

REST API Legacy Products controller class.

Defined (1)

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

/includes/api/legacy/class-wc-rest-legacy-products-controller.php  
  1. class WC_REST_Legacy_Products_Controller extends WC_REST_CRUD_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Query args. 
  11. * @deprecated 3.0.0 
  12. * @param array $args Request args. 
  13. * @param WP_REST_Request $request Request data. 
  14. * @return array 
  15. */ 
  16. public function query_args( $args, $request ) { 
  17. // Set post_status. 
  18. $args['post_status'] = $request['status']; 
  19.  
  20. // Taxonomy query to filter products by type, category,  
  21. // tag, shipping class, and attribute. 
  22. $tax_query = array(); 
  23.  
  24. // Map between taxonomy name and arg's key. 
  25. $taxonomies = array( 
  26. 'product_cat' => 'category',  
  27. 'product_tag' => 'tag',  
  28. 'product_shipping_class' => 'shipping_class',  
  29. ); 
  30.  
  31. // Set tax_query for each passed arg. 
  32. foreach ( $taxonomies as $taxonomy => $key ) { 
  33. if ( ! empty( $request[ $key ] ) ) { 
  34. $tax_query[] = array( 
  35. 'taxonomy' => $taxonomy,  
  36. 'field' => 'term_id',  
  37. 'terms' => $request[ $key ],  
  38. ); 
  39.  
  40. // Filter product type by slug. 
  41. if ( ! empty( $request['type'] ) ) { 
  42. $tax_query[] = array( 
  43. 'taxonomy' => 'product_type',  
  44. 'field' => 'slug',  
  45. 'terms' => $request['type'],  
  46. ); 
  47.  
  48. // Filter by attribute and term. 
  49. if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) { 
  50. if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) { 
  51. $tax_query[] = array( 
  52. 'taxonomy' => $request['attribute'],  
  53. 'field' => 'term_id',  
  54. 'terms' => $request['attribute_term'],  
  55. ); 
  56.  
  57. if ( ! empty( $tax_query ) ) { 
  58. $args['tax_query'] = $tax_query; 
  59.  
  60. // Filter featured. 
  61. if ( is_bool( $request['featured'] ) ) { 
  62. $args['tax_query'][] = array( 
  63. 'taxonomy' => 'product_visibility',  
  64. 'field' => 'name',  
  65. 'terms' => 'featured',  
  66. ); 
  67.  
  68. // Filter by sku. 
  69. if ( ! empty( $request['sku'] ) ) { 
  70. $skus = explode( ', ', $request['sku'] ); 
  71. // Include the current string as a SKU too. 
  72. if ( 1 < count( $skus ) ) { 
  73. $skus[] = $request['sku']; 
  74.  
  75. $args['meta_query'] = $this->add_meta_query( $args, array( 
  76. 'key' => '_sku',  
  77. 'value' => $skus,  
  78. 'compare' => 'IN',  
  79. ) ); 
  80.  
  81. // Filter by tax class. 
  82. if ( ! empty( $request['tax_class'] ) ) { 
  83. $args['meta_query'] = $this->add_meta_query( $args, array( 
  84. 'key' => '_tax_class',  
  85. 'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',  
  86. ) ); 
  87.  
  88. // Price filter. 
  89. if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) { 
  90. $args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); 
  91.  
  92. // Filter product in stock or out of stock. 
  93. if ( is_bool( $request['in_stock'] ) ) { 
  94. $args['meta_query'] = $this->add_meta_query( $args, array( 
  95. 'key' => '_stock_status',  
  96. 'value' => true === $request['in_stock'] ? 'instock' : 'outofstock',  
  97. ) ); 
  98.  
  99. // Filter by on sale products. 
  100. if ( is_bool( $request['on_sale'] ) ) { 
  101. $on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in'; 
  102. $args[ $on_sale_key ] += wc_get_product_ids_on_sale(); 
  103.  
  104. // Force the post_type argument, since it's not a user input variable. 
  105. if ( ! empty( $request['sku'] ) ) { 
  106. $args['post_type'] = array( 'product', 'product_variation' ); 
  107. } else { 
  108. $args['post_type'] = $this->post_type; 
  109.  
  110. return $args; 
  111.  
  112. /** 
  113. * Prepare a single product output for response. 
  114. * @deprecated 3.0.0 
  115. * @param WP_Post $post Post object. 
  116. * @param WP_REST_Request $request Request object. 
  117. * @return WP_REST_Response 
  118. */ 
  119. public function prepare_item_for_response( $post, $request ) { 
  120. $product = wc_get_product( $post ); 
  121. $data = $this->get_product_data( $product ); 
  122.  
  123. // Add variations to variable products. 
  124. if ( $product->is_type( 'variable' ) && $product->has_child() ) { 
  125. $data['variations'] = $product->get_children(); 
  126.  
  127. // Add grouped products data. 
  128. if ( $product->is_type( 'grouped' ) && $product->has_child() ) { 
  129. $data['grouped_products'] = $product->get_children(); 
  130.  
  131. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  132. $data = $this->add_additional_fields_to_object( $data, $request ); 
  133. $data = $this->filter_response_by_context( $data, $context ); 
  134.  
  135. // Wrap the data in a response object. 
  136. $response = rest_ensure_response( $data ); 
  137.  
  138. $response->add_links( $this->prepare_links( $product, $request ) ); 
  139.  
  140. /** 
  141. * Filter the data for a response. 
  142. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being 
  143. * prepared for the response. 
  144. * @param WP_REST_Response $response The response object. 
  145. * @param WP_Post $post Post object. 
  146. * @param WP_REST_Request $request Request object. 
  147. */ 
  148. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request ); 
  149.  
  150. /** 
  151. * Get product menu order. 
  152. * @deprecated 3.0.0 
  153. * @param WC_Product $product Product instance. 
  154. * @return int 
  155. */ 
  156. protected function get_product_menu_order( $product ) { 
  157. return $product->get_menu_order(); 
  158.  
  159. /** 
  160. * Save product meta. 
  161. * @deprecated 3.0.0 
  162. * @param WC_Product $product 
  163. * @param WP_REST_Request $request 
  164. * @return bool 
  165. * @throws WC_REST_Exception 
  166. */ 
  167. protected function save_product_meta( $product, $request ) { 
  168. $product = $this->set_product_meta( $product, $request ); 
  169. $product->save(); 
  170.  
  171. return true; 
  172.  
  173. /** 
  174. * Set product meta. 
  175. * @deprecated 3.0.0 
  176. * @throws WC_REST_Exception REST API exceptions. 
  177. * @param WC_Product $product Product instance. 
  178. * @param WP_REST_Request $request Request data. 
  179. * @return WC_Product 
  180. */ 
  181. protected function set_product_meta( $product, $request ) { 
  182. // Virtual. 
  183. if ( isset( $request['virtual'] ) ) { 
  184. $product->set_virtual( $request['virtual'] ); 
  185.  
  186. // Tax status. 
  187. if ( isset( $request['tax_status'] ) ) { 
  188. $product->set_tax_status( $request['tax_status'] ); 
  189.  
  190. // Tax Class. 
  191. if ( isset( $request['tax_class'] ) ) { 
  192. $product->set_tax_class( $request['tax_class'] ); 
  193.  
  194. // Catalog Visibility. 
  195. if ( isset( $request['catalog_visibility'] ) ) { 
  196. $product->set_catalog_visibility( $request['catalog_visibility'] ); 
  197.  
  198. // Purchase Note. 
  199. if ( isset( $request['purchase_note'] ) ) { 
  200. $product->set_purchase_note( wc_clean( $request['purchase_note'] ) ); 
  201.  
  202. // Featured Product. 
  203. if ( isset( $request['featured'] ) ) { 
  204. $product->set_featured( $request['featured'] ); 
  205.  
  206. // Shipping data. 
  207. $product = $this->save_product_shipping_data( $product, $request ); 
  208.  
  209. // SKU. 
  210. if ( isset( $request['sku'] ) ) { 
  211. $product->set_sku( wc_clean( $request['sku'] ) ); 
  212.  
  213. // Attributes. 
  214. if ( isset( $request['attributes'] ) ) { 
  215. $attributes = array(); 
  216.  
  217. foreach ( $request['attributes'] as $attribute ) { 
  218. $attribute_id = 0; 
  219. $attribute_name = ''; 
  220.  
  221. // Check ID for global attributes or name for product attributes. 
  222. if ( ! empty( $attribute['id'] ) ) { 
  223. $attribute_id = absint( $attribute['id'] ); 
  224. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  225. } elseif ( ! empty( $attribute['name'] ) ) { 
  226. $attribute_name = wc_clean( $attribute['name'] ); 
  227.  
  228. if ( ! $attribute_id && ! $attribute_name ) { 
  229. continue; 
  230.  
  231. if ( $attribute_id ) { 
  232.  
  233. if ( isset( $attribute['options'] ) ) { 
  234. $options = $attribute['options']; 
  235.  
  236. if ( ! is_array( $attribute['options'] ) ) { 
  237. // Text based attributes - Posted values are term names. 
  238. $options = explode( WC_DELIMITER, $options ); 
  239.  
  240. $values = array_map( 'wc_sanitize_term_text_based', $options ); 
  241. $values = array_filter( $values, 'strlen' ); 
  242. } else { 
  243. $values = array(); 
  244.  
  245. if ( ! empty( $values ) ) { 
  246. // Add attribute to array, but don't set values. 
  247. $attribute_object = new WC_Product_Attribute(); 
  248. $attribute_object->set_id( $attribute_id ); 
  249. $attribute_object->set_name( $attribute_name ); 
  250. $attribute_object->set_options( $values ); 
  251. $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); 
  252. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); 
  253. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); 
  254. $attributes[] = $attribute_object; 
  255. } elseif ( isset( $attribute['options'] ) ) { 
  256. // Custom attribute - Add attribute to array and set the values. 
  257. if ( is_array( $attribute['options'] ) ) { 
  258. $values = $attribute['options']; 
  259. } else { 
  260. $values = explode( WC_DELIMITER, $attribute['options'] ); 
  261. $attribute_object = new WC_Product_Attribute(); 
  262. $attribute_object->set_name( $attribute_name ); 
  263. $attribute_object->set_options( $values ); 
  264. $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); 
  265. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); 
  266. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); 
  267. $attributes[] = $attribute_object; 
  268. $product->set_attributes( $attributes ); 
  269.  
  270. // Sales and prices. 
  271. if ( in_array( $product->get_type(), array( 'variable', 'grouped' ), true ) ) { 
  272. $product->set_regular_price( '' ); 
  273. $product->set_sale_price( '' ); 
  274. $product->set_date_on_sale_to( '' ); 
  275. $product->set_date_on_sale_from( '' ); 
  276. $product->set_price( '' ); 
  277. } else { 
  278. // Regular Price. 
  279. if ( isset( $request['regular_price'] ) ) { 
  280. $product->set_regular_price( $request['regular_price'] ); 
  281.  
  282. // Sale Price. 
  283. if ( isset( $request['sale_price'] ) ) { 
  284. $product->set_sale_price( $request['sale_price'] ); 
  285.  
  286. if ( isset( $request['date_on_sale_from'] ) ) { 
  287. $product->set_date_on_sale_from( $request['date_on_sale_from'] ); 
  288.  
  289. if ( isset( $request['date_on_sale_to'] ) ) { 
  290. $product->set_date_on_sale_to( $request['date_on_sale_to'] ); 
  291.  
  292. // Product parent ID for groups. 
  293. if ( isset( $request['parent_id'] ) ) { 
  294. $product->set_parent_id( $request['parent_id'] ); 
  295.  
  296. // Sold individually. 
  297. if ( isset( $request['sold_individually'] ) ) { 
  298. $product->set_sold_individually( $request['sold_individually'] ); 
  299.  
  300. // Stock status. 
  301. if ( isset( $request['in_stock'] ) ) { 
  302. $stock_status = true === $request['in_stock'] ? 'instock' : 'outofstock'; 
  303. } else { 
  304. $stock_status = $product->get_stock_status(); 
  305.  
  306. // Stock data. 
  307. if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { 
  308. // Manage stock. 
  309. if ( isset( $request['manage_stock'] ) ) { 
  310. $product->set_manage_stock( $request['manage_stock'] ); 
  311.  
  312. // Backorders. 
  313. if ( isset( $request['backorders'] ) ) { 
  314. $product->set_backorders( $request['backorders'] ); 
  315.  
  316. if ( $product->is_type( 'grouped' ) ) { 
  317. $product->set_manage_stock( 'no' ); 
  318. $product->set_backorders( 'no' ); 
  319. $product->set_stock_quantity( '' ); 
  320. $product->set_stock_status( $stock_status ); 
  321. } elseif ( $product->is_type( 'external' ) ) { 
  322. $product->set_manage_stock( 'no' ); 
  323. $product->set_backorders( 'no' ); 
  324. $product->set_stock_quantity( '' ); 
  325. $product->set_stock_status( 'instock' ); 
  326. } elseif ( $product->get_manage_stock() ) { 
  327. // Stock status is always determined by children so sync later. 
  328. if ( ! $product->is_type( 'variable' ) ) { 
  329. $product->set_stock_status( $stock_status ); 
  330.  
  331. // Stock quantity. 
  332. if ( isset( $request['stock_quantity'] ) ) { 
  333. $product->set_stock_quantity( wc_stock_amount( $request['stock_quantity'] ) ); 
  334. } elseif ( isset( $request['inventory_delta'] ) ) { 
  335. $stock_quantity = wc_stock_amount( $product->get_stock_quantity() ); 
  336. $stock_quantity += wc_stock_amount( $request['inventory_delta'] ); 
  337. $product->set_stock_quantity( wc_stock_amount( $stock_quantity ) ); 
  338. } else { 
  339. // Don't manage stock. 
  340. $product->set_manage_stock( 'no' ); 
  341. $product->set_stock_quantity( '' ); 
  342. $product->set_stock_status( $stock_status ); 
  343. } elseif ( ! $product->is_type( 'variable' ) ) { 
  344. $product->set_stock_status( $stock_status ); 
  345.  
  346. // Upsells. 
  347. if ( isset( $request['upsell_ids'] ) ) { 
  348. $upsells = array(); 
  349. $ids = $request['upsell_ids']; 
  350.  
  351. if ( ! empty( $ids ) ) { 
  352. foreach ( $ids as $id ) { 
  353. if ( $id && $id > 0 ) { 
  354. $upsells[] = $id; 
  355.  
  356. $product->set_upsell_ids( $upsells ); 
  357.  
  358. // Cross sells. 
  359. if ( isset( $request['cross_sell_ids'] ) ) { 
  360. $crosssells = array(); 
  361. $ids = $request['cross_sell_ids']; 
  362.  
  363. if ( ! empty( $ids ) ) { 
  364. foreach ( $ids as $id ) { 
  365. if ( $id && $id > 0 ) { 
  366. $crosssells[] = $id; 
  367.  
  368. $product->set_cross_sell_ids( $crosssells ); 
  369.  
  370. // Product categories. 
  371. if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) { 
  372. $product = $this->save_taxonomy_terms( $product, $request['categories'] ); 
  373.  
  374. // Product tags. 
  375. if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) { 
  376. $product = $this->save_taxonomy_terms( $product, $request['tags'], 'tag' ); 
  377.  
  378. // Downloadable. 
  379. if ( isset( $request['downloadable'] ) ) { 
  380. $product->set_downloadable( $request['downloadable'] ); 
  381.  
  382. // Downloadable options. 
  383. if ( $product->get_downloadable() ) { 
  384.  
  385. // Downloadable files. 
  386. if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) { 
  387. $product = $this->save_downloadable_files( $product, $request['downloads'] ); 
  388.  
  389. // Download limit. 
  390. if ( isset( $request['download_limit'] ) ) { 
  391. $product->set_download_limit( $request['download_limit'] ); 
  392.  
  393. // Download expiry. 
  394. if ( isset( $request['download_expiry'] ) ) { 
  395. $product->set_download_expiry( $request['download_expiry'] ); 
  396.  
  397. // Product url and button text for external products. 
  398. if ( $product->is_type( 'external' ) ) { 
  399. if ( isset( $request['external_url'] ) ) { 
  400. $product->set_product_url( $request['external_url'] ); 
  401.  
  402. if ( isset( $request['button_text'] ) ) { 
  403. $product->set_button_text( $request['button_text'] ); 
  404.  
  405. // Save default attributes for variable products. 
  406. if ( $product->is_type( 'variable' ) ) { 
  407. $product = $this->save_default_attributes( $product, $request ); 
  408.  
  409. return $product; 
  410.  
  411. /** 
  412. * Save variations. 
  413. * @deprecated 3.0.0 
  414. * @throws WC_REST_Exception REST API exceptions. 
  415. * @param WC_Product $product Product instance. 
  416. * @param WP_REST_Request $request Request data. 
  417. * @return bool 
  418. */ 
  419. protected function save_variations_data( $product, $request ) { 
  420. foreach ( $request['variations'] as $menu_order => $data ) { 
  421. $variation = new WC_Product_Variation( isset( $data['id'] ) ? absint( $data['id'] ) : 0 ); 
  422.  
  423. // Create initial name and status. 
  424. if ( ! $variation->get_slug() ) { 
  425. /** translators: 1: variation id 2: product name */ 
  426. $variation->set_name( sprintf( __( 'Variation #%1$s of %2$s', 'woocommerce' ), $variation->get_id(), $product->get_name() ) ); 
  427. $variation->set_status( isset( $data['visible'] ) && false === $data['visible'] ? 'private' : 'publish' ); 
  428.  
  429. // Parent ID. 
  430. $variation->set_parent_id( $product->get_id() ); 
  431.  
  432. // Menu order. 
  433. $variation->set_menu_order( $menu_order ); 
  434.  
  435. // Status. 
  436. if ( isset( $data['visible'] ) ) { 
  437. $variation->set_status( false === $data['visible'] ? 'private' : 'publish' ); 
  438.  
  439. // SKU. 
  440. if ( isset( $data['sku'] ) ) { 
  441. $variation->set_sku( wc_clean( $data['sku'] ) ); 
  442.  
  443. // Thumbnail. 
  444. if ( isset( $data['image'] ) && is_array( $data['image'] ) ) { 
  445. $image = $data['image']; 
  446. $image = current( $image ); 
  447. if ( is_array( $image ) ) { 
  448. $image['position'] = 0; 
  449.  
  450. $variation = $this->set_product_images( $variation, array( $image ) ); 
  451.  
  452. // Virtual variation. 
  453. if ( isset( $data['virtual'] ) ) { 
  454. $variation->set_virtual( $data['virtual'] ); 
  455.  
  456. // Downloadable variation. 
  457. if ( isset( $data['downloadable'] ) ) { 
  458. $variation->set_downloadable( $data['downloadable'] ); 
  459.  
  460. // Downloads. 
  461. if ( $variation->get_downloadable() ) { 
  462. // Downloadable files. 
  463. if ( isset( $data['downloads'] ) && is_array( $data['downloads'] ) ) { 
  464. $variation = $this->save_downloadable_files( $variation, $data['downloads'] ); 
  465.  
  466. // Download limit. 
  467. if ( isset( $data['download_limit'] ) ) { 
  468. $variation->set_download_limit( $data['download_limit'] ); 
  469.  
  470. // Download expiry. 
  471. if ( isset( $data['download_expiry'] ) ) { 
  472. $variation->set_download_expiry( $data['download_expiry'] ); 
  473.  
  474. // Shipping data. 
  475. $variation = $this->save_product_shipping_data( $variation, $data ); 
  476.  
  477. // Stock handling. 
  478. if ( isset( $data['manage_stock'] ) ) { 
  479. $variation->set_manage_stock( $data['manage_stock'] ); 
  480.  
  481. if ( isset( $data['in_stock'] ) ) { 
  482. $variation->set_stock_status( true === $data['in_stock'] ? 'instock' : 'outofstock' ); 
  483.  
  484. if ( isset( $data['backorders'] ) ) { 
  485. $variation->set_backorders( $data['backorders'] ); 
  486.  
  487. if ( $variation->get_manage_stock() ) { 
  488. if ( isset( $data['stock_quantity'] ) ) { 
  489. $variation->set_stock_quantity( $data['stock_quantity'] ); 
  490. } elseif ( isset( $data['inventory_delta'] ) ) { 
  491. $stock_quantity = wc_stock_amount( $variation->get_stock_quantity() ); 
  492. $stock_quantity += wc_stock_amount( $data['inventory_delta'] ); 
  493. $variation->set_stock_quantity( $stock_quantity ); 
  494. } else { 
  495. $variation->set_backorders( 'no' ); 
  496. $variation->set_stock_quantity( '' ); 
  497.  
  498. // Regular Price. 
  499. if ( isset( $data['regular_price'] ) ) { 
  500. $variation->set_regular_price( $data['regular_price'] ); 
  501.  
  502. // Sale Price. 
  503. if ( isset( $data['sale_price'] ) ) { 
  504. $variation->set_sale_price( $data['sale_price'] ); 
  505.  
  506. if ( isset( $data['date_on_sale_from'] ) ) { 
  507. $variation->set_date_on_sale_from( $data['date_on_sale_from'] ); 
  508.  
  509. if ( isset( $data['date_on_sale_to'] ) ) { 
  510. $variation->set_date_on_sale_to( $data['date_on_sale_to'] ); 
  511.  
  512. // Tax class. 
  513. if ( isset( $data['tax_class'] ) ) { 
  514. $variation->set_tax_class( $data['tax_class'] ); 
  515.  
  516. // Description. 
  517. if ( isset( $data['description'] ) ) { 
  518. $variation->set_description( wp_kses_post( $data['description'] ) ); 
  519.  
  520. // Update taxonomies. 
  521. if ( isset( $data['attributes'] ) ) { 
  522. $attributes = array(); 
  523. $parent_attributes = $product->get_attributes(); 
  524.  
  525. foreach ( $data['attributes'] as $attribute ) { 
  526. $attribute_id = 0; 
  527. $attribute_name = ''; 
  528.  
  529. // Check ID for global attributes or name for product attributes. 
  530. if ( ! empty( $attribute['id'] ) ) { 
  531. $attribute_id = absint( $attribute['id'] ); 
  532. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  533. } elseif ( ! empty( $attribute['name'] ) ) { 
  534. $attribute_name = sanitize_title( $attribute['name'] ); 
  535.  
  536. if ( ! $attribute_id && ! $attribute_name ) { 
  537. continue; 
  538.  
  539. if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) { 
  540. continue; 
  541.  
  542. $attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() ); 
  543. $attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; 
  544.  
  545. if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) { 
  546. // If dealing with a taxonomy, we need to get the slug from the name posted to the API. 
  547. $term = get_term_by( 'name', $attribute_value, $attribute_name ); 
  548.  
  549. if ( $term && ! is_wp_error( $term ) ) { 
  550. $attribute_value = $term->slug; 
  551. } else { 
  552. $attribute_value = sanitize_title( $attribute_value ); 
  553.  
  554. $attributes[ $attribute_key ] = $attribute_value; 
  555.  
  556. $variation->set_attributes( $attributes ); 
  557.  
  558. $variation->save(); 
  559.  
  560. do_action( 'woocommerce_rest_save_product_variation', $variation->get_id(), $menu_order, $data ); 
  561.  
  562. return true; 
  563.  
  564. /** 
  565. * Add post meta fields. 
  566. * @deprecated 3.0.0 
  567. * @param WP_Post $post Post data. 
  568. * @param WP_REST_Request $request Request data. 
  569. * @return bool|WP_Error 
  570. */ 
  571. protected function add_post_meta_fields( $post, $request ) { 
  572. return $this->update_post_meta_fields( $post, $request ); 
  573.  
  574. /** 
  575. * Update post meta fields. 
  576. * @param WP_Post $post Post data. 
  577. * @param WP_REST_Request $request Request data. 
  578. * @return bool|WP_Error 
  579. */ 
  580. protected function update_post_meta_fields( $post, $request ) { 
  581. $product = wc_get_product( $post ); 
  582.  
  583. // Check for featured/gallery images, upload it and set it. 
  584. if ( isset( $request['images'] ) ) { 
  585. $product = $this->set_product_images( $product, $request['images'] ); 
  586.  
  587. // Save product meta fields. 
  588. $product = $this->set_product_meta( $product, $request ); 
  589.  
  590. // Save the product data. 
  591. $product->save(); 
  592.  
  593. // Save variations. 
  594. if ( $product->is_type( 'variable' ) ) { 
  595. if ( isset( $request['variations'] ) && is_array( $request['variations'] ) ) { 
  596. $this->save_variations_data( $product, $request ); 
  597.  
  598. // Clear caches here so in sync with any new variations/children. 
  599. wc_delete_product_transients( $product->get_id() ); 
  600. wp_cache_delete( 'product-' . $product->get_id(), 'products' ); 
  601.  
  602. return true; 
  603.  
  604. /** 
  605. * Delete post. 
  606. * @deprecated 3.0.0 
  607. * @param int|WP_Post $id Post ID or WP_Post instance. 
  608. */ 
  609. protected function delete_post( $id ) { 
  610. if ( ! empty( $id->ID ) ) { 
  611. $id = $id->ID; 
  612. } elseif ( ! is_numeric( $id ) || 0 >= $id ) { 
  613. return; 
  614.  
  615. // Delete product attachments. 
  616. $attachments = get_posts( array( 
  617. 'post_parent' => $id,  
  618. 'post_status' => 'any',  
  619. 'post_type' => 'attachment',  
  620. ) ); 
  621.  
  622. foreach ( (array) $attachments as $attachment ) { 
  623. wp_delete_attachment( $attachment->ID, true ); 
  624.  
  625. // Delete product. 
  626. $product = wc_get_product( $id ); 
  627. $product->delete( true ); 
  628.  
  629. /** 
  630. * Get post types. 
  631. * @deprecated 3.0.0 
  632. * @return array 
  633. */ 
  634. protected function get_post_types() { 
  635. return array( 'product', 'product_variation' ); 
  636.  
  637. /** 
  638. * Save product images. 
  639. * @deprecated 3.0.0 
  640. * @param int $product_id 
  641. * @param array $images 
  642. * @throws WC_REST_Exception 
  643. */ 
  644. protected function save_product_images( $product_id, $images ) { 
  645. $product = wc_get_product( $product_id ); 
  646.  
  647. return set_product_images( $product, $images );