/includes/api/legacy/v3/class-wc-api-products.php

  1. <?php 
  2. /** 
  3. * WooCommerce API Products Class 
  4. * 
  5. * Handles requests to the /products endpoint 
  6. * 
  7. * @author WooThemes 
  8. * @category API 
  9. * @package WooCommerce/API 
  10. * @since 2.1 
  11. * @version 3.0 
  12. */ 
  13.  
  14. if ( ! defined( 'ABSPATH' ) ) { 
  15. exit; // Exit if accessed directly 
  16.  
  17. class WC_API_Products extends WC_API_Resource { 
  18.  
  19. /** @var string $base the route base */ 
  20. protected $base = '/products'; 
  21.  
  22. /** 
  23. * Register the routes for this class 
  24. * 
  25. * GET/POST /products 
  26. * GET /products/count 
  27. * GET/PUT/DELETE /products/<id> 
  28. * GET /products/<id>/reviews 
  29. * 
  30. * @since 2.1 
  31. * @param array $routes 
  32. * @return array 
  33. */ 
  34. public function register_routes( $routes ) { 
  35.  
  36. # GET/POST /products 
  37. $routes[ $this->base ] = array( 
  38. array( array( $this, 'get_products' ), WC_API_Server::READABLE ),  
  39. array( array( $this, 'create_product' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  40. ); 
  41.  
  42. # GET /products/count 
  43. $routes[ $this->base . '/count' ] = array( 
  44. array( array( $this, 'get_products_count' ), WC_API_Server::READABLE ),  
  45. ); 
  46.  
  47. # GET/PUT/DELETE /products/<id> 
  48. $routes[ $this->base . '/(?P<id>\d+)' ] = array( 
  49. array( array( $this, 'get_product' ), WC_API_Server::READABLE ),  
  50. array( array( $this, 'edit_product' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  51. array( array( $this, 'delete_product' ), WC_API_Server::DELETABLE ),  
  52. ); 
  53.  
  54. # GET /products/<id>/reviews 
  55. $routes[ $this->base . '/(?P<id>\d+)/reviews' ] = array( 
  56. array( array( $this, 'get_product_reviews' ), WC_API_Server::READABLE ),  
  57. ); 
  58.  
  59. # GET /products/<id>/orders 
  60. $routes[ $this->base . '/(?P<id>\d+)/orders' ] = array( 
  61. array( array( $this, 'get_product_orders' ), WC_API_Server::READABLE ),  
  62. ); 
  63.  
  64. # GET/POST /products/categories 
  65. $routes[ $this->base . '/categories' ] = array( 
  66. array( array( $this, 'get_product_categories' ), WC_API_Server::READABLE ),  
  67. array( array( $this, 'create_product_category' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  68. ); 
  69.  
  70. # GET/PUT/DELETE /products/categories/<id> 
  71. $routes[ $this->base . '/categories/(?P<id>\d+)' ] = array( 
  72. array( array( $this, 'get_product_category' ), WC_API_Server::READABLE ),  
  73. array( array( $this, 'edit_product_category' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  74. array( array( $this, 'delete_product_category' ), WC_API_Server::DELETABLE ),  
  75. ); 
  76.  
  77. # GET/POST /products/tags 
  78. $routes[ $this->base . '/tags' ] = array( 
  79. array( array( $this, 'get_product_tags' ), WC_API_Server::READABLE ),  
  80. array( array( $this, 'create_product_tag' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  81. ); 
  82.  
  83. # GET/PUT/DELETE /products/tags/<id> 
  84. $routes[ $this->base . '/tags/(?P<id>\d+)' ] = array( 
  85. array( array( $this, 'get_product_tag' ), WC_API_Server::READABLE ),  
  86. array( array( $this, 'edit_product_tag' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  87. array( array( $this, 'delete_product_tag' ), WC_API_Server::DELETABLE ),  
  88. ); 
  89.  
  90. # GET/POST /products/shipping_classes 
  91. $routes[ $this->base . '/shipping_classes' ] = array( 
  92. array( array( $this, 'get_product_shipping_classes' ), WC_API_Server::READABLE ),  
  93. array( array( $this, 'create_product_shipping_class' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  94. ); 
  95.  
  96. # GET/PUT/DELETE /products/shipping_classes/<id> 
  97. $routes[ $this->base . '/shipping_classes/(?P<id>\d+)' ] = array( 
  98. array( array( $this, 'get_product_shipping_class' ), WC_API_Server::READABLE ),  
  99. array( array( $this, 'edit_product_shipping_class' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  100. array( array( $this, 'delete_product_shipping_class' ), WC_API_Server::DELETABLE ),  
  101. ); 
  102.  
  103. # GET/POST /products/attributes 
  104. $routes[ $this->base . '/attributes' ] = array( 
  105. array( array( $this, 'get_product_attributes' ), WC_API_Server::READABLE ),  
  106. array( array( $this, 'create_product_attribute' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  107. ); 
  108.  
  109. # GET/PUT/DELETE /products/attributes/<id> 
  110. $routes[ $this->base . '/attributes/(?P<id>\d+)' ] = array( 
  111. array( array( $this, 'get_product_attribute' ), WC_API_Server::READABLE ),  
  112. array( array( $this, 'edit_product_attribute' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  113. array( array( $this, 'delete_product_attribute' ), WC_API_Server::DELETABLE ),  
  114. ); 
  115.  
  116. # GET/POST /products/attributes/<attribute_id>/terms 
  117. $routes[ $this->base . '/attributes/(?P<attribute_id>\d+)/terms' ] = array( 
  118. array( array( $this, 'get_product_attribute_terms' ), WC_API_Server::READABLE ),  
  119. array( array( $this, 'create_product_attribute_term' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  120. ); 
  121.  
  122. # GET/PUT/DELETE /products/attributes/<attribute_id>/terms/<id> 
  123. $routes[ $this->base . '/attributes/(?P<attribute_id>\d+)/terms/(?P<id>\d+)' ] = array( 
  124. array( array( $this, 'get_product_attribute_term' ), WC_API_Server::READABLE ),  
  125. array( array( $this, 'edit_product_attribute_term' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  126. array( array( $this, 'delete_product_attribute_term' ), WC_API_Server::DELETABLE ),  
  127. ); 
  128.  
  129. # POST|PUT /products/bulk 
  130. $routes[ $this->base . '/bulk' ] = array( 
  131. array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  132. ); 
  133.  
  134. return $routes; 
  135.  
  136. /** 
  137. * Get all products 
  138. * 
  139. * @since 2.1 
  140. * @param string $fields 
  141. * @param string $type 
  142. * @param array $filter 
  143. * @param int $page 
  144. * @return array 
  145. */ 
  146. public function get_products( $fields = null, $type = null, $filter = array(), $page = 1 ) { 
  147.  
  148. if ( ! empty( $type ) ) { 
  149. $filter['type'] = $type; 
  150.  
  151. $filter['page'] = $page; 
  152.  
  153. $query = $this->query_products( $filter ); 
  154.  
  155. $products = array(); 
  156.  
  157. foreach ( $query->posts as $product_id ) { 
  158.  
  159. if ( ! $this->is_readable( $product_id ) ) { 
  160. continue; 
  161.  
  162. $products[] = current( $this->get_product( $product_id, $fields ) ); 
  163.  
  164. $this->server->add_pagination_headers( $query ); 
  165.  
  166. return array( 'products' => $products ); 
  167.  
  168. /** 
  169. * Get the product for the given ID 
  170. * 
  171. * @since 2.1 
  172. * @param int $id the product ID 
  173. * @param string $fields 
  174. * @return array 
  175. */ 
  176. public function get_product( $id, $fields = null ) { 
  177.  
  178. $id = $this->validate_request( $id, 'product', 'read' ); 
  179.  
  180. if ( is_wp_error( $id ) ) { 
  181. return $id; 
  182.  
  183. $product = wc_get_product( $id ); 
  184.  
  185. // add data that applies to every product type 
  186. $product_data = $this->get_product_data( $product ); 
  187.  
  188. // add variations to variable products 
  189. if ( $product->is_type( 'variable' ) && $product->has_child() ) { 
  190. $product_data['variations'] = $this->get_variation_data( $product ); 
  191.  
  192. // add the parent product data to an individual variation 
  193. if ( $product->is_type( 'variation' ) && $product->get_parent_id() ) { 
  194. $product_data['parent'] = $this->get_product_data( $product->get_parent_id() ); 
  195.  
  196. // Add grouped products data 
  197. if ( $product->is_type( 'grouped' ) && $product->has_child() ) { 
  198. $product_data['grouped_products'] = $this->get_grouped_products_data( $product ); 
  199.  
  200. if ( $product->is_type( 'simple' ) ) { 
  201. $parent_id = $product->get_parent_id(); 
  202. if ( ! empty( $parent_id ) ) { 
  203. $_product = wc_get_product( $parent_id ); 
  204. $product_data['parent'] = $this->get_product_data( $_product ); 
  205.  
  206. return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) ); 
  207.  
  208. /** 
  209. * Get the total number of products 
  210. * 
  211. * @since 2.1 
  212. * @param string $type 
  213. * @param array $filter 
  214. * @return array 
  215. */ 
  216. public function get_products_count( $type = null, $filter = array() ) { 
  217. try { 
  218. if ( ! current_user_can( 'read_private_products' ) ) { 
  219. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_products_count', __( 'You do not have permission to read the products count', 'woocommerce' ), 401 ); 
  220.  
  221. if ( ! empty( $type ) ) { 
  222. $filter['type'] = $type; 
  223.  
  224. $query = $this->query_products( $filter ); 
  225.  
  226. return array( 'count' => (int) $query->found_posts ); 
  227. } catch ( WC_API_Exception $e ) { 
  228. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  229.  
  230. /** 
  231. * Create a new product. 
  232. * 
  233. * @since 2.2 
  234. * @param array $data posted data 
  235. * @return array 
  236. */ 
  237. public function create_product( $data ) { 
  238. $id = 0; 
  239.  
  240. try { 
  241. if ( ! isset( $data['product'] ) ) { 
  242. throw new WC_API_Exception( 'woocommerce_api_missing_product_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product' ), 400 ); 
  243.  
  244. $data = $data['product']; 
  245.  
  246. // Check permissions. 
  247. if ( ! current_user_can( 'publish_products' ) ) { 
  248. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product', __( 'You do not have permission to create products', 'woocommerce' ), 401 ); 
  249.  
  250. $data = apply_filters( 'woocommerce_api_create_product_data', $data, $this ); 
  251.  
  252. // Check if product title is specified. 
  253. if ( ! isset( $data['title'] ) ) { 
  254. throw new WC_API_Exception( 'woocommerce_api_missing_product_title', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'title' ), 400 ); 
  255.  
  256. // Check product type. 
  257. if ( ! isset( $data['type'] ) ) { 
  258. $data['type'] = 'simple'; 
  259.  
  260. // Set visible visibility when not sent. 
  261. if ( ! isset( $data['catalog_visibility'] ) ) { 
  262. $data['catalog_visibility'] = 'visible'; 
  263.  
  264. // Validate the product type. 
  265. if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) { 
  266. throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 ); 
  267.  
  268. // Enable description html tags. 
  269. $post_content = isset( $data['description'] ) ? wc_clean( $data['description'] ) : ''; 
  270. if ( $post_content && isset( $data['enable_html_description'] ) && true === $data['enable_html_description'] ) { 
  271.  
  272. $post_content = $data['description']; 
  273.  
  274. // Enable short description html tags. 
  275. $post_excerpt = isset( $data['short_description'] ) ? wc_clean( $data['short_description'] ) : ''; 
  276. if ( $post_excerpt && isset( $data['enable_html_short_description'] ) && true === $data['enable_html_short_description'] ) { 
  277. $post_excerpt = $data['short_description']; 
  278.  
  279. $classname = WC_Product_Factory::get_classname_from_product_type( $data['type'] ); 
  280. if ( ! class_exists( $classname ) ) { 
  281. $classname = 'WC_Product_Simple'; 
  282. $product = new $classname(); 
  283.  
  284. $product->set_name( wc_clean( $data['title'] ) ); 
  285. $product->set_status( isset( $data['status'] ) ? wc_clean( $data['status'] ) : 'publish' ); 
  286. $product->set_short_description( isset( $data['short_description'] ) ? $post_excerpt : '' ); 
  287. $product->set_description( isset( $data['description'] ) ? $post_content : '' ); 
  288. $product->set_menu_order( isset( $data['menu_order'] ) ? intval( $data['menu_order'] ) : 0 ); 
  289.  
  290. if ( ! empty( $data['name'] ) ) { 
  291. $product->set_slug( sanitize_title( $data['name'] ) ); 
  292.  
  293. // Attempts to create the new product. 
  294. $product->save(); 
  295. $id = $product->get_id(); 
  296.  
  297. // Checks for an error in the product creation. 
  298. if ( 0 >= $id ) { 
  299. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product', $id->get_error_message(), 400 ); 
  300.  
  301. // Check for featured/gallery images, upload it and set it. 
  302. if ( isset( $data['images'] ) ) { 
  303. $product = $this->save_product_images( $product, $data['images'] ); 
  304.  
  305. // Save product meta fields. 
  306. $product = $this->save_product_meta( $product, $data ); 
  307. $product->save(); 
  308.  
  309. // Save variations. 
  310. if ( isset( $data['type'] ) && 'variable' == $data['type'] && isset( $data['variations'] ) && is_array( $data['variations'] ) ) { 
  311. $this->save_variations( $product, $data ); 
  312.  
  313. do_action( 'woocommerce_api_create_product', $id, $data ); 
  314.  
  315. // Clear cache/transients. 
  316. wc_delete_product_transients( $id ); 
  317.  
  318. $this->server->send_status( 201 ); 
  319.  
  320. return $this->get_product( $id ); 
  321. } catch ( WC_Data_Exception $e ) { 
  322. $this->clear_product( $id ); 
  323. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  324. } catch ( WC_API_Exception $e ) { 
  325. $this->clear_product( $id ); 
  326. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  327.  
  328. /** 
  329. * Edit a product 
  330. * 
  331. * @since 2.2 
  332. * @param int $id the product ID 
  333. * @param array $data 
  334. * @return array 
  335. */ 
  336. public function edit_product( $id, $data ) { 
  337. try { 
  338. if ( ! isset( $data['product'] ) ) { 
  339. throw new WC_API_Exception( 'woocommerce_api_missing_product_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product' ), 400 ); 
  340.  
  341. $data = $data['product']; 
  342.  
  343. $id = $this->validate_request( $id, 'product', 'edit' ); 
  344.  
  345. if ( is_wp_error( $id ) ) { 
  346. return $id; 
  347.  
  348. $product = wc_get_product( $id ); 
  349.  
  350. $data = apply_filters( 'woocommerce_api_edit_product_data', $data, $this ); 
  351.  
  352. // Product title. 
  353. if ( isset( $data['title'] ) ) { 
  354. $product->set_name( wc_clean( $data['title'] ) ); 
  355.  
  356. // Product name (slug). 
  357. if ( isset( $data['name'] ) ) { 
  358. $product->set_slug( wc_clean( $data['name'] ) ); 
  359.  
  360. // Product status. 
  361. if ( isset( $data['status'] ) ) { 
  362. $product->set_status( wc_clean( $data['status'] ) ); 
  363.  
  364. // Product short description. 
  365. if ( isset( $data['short_description'] ) ) { 
  366. // Enable short description html tags. 
  367. $post_excerpt = ( isset( $data['enable_html_short_description'] ) && true === $data['enable_html_short_description'] ) ? $data['short_description'] : wc_clean( $data['short_description'] ); 
  368. $product->set_short_description( $post_excerpt ); 
  369.  
  370. // Product description. 
  371. if ( isset( $data['description'] ) ) { 
  372. // Enable description html tags. 
  373. $post_content = ( isset( $data['enable_html_description'] ) && true === $data['enable_html_description'] ) ? $data['description'] : wc_clean( $data['description'] ); 
  374. $product->set_description( $post_content ); 
  375.  
  376. // Validate the product type. 
  377. if ( isset( $data['type'] ) && ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_product_types() ) ) ) { 
  378. throw new WC_API_Exception( 'woocommerce_api_invalid_product_type', sprintf( __( 'Invalid product type - the product type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_product_types() ) ) ), 400 ); 
  379.  
  380. // Menu order. 
  381. if ( isset( $data['menu_order'] ) ) { 
  382. $product->set_menu_order( intval( $data['menu_order'] ) ); 
  383.  
  384. // Check for featured/gallery images, upload it and set it. 
  385. if ( isset( $data['images'] ) ) { 
  386. $product = $this->save_product_images( $product, $data['images'] ); 
  387.  
  388. // Save product meta fields. 
  389. $product = $this->save_product_meta( $product, $data ); 
  390.  
  391. // Save variations. 
  392. if ( $product->is_type( 'variable' ) ) { 
  393. if ( isset( $data['variations'] ) && is_array( $data['variations'] ) ) { 
  394. $this->save_variations( $product, $data ); 
  395. } else { 
  396. // Just sync variations. 
  397. $product = WC_Product_Variable::sync( $product, false ); 
  398.  
  399. $product->save(); 
  400.  
  401. do_action( 'woocommerce_api_edit_product', $id, $data ); 
  402.  
  403. // Clear cache/transients. 
  404. wc_delete_product_transients( $id ); 
  405.  
  406. return $this->get_product( $id ); 
  407. } catch ( WC_Data_Exception $e ) { 
  408. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  409. } catch ( WC_API_Exception $e ) { 
  410. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  411.  
  412. /** 
  413. * Delete a product. 
  414. * 
  415. * @since 2.2 
  416. * @param int $id the product ID. 
  417. * @param bool $force true to permanently delete order, false to move to trash. 
  418. * @return array 
  419. */ 
  420. public function delete_product( $id, $force = false ) { 
  421.  
  422. $id = $this->validate_request( $id, 'product', 'delete' ); 
  423.  
  424. if ( is_wp_error( $id ) ) { 
  425. return $id; 
  426.  
  427. $product = wc_get_product( $id ); 
  428.  
  429. do_action( 'woocommerce_api_delete_product', $id, $this ); 
  430.  
  431. // If we're forcing, then delete permanently. 
  432. if ( $force ) { 
  433. if ( $product->is_type( 'variable' ) ) { 
  434. foreach ( $product->get_children() as $child_id ) { 
  435. $child = wc_get_product( $child_id ); 
  436. $child->delete( true ); 
  437. } elseif ( $product->is_type( 'grouped' ) ) { 
  438. foreach ( $product->get_children() as $child_id ) { 
  439. $child = wc_get_product( $child_id ); 
  440. $child->set_parent_id( 0 ); 
  441. $child->save(); 
  442.  
  443. $product->delete( true ); 
  444. $result = $product->get_id() > 0 ? false : true; 
  445. } else { 
  446. $product->delete(); 
  447. $result = 'trash' === $product->get_status(); 
  448.  
  449. if ( ! $result ) { 
  450. return new WP_Error( 'woocommerce_api_cannot_delete_product', sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), 'product' ), array( 'status' => 500 ) ); 
  451.  
  452. // Delete parent product transients. 
  453. if ( $parent_id = wp_get_post_parent_id( $id ) ) { 
  454. wc_delete_product_transients( $parent_id ); 
  455.  
  456. if ( $force ) { 
  457. return array( 'message' => sprintf( __( 'Permanently deleted %s', 'woocommerce' ), 'product' ) ); 
  458. } else { 
  459. $this->server->send_status( '202' ); 
  460.  
  461. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product' ) ); 
  462.  
  463. /** 
  464. * Get the reviews for a product 
  465. * 
  466. * @since 2.1 
  467. * @param int $id the product ID to get reviews for 
  468. * @param string $fields fields to include in response 
  469. * @return array 
  470. */ 
  471. public function get_product_reviews( $id, $fields = null ) { 
  472.  
  473. $id = $this->validate_request( $id, 'product', 'read' ); 
  474.  
  475. if ( is_wp_error( $id ) ) { 
  476. return $id; 
  477.  
  478. $comments = get_approved_comments( $id ); 
  479. $reviews = array(); 
  480.  
  481. foreach ( $comments as $comment ) { 
  482.  
  483. $reviews[] = array( 
  484. 'id' => intval( $comment->comment_ID ),  
  485. 'created_at' => $this->server->format_datetime( $comment->comment_date_gmt ),  
  486. 'review' => $comment->comment_content,  
  487. 'rating' => get_comment_meta( $comment->comment_ID, 'rating', true ),  
  488. 'reviewer_name' => $comment->comment_author,  
  489. 'reviewer_email' => $comment->comment_author_email,  
  490. 'verified' => wc_review_is_from_verified_owner( $comment->comment_ID ),  
  491. ); 
  492.  
  493. return array( 'product_reviews' => apply_filters( 'woocommerce_api_product_reviews_response', $reviews, $id, $fields, $comments, $this->server ) ); 
  494.  
  495. /** 
  496. * Get the orders for a product 
  497. * 
  498. * @since 2.4.0 
  499. * @param int $id the product ID to get orders for 
  500. * @param string fields fields to retrieve 
  501. * @param string $filter filters to include in response 
  502. * @param string $status the order status to retrieve 
  503. * @param $page $page page to retrieve 
  504. * @return array 
  505. */ 
  506. public function get_product_orders( $id, $fields = null, $filter = array(), $status = null, $page = 1 ) { 
  507. global $wpdb; 
  508.  
  509. $id = $this->validate_request( $id, 'product', 'read' ); 
  510.  
  511. if ( is_wp_error( $id ) ) { 
  512. return $id; 
  513.  
  514. $order_ids = $wpdb->get_col( $wpdb->prepare( " 
  515. SELECT order_id 
  516. FROM {$wpdb->prefix}woocommerce_order_items 
  517. WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d ) 
  518. AND order_item_type = 'line_item' 
  519. ", $id ) ); 
  520.  
  521. if ( empty( $order_ids ) ) { 
  522. return array( 'orders' => array() ); 
  523.  
  524. $filter = array_merge( $filter, array( 
  525. 'in' => implode( ', ', $order_ids ),  
  526. ) ); 
  527.  
  528. $orders = WC()->api->WC_API_Orders->get_orders( $fields, $filter, $status, $page ); 
  529.  
  530. return array( 'orders' => apply_filters( 'woocommerce_api_product_orders_response', $orders['orders'], $id, $filter, $fields, $this->server ) ); 
  531.  
  532. /** 
  533. * Get a listing of product categories 
  534. * 
  535. * @since 2.2 
  536. * @param string|null $fields fields to limit response to 
  537. * @return array 
  538. */ 
  539. public function get_product_categories( $fields = null ) { 
  540. try { 
  541. // Permissions check 
  542. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  543. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 ); 
  544.  
  545. $product_categories = array(); 
  546.  
  547. $terms = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'ids' ) ); 
  548.  
  549. foreach ( $terms as $term_id ) { 
  550. $product_categories[] = current( $this->get_product_category( $term_id, $fields ) ); 
  551.  
  552. return array( 'product_categories' => apply_filters( 'woocommerce_api_product_categories_response', $product_categories, $terms, $fields, $this ) ); 
  553. } catch ( WC_API_Exception $e ) { 
  554. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  555.  
  556. /** 
  557. * Get the product category for the given ID 
  558. * 
  559. * @since 2.2 
  560. * @param string $id product category term ID 
  561. * @param string|null $fields fields to limit response to 
  562. * @return array 
  563. */ 
  564. public function get_product_category( $id, $fields = null ) { 
  565. try { 
  566. $id = absint( $id ); 
  567.  
  568. // Validate ID 
  569. if ( empty( $id ) ) { 
  570. throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'Invalid product category ID', 'woocommerce' ), 400 ); 
  571.  
  572. // Permissions check 
  573. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  574. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product categories', 'woocommerce' ), 401 ); 
  575.  
  576. $term = get_term( $id, 'product_cat' ); 
  577.  
  578. if ( is_wp_error( $term ) || is_null( $term ) ) { 
  579. throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_id', __( 'A product category with the provided ID could not be found', 'woocommerce' ), 404 ); 
  580.  
  581. $term_id = intval( $term->term_id ); 
  582.  
  583. // Get category display type 
  584. $display_type = get_woocommerce_term_meta( $term_id, 'display_type' ); 
  585.  
  586. // Get category image 
  587. $image = ''; 
  588. if ( $image_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id' ) ) { 
  589. $image = wp_get_attachment_url( $image_id ); 
  590.  
  591. $product_category = array( 
  592. 'id' => $term_id,  
  593. 'name' => $term->name,  
  594. 'slug' => $term->slug,  
  595. 'parent' => $term->parent,  
  596. 'description' => $term->description,  
  597. 'display' => $display_type ? $display_type : 'default',  
  598. 'image' => $image ? esc_url( $image ) : '',  
  599. 'count' => intval( $term->count ),  
  600. ); 
  601.  
  602. return array( 'product_category' => apply_filters( 'woocommerce_api_product_category_response', $product_category, $id, $fields, $term, $this ) ); 
  603. } catch ( WC_API_Exception $e ) { 
  604. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  605.  
  606. /** 
  607. * Create a new product category. 
  608. * 
  609. * @since 2.5.0 
  610. * @param array $data Posted data 
  611. * @return array|WP_Error Product category if succeed, otherwise WP_Error 
  612. * will be returned 
  613. */ 
  614. public function create_product_category( $data ) { 
  615. global $wpdb; 
  616.  
  617. try { 
  618. if ( ! isset( $data['product_category'] ) ) { 
  619. throw new WC_API_Exception( 'woocommerce_api_missing_product_category_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_category' ), 400 ); 
  620.  
  621. // Check permissions 
  622. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  623. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_category', __( 'You do not have permission to create product categories', 'woocommerce' ), 401 ); 
  624.  
  625. $defaults = array( 
  626. 'name' => '',  
  627. 'slug' => '',  
  628. 'description' => '',  
  629. 'parent' => 0,  
  630. 'display' => 'default',  
  631. 'image' => '',  
  632. ); 
  633.  
  634. $data = wp_parse_args( $data['product_category'], $defaults ); 
  635. $data = apply_filters( 'woocommerce_api_create_product_category_data', $data, $this ); 
  636.  
  637. // Check parent. 
  638. $data['parent'] = absint( $data['parent'] ); 
  639. if ( $data['parent'] ) { 
  640. $parent = get_term_by( 'id', $data['parent'], 'product_cat' ); 
  641. if ( ! $parent ) { 
  642. throw new WC_API_Exception( 'woocommerce_api_invalid_product_category_parent', __( 'Product category parent is invalid', 'woocommerce' ), 400 ); 
  643.  
  644. // If value of image is numeric, assume value as image_id. 
  645. $image = $data['image']; 
  646. $image_id = 0; 
  647. if ( is_numeric( $image ) ) { 
  648. $image_id = absint( $image ); 
  649. } elseif ( ! empty( $image ) ) { 
  650. $upload = $this->upload_product_category_image( esc_url_raw( $image ) ); 
  651. $image_id = $this->set_product_category_image_as_attachment( $upload ); 
  652.  
  653. $insert = wp_insert_term( $data['name'], 'product_cat', $data ); 
  654. if ( is_wp_error( $insert ) ) { 
  655. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_category', $insert->get_error_message(), 400 ); 
  656.  
  657. $id = $insert['term_id']; 
  658.  
  659. update_woocommerce_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) ); 
  660.  
  661. // Check if image_id is a valid image attachment before updating the term meta. 
  662. if ( $image_id && wp_attachment_is_image( $image_id ) ) { 
  663. update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id ); 
  664.  
  665. do_action( 'woocommerce_api_create_product_category', $id, $data ); 
  666.  
  667. $this->server->send_status( 201 ); 
  668.  
  669. return $this->get_product_category( $id ); 
  670. } catch ( WC_API_Exception $e ) { 
  671. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  672.  
  673. /** 
  674. * Edit a product category. 
  675. * 
  676. * @since 2.5.0 
  677. * @param int $id Product category term ID 
  678. * @param array $data Posted data 
  679. * @return array|WP_Error Product category if succeed, otherwise WP_Error 
  680. * will be returned 
  681. */ 
  682. public function edit_product_category( $id, $data ) { 
  683. global $wpdb; 
  684.  
  685. try { 
  686. if ( ! isset( $data['product_category'] ) ) { 
  687. throw new WC_API_Exception( 'woocommerce_api_missing_product_category', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_category' ), 400 ); 
  688.  
  689. $id = absint( $id ); 
  690. $data = $data['product_category']; 
  691.  
  692. // Check permissions. 
  693. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  694. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_category', __( 'You do not have permission to edit product categories', 'woocommerce' ), 401 ); 
  695.  
  696. $data = apply_filters( 'woocommerce_api_edit_product_category_data', $data, $this ); 
  697. $category = $this->get_product_category( $id ); 
  698.  
  699. if ( is_wp_error( $category ) ) { 
  700. return $category; 
  701.  
  702. if ( isset( $data['image'] ) ) { 
  703. $image_id = 0; 
  704.  
  705. // If value of image is numeric, assume value as image_id. 
  706. $image = $data['image']; 
  707. if ( is_numeric( $image ) ) { 
  708. $image_id = absint( $image ); 
  709. } elseif ( ! empty( $image ) ) { 
  710. $upload = $this->upload_product_category_image( esc_url_raw( $image ) ); 
  711. $image_id = $this->set_product_category_image_as_attachment( $upload ); 
  712.  
  713. // In case client supplies invalid image or wants to unset category image. 
  714. if ( ! wp_attachment_is_image( $image_id ) ) { 
  715. $image_id = ''; 
  716.  
  717. $update = wp_update_term( $id, 'product_cat', $data ); 
  718. if ( is_wp_error( $update ) ) { 
  719. throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_catgory', __( 'Could not edit the category', 'woocommerce' ), 400 ); 
  720.  
  721. if ( ! empty( $data['display'] ) ) { 
  722. update_woocommerce_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) ); 
  723.  
  724. if ( isset( $image_id ) ) { 
  725. update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id ); 
  726.  
  727. do_action( 'woocommerce_api_edit_product_category', $id, $data ); 
  728.  
  729. return $this->get_product_category( $id ); 
  730. } catch ( WC_API_Exception $e ) { 
  731. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  732.  
  733. /** 
  734. * Delete a product category. 
  735. * 
  736. * @since 2.5.0 
  737. * @param int $id Product category term ID 
  738. * @return array|WP_Error Success message if succeed, otherwise WP_Error 
  739. * will be returned 
  740. */ 
  741. public function delete_product_category( $id ) { 
  742. global $wpdb; 
  743.  
  744. try { 
  745. // Check permissions 
  746. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  747. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_category', __( 'You do not have permission to delete product category', 'woocommerce' ), 401 ); 
  748.  
  749. $id = absint( $id ); 
  750. $deleted = wp_delete_term( $id, 'product_cat' ); 
  751. if ( ! $deleted || is_wp_error( $deleted ) ) { 
  752. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_category', __( 'Could not delete the category', 'woocommerce' ), 401 ); 
  753.  
  754. // When a term is deleted, delete its meta. 
  755. if ( get_option( 'db_version' ) < 34370 ) { 
  756. $wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $id ), array( '%d' ) ); 
  757.  
  758. do_action( 'woocommerce_api_delete_product_category', $id, $this ); 
  759.  
  760. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_category' ) ); 
  761. } catch ( WC_API_Exception $e ) { 
  762. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  763.  
  764. /** 
  765. * Get a listing of product tags. 
  766. * 
  767. * @since 2.5.0 
  768. * @param string|null $fields Fields to limit response to 
  769. * @return array Product tags 
  770. */ 
  771. public function get_product_tags( $fields = null ) { 
  772. try { 
  773. // Permissions check 
  774. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  775. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_tags', __( 'You do not have permission to read product tags', 'woocommerce' ), 401 ); 
  776.  
  777. $product_tags = array(); 
  778.  
  779. $terms = get_terms( 'product_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) ); 
  780.  
  781. foreach ( $terms as $term_id ) { 
  782. $product_tags[] = current( $this->get_product_tag( $term_id, $fields ) ); 
  783.  
  784. return array( 'product_tags' => apply_filters( 'woocommerce_api_product_tags_response', $product_tags, $terms, $fields, $this ) ); 
  785. } catch ( WC_API_Exception $e ) { 
  786. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  787.  
  788. /** 
  789. * Get the product tag for the given ID. 
  790. * 
  791. * @since 2.5.0 
  792. * @param string $id Product tag term ID 
  793. * @param string|null $fields Fields to limit response to 
  794. * @return array Product tag 
  795. */ 
  796. public function get_product_tag( $id, $fields = null ) { 
  797. try { 
  798. $id = absint( $id ); 
  799.  
  800. // Validate ID 
  801. if ( empty( $id ) ) { 
  802. throw new WC_API_Exception( 'woocommerce_api_invalid_product_tag_id', __( 'Invalid product tag ID', 'woocommerce' ), 400 ); 
  803.  
  804. // Permissions check 
  805. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  806. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_tags', __( 'You do not have permission to read product tags', 'woocommerce' ), 401 ); 
  807.  
  808. $term = get_term( $id, 'product_tag' ); 
  809.  
  810. if ( is_wp_error( $term ) || is_null( $term ) ) { 
  811. throw new WC_API_Exception( 'woocommerce_api_invalid_product_tag_id', __( 'A product tag with the provided ID could not be found', 'woocommerce' ), 404 ); 
  812.  
  813. $term_id = intval( $term->term_id ); 
  814.  
  815. $tag = array( 
  816. 'id' => $term_id,  
  817. 'name' => $term->name,  
  818. 'slug' => $term->slug,  
  819. 'description' => $term->description,  
  820. 'count' => intval( $term->count ),  
  821. ); 
  822.  
  823. return array( 'product_tag' => apply_filters( 'woocommerce_api_product_tag_response', $tag, $id, $fields, $term, $this ) ); 
  824. } catch ( WC_API_Exception $e ) { 
  825. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  826.  
  827. /** 
  828. * Create a new product tag. 
  829. * 
  830. * @since 2.5.0 
  831. * @param array $data Posted data 
  832. * @return array|WP_Error Product tag if succeed, otherwise WP_Error 
  833. * will be returned 
  834. */ 
  835. public function create_product_tag( $data ) { 
  836. try { 
  837. if ( ! isset( $data['product_tag'] ) ) { 
  838. throw new WC_API_Exception( 'woocommerce_api_missing_product_tag_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_tag' ), 400 ); 
  839.  
  840. // Check permissions 
  841. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  842. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_tag', __( 'You do not have permission to create product tags', 'woocommerce' ), 401 ); 
  843.  
  844. $defaults = array( 
  845. 'name' => '',  
  846. 'slug' => '',  
  847. 'description' => '',  
  848. ); 
  849.  
  850. $data = wp_parse_args( $data['product_tag'], $defaults ); 
  851. $data = apply_filters( 'woocommerce_api_create_product_tag_data', $data, $this ); 
  852.  
  853. $insert = wp_insert_term( $data['name'], 'product_tag', $data ); 
  854. if ( is_wp_error( $insert ) ) { 
  855. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_tag', $insert->get_error_message(), 400 ); 
  856. $id = $insert['term_id']; 
  857.  
  858. do_action( 'woocommerce_api_create_product_tag', $id, $data ); 
  859.  
  860. $this->server->send_status( 201 ); 
  861.  
  862. return $this->get_product_tag( $id ); 
  863. } catch ( WC_API_Exception $e ) { 
  864. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  865.  
  866. /** 
  867. * Edit a product tag. 
  868. * 
  869. * @since 2.5.0 
  870. * @param int $id Product tag term ID 
  871. * @param array $data Posted data 
  872. * @return array|WP_Error Product tag if succeed, otherwise WP_Error 
  873. * will be returned 
  874. */ 
  875. public function edit_product_tag( $id, $data ) { 
  876. try { 
  877. if ( ! isset( $data['product_tag'] ) ) { 
  878. throw new WC_API_Exception( 'woocommerce_api_missing_product_tag', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_tag' ), 400 ); 
  879.  
  880. $id = absint( $id ); 
  881. $data = $data['product_tag']; 
  882.  
  883. // Check permissions. 
  884. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  885. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_tag', __( 'You do not have permission to edit product tags', 'woocommerce' ), 401 ); 
  886.  
  887. $data = apply_filters( 'woocommerce_api_edit_product_tag_data', $data, $this ); 
  888. $tag = $this->get_product_tag( $id ); 
  889.  
  890. if ( is_wp_error( $tag ) ) { 
  891. return $tag; 
  892.  
  893. $update = wp_update_term( $id, 'product_tag', $data ); 
  894. if ( is_wp_error( $update ) ) { 
  895. throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_tag', __( 'Could not edit the tag', 'woocommerce' ), 400 ); 
  896.  
  897. do_action( 'woocommerce_api_edit_product_tag', $id, $data ); 
  898.  
  899. return $this->get_product_tag( $id ); 
  900. } catch ( WC_API_Exception $e ) { 
  901. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  902.  
  903. /** 
  904. * Delete a product tag. 
  905. * 
  906. * @since 2.5.0 
  907. * @param int $id Product tag term ID 
  908. * @return array|WP_Error Success message if succeed, otherwise WP_Error 
  909. * will be returned 
  910. */ 
  911. public function delete_product_tag( $id ) { 
  912. try { 
  913. // Check permissions 
  914. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  915. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_tag', __( 'You do not have permission to delete product tag', 'woocommerce' ), 401 ); 
  916.  
  917. $id = absint( $id ); 
  918. $deleted = wp_delete_term( $id, 'product_tag' ); 
  919. if ( ! $deleted || is_wp_error( $deleted ) ) { 
  920. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_tag', __( 'Could not delete the tag', 'woocommerce' ), 401 ); 
  921.  
  922. do_action( 'woocommerce_api_delete_product_tag', $id, $this ); 
  923.  
  924. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_tag' ) ); 
  925. } catch ( WC_API_Exception $e ) { 
  926. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  927.  
  928. /** 
  929. * Helper method to get product post objects 
  930. * 
  931. * @since 2.1 
  932. * @param array $args request arguments for filtering query 
  933. * @return WP_Query 
  934. */ 
  935. private function query_products( $args ) { 
  936.  
  937. // Set base query arguments 
  938. $query_args = array( 
  939. 'fields' => 'ids',  
  940. 'post_type' => 'product',  
  941. 'post_status' => 'publish',  
  942. 'meta_query' => array(),  
  943. ); 
  944.  
  945. // Taxonomy query to filter products by type, category, tag, shipping class, and 
  946. // attribute. 
  947. $tax_query = array(); 
  948.  
  949. // Map between taxonomy name and arg's key. 
  950. $taxonomies_arg_map = array( 
  951. 'product_type' => 'type',  
  952. 'product_cat' => 'category',  
  953. 'product_tag' => 'tag',  
  954. 'product_shipping_class' => 'shipping_class',  
  955. ); 
  956.  
  957. // Add attribute taxonomy names into the map. 
  958. foreach ( wc_get_attribute_taxonomy_names() as $attribute_name ) { 
  959. $taxonomies_arg_map[ $attribute_name ] = $attribute_name; 
  960.  
  961. // Set tax_query for each passed arg. 
  962. foreach ( $taxonomies_arg_map as $tax_name => $arg ) { 
  963. if ( ! empty( $args[ $arg ] ) ) { 
  964. $terms = explode( ', ', $args[ $arg ] ); 
  965.  
  966. $tax_query[] = array( 
  967. 'taxonomy' => $tax_name,  
  968. 'field' => 'slug',  
  969. 'terms' => $terms,  
  970. ); 
  971.  
  972. unset( $args[ $arg ] ); 
  973.  
  974. if ( ! empty( $tax_query ) ) { 
  975. $query_args['tax_query'] = $tax_query; 
  976.  
  977. // Filter by specific sku 
  978. if ( ! empty( $args['sku'] ) ) { 
  979. if ( ! is_array( $query_args['meta_query'] ) ) { 
  980. $query_args['meta_query'] = array(); 
  981.  
  982. $query_args['meta_query'][] = array( 
  983. 'key' => '_sku',  
  984. 'value' => $args['sku'],  
  985. 'compare' => '=',  
  986. ); 
  987.  
  988. $query_args['post_type'] = array( 'product', 'product_variation' ); 
  989.  
  990. $query_args = $this->merge_query_args( $query_args, $args ); 
  991.  
  992. return new WP_Query( $query_args ); 
  993.  
  994. /** 
  995. * Get standard product data that applies to every product type 
  996. * 
  997. * @since 2.1 
  998. * @param WC_Product|int $product 
  999. * @return WC_Product 
  1000. */ 
  1001. private function get_product_data( $product ) { 
  1002. if ( is_numeric( $product ) ) { 
  1003. $product = wc_get_product( $product ); 
  1004.  
  1005. return array( 
  1006. 'title' => $product->get_name(),  
  1007. 'id' => $product->get_id(),  
  1008. 'created_at' => $this->server->format_datetime( $product->get_date_created(), false, true ),  
  1009. 'updated_at' => $this->server->format_datetime( $product->get_date_modified(), false, true ),  
  1010. 'type' => $product->get_type(),  
  1011. 'status' => $product->get_status(),  
  1012. 'downloadable' => $product->is_downloadable(),  
  1013. 'virtual' => $product->is_virtual(),  
  1014. 'permalink' => $product->get_permalink(),  
  1015. 'sku' => $product->get_sku(),  
  1016. 'price' => $product->get_price(),  
  1017. 'regular_price' => $product->get_regular_price(),  
  1018. 'sale_price' => $product->get_sale_price() ? $product->get_sale_price() : null,  
  1019. 'price_html' => $product->get_price_html(),  
  1020. 'taxable' => $product->is_taxable(),  
  1021. 'tax_status' => $product->get_tax_status(),  
  1022. 'tax_class' => $product->get_tax_class(),  
  1023. 'managing_stock' => $product->managing_stock(),  
  1024. 'stock_quantity' => $product->get_stock_quantity(),  
  1025. 'in_stock' => $product->is_in_stock(),  
  1026. 'backorders_allowed' => $product->backorders_allowed(),  
  1027. 'backordered' => $product->is_on_backorder(),  
  1028. 'sold_individually' => $product->is_sold_individually(),  
  1029. 'purchaseable' => $product->is_purchasable(),  
  1030. 'featured' => $product->is_featured(),  
  1031. 'visible' => $product->is_visible(),  
  1032. 'catalog_visibility' => $product->get_catalog_visibility(),  
  1033. 'on_sale' => $product->is_on_sale(),  
  1034. 'product_url' => $product->is_type( 'external' ) ? $product->get_product_url() : '',  
  1035. 'button_text' => $product->is_type( 'external' ) ? $product->get_button_text() : '',  
  1036. 'weight' => $product->get_weight() ? $product->get_weight() : null,  
  1037. 'dimensions' => array( 
  1038. 'length' => $product->get_length(),  
  1039. 'width' => $product->get_width(),  
  1040. 'height' => $product->get_height(),  
  1041. 'unit' => get_option( 'woocommerce_dimension_unit' ),  
  1042. ),  
  1043. 'shipping_required' => $product->needs_shipping(),  
  1044. 'shipping_taxable' => $product->is_shipping_taxable(),  
  1045. 'shipping_class' => $product->get_shipping_class(),  
  1046. 'shipping_class_id' => ( 0 !== $product->get_shipping_class_id() ) ? $product->get_shipping_class_id() : null,  
  1047. 'description' => wpautop( do_shortcode( $product->get_description() ) ),  
  1048. 'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),  
  1049. 'reviews_allowed' => $product->get_reviews_allowed(),  
  1050. 'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),  
  1051. 'rating_count' => $product->get_rating_count(),  
  1052. 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),  
  1053. 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),  
  1054. 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),  
  1055. 'parent_id' => $product->get_parent_id(),  
  1056. 'categories' => wc_get_object_terms( $product->get_id(), 'product_cat', 'name' ),  
  1057. 'tags' => wc_get_object_terms( $product->get_id(), 'product_tag', 'name' ),  
  1058. 'images' => $this->get_images( $product ),  
  1059. 'featured_src' => wp_get_attachment_url( get_post_thumbnail_id( $product->get_id() ) ),  
  1060. 'attributes' => $this->get_attributes( $product ),  
  1061. 'downloads' => $this->get_downloads( $product ),  
  1062. 'download_limit' => $product->get_download_limit(),  
  1063. 'download_expiry' => $product->get_download_expiry(),  
  1064. 'download_type' => 'standard',  
  1065. 'purchase_note' => wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ),  
  1066. 'total_sales' => $product->get_total_sales(),  
  1067. 'variations' => array(),  
  1068. 'parent' => array(),  
  1069. 'grouped_products' => array(),  
  1070. 'menu_order' => $this->get_product_menu_order( $product ),  
  1071. ); 
  1072.  
  1073. /** 
  1074. * Get product menu order. 
  1075. * 
  1076. * @since 2.5.3 
  1077. * @param WC_Product $product 
  1078. * @return int 
  1079. */ 
  1080. private function get_product_menu_order( $product ) { 
  1081. $menu_order = $product->get_menu_order(); 
  1082.  
  1083. return apply_filters( 'woocommerce_api_product_menu_order', $menu_order, $product ); 
  1084.  
  1085. /** 
  1086. * Get an individual variation's data. 
  1087. * 
  1088. * @since 2.1 
  1089. * @param WC_Product $product 
  1090. * @return array 
  1091. */ 
  1092. private function get_variation_data( $product ) { 
  1093. $variations = array(); 
  1094.  
  1095. foreach ( $product->get_children() as $child_id ) { 
  1096. $variation = wc_get_product( $child_id ); 
  1097.  
  1098. if ( ! $variation || ! $variation->exists() ) { 
  1099. continue; 
  1100.  
  1101. $variations[] = array( 
  1102. 'id' => $variation->get_id(),  
  1103. 'created_at' => $this->server->format_datetime( $variation->get_date_created(), false, true ),  
  1104. 'updated_at' => $this->server->format_datetime( $variation->get_date_modified(), false, true ),  
  1105. 'downloadable' => $variation->is_downloadable(),  
  1106. 'virtual' => $variation->is_virtual(),  
  1107. 'permalink' => $variation->get_permalink(),  
  1108. 'sku' => $variation->get_sku(),  
  1109. 'price' => $variation->get_price(),  
  1110. 'regular_price' => $variation->get_regular_price(),  
  1111. 'sale_price' => $variation->get_sale_price() ? $variation->get_sale_price() : null,  
  1112. 'taxable' => $variation->is_taxable(),  
  1113. 'tax_status' => $variation->get_tax_status(),  
  1114. 'tax_class' => $variation->get_tax_class(),  
  1115. 'managing_stock' => $variation->managing_stock(),  
  1116. 'stock_quantity' => $variation->get_stock_quantity(),  
  1117. 'in_stock' => $variation->is_in_stock(),  
  1118. 'backorders_allowed' => $variation->backorders_allowed(),  
  1119. 'backordered' => $variation->is_on_backorder(),  
  1120. 'purchaseable' => $variation->is_purchasable(),  
  1121. 'visible' => $variation->variation_is_visible(),  
  1122. 'on_sale' => $variation->is_on_sale(),  
  1123. 'weight' => $variation->get_weight() ? $variation->get_weight() : null,  
  1124. 'dimensions' => array( 
  1125. 'length' => $variation->get_length(),  
  1126. 'width' => $variation->get_width(),  
  1127. 'height' => $variation->get_height(),  
  1128. 'unit' => get_option( 'woocommerce_dimension_unit' ),  
  1129. ),  
  1130. 'shipping_class' => $variation->get_shipping_class(),  
  1131. 'shipping_class_id' => ( 0 !== $variation->get_shipping_class_id() ) ? $variation->get_shipping_class_id() : null,  
  1132. 'image' => $this->get_images( $variation ),  
  1133. 'attributes' => $this->get_attributes( $variation ),  
  1134. 'downloads' => $this->get_downloads( $variation ),  
  1135. 'download_limit' => (int) $product->get_download_limit(),  
  1136. 'download_expiry' => (int) $product->get_download_expiry(),  
  1137. ); 
  1138.  
  1139. return $variations; 
  1140.  
  1141. /** 
  1142. * Get grouped products data 
  1143. * 
  1144. * @since 2.5.0 
  1145. * @param WC_Product $product 
  1146. * 
  1147. * @return array 
  1148. */ 
  1149. private function get_grouped_products_data( $product ) { 
  1150. $products = array(); 
  1151.  
  1152. foreach ( $product->get_children() as $child_id ) { 
  1153. $_product = wc_get_product( $child_id ); 
  1154.  
  1155. if ( ! $_product || ! $_product->exists() ) { 
  1156. continue; 
  1157.  
  1158. $products[] = $this->get_product_data( $_product ); 
  1159.  
  1160.  
  1161. return $products; 
  1162.  
  1163. /** 
  1164. * Save default attributes. 
  1165. * 
  1166. * @since 3.0.0 
  1167. * 
  1168. * @param WC_Product $product 
  1169. * @param WP_REST_Request $request 
  1170. * @return WC_Product 
  1171. */ 
  1172. protected function save_default_attributes( $product, $request ) { 
  1173. // Update default attributes options setting. 
  1174. if ( isset( $request['default_attribute'] ) ) { 
  1175. $request['default_attributes'] = $request['default_attribute']; 
  1176.  
  1177. if ( isset( $request['default_attributes'] ) && is_array( $request['default_attributes'] ) ) { 
  1178. $attributes = $product->get_attributes(); 
  1179. $default_attributes = array(); 
  1180.  
  1181. foreach ( $request['default_attributes'] as $default_attr_key => $default_attr ) { 
  1182. if ( ! isset( $default_attr['name'] ) ) { 
  1183. continue; 
  1184.  
  1185. $taxonomy = sanitize_title( $default_attr['name'] ); 
  1186.  
  1187. if ( isset( $default_attr['slug'] ) ) { 
  1188. $taxonomy = $this->get_attribute_taxonomy_by_slug( $default_attr['slug'] ); 
  1189.  
  1190. if ( isset( $attributes[ $taxonomy ] ) ) { 
  1191. $_attribute = $attributes[ $taxonomy ]; 
  1192.  
  1193. if ( $_attribute['is_variation'] ) { 
  1194. $value = ''; 
  1195.  
  1196. if ( isset( $default_attr['option'] ) ) { 
  1197. if ( $_attribute['is_taxonomy'] ) { 
  1198. // Don't use wc_clean as it destroys sanitized characters 
  1199. $value = sanitize_title( trim( stripslashes( $default_attr['option'] ) ) ); 
  1200. } else { 
  1201. $value = wc_clean( trim( stripslashes( $default_attr['option'] ) ) ); 
  1202.  
  1203. if ( $value ) { 
  1204. $default_attributes[ $taxonomy ] = $value; 
  1205.  
  1206. $product->set_default_attributes( $default_attributes ); 
  1207.  
  1208. return $product; 
  1209.  
  1210. /** 
  1211. * Save product meta. 
  1212. * 
  1213. * @since 2.2 
  1214. * @param WC_Product $product 
  1215. * @param array $data 
  1216. * @return WC_Product 
  1217. * @throws WC_API_Exception 
  1218. */ 
  1219. protected function save_product_meta( $product, $data ) { 
  1220. global $wpdb; 
  1221.  
  1222. // Virtual. 
  1223. if ( isset( $data['virtual'] ) ) { 
  1224. $product->set_virtual( $data['virtual'] ); 
  1225.  
  1226. // Tax status. 
  1227. if ( isset( $data['tax_status'] ) ) { 
  1228. $product->set_tax_status( wc_clean( $data['tax_status'] ) ); 
  1229.  
  1230. // Tax Class. 
  1231. if ( isset( $data['tax_class'] ) ) { 
  1232. $product->set_tax_class( wc_clean( $data['tax_class'] ) ); 
  1233.  
  1234. // Catalog Visibility. 
  1235. if ( isset( $data['catalog_visibility'] ) ) { 
  1236. $product->set_catalog_visibility( wc_clean( $data['catalog_visibility'] ) ); 
  1237.  
  1238. // Purchase Note. 
  1239. if ( isset( $data['purchase_note'] ) ) { 
  1240. $product->set_purchase_note( wc_clean( $data['purchase_note'] ) ); 
  1241.  
  1242. // Featured Product. 
  1243. if ( isset( $data['featured'] ) ) { 
  1244. $product->set_featured( $data['featured'] ); 
  1245.  
  1246. // Shipping data. 
  1247. $product = $this->save_product_shipping_data( $product, $data ); 
  1248.  
  1249. // SKU. 
  1250. if ( isset( $data['sku'] ) ) { 
  1251. $sku = $product->get_sku(); 
  1252. $new_sku = wc_clean( $data['sku'] ); 
  1253.  
  1254. if ( '' == $new_sku ) { 
  1255. $product->set_sku( '' ); 
  1256. } elseif ( $new_sku !== $sku ) { 
  1257. if ( ! empty( $new_sku ) ) { 
  1258. $unique_sku = wc_product_has_unique_sku( $product->get_id(), $new_sku ); 
  1259. if ( ! $unique_sku ) { 
  1260. throw new WC_API_Exception( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product.', 'woocommerce' ), 400 ); 
  1261. } else { 
  1262. $product->set_sku( $new_sku ); 
  1263. } else { 
  1264. $product->set_sku( '' ); 
  1265.  
  1266. // Attributes. 
  1267. if ( isset( $data['attributes'] ) ) { 
  1268. $attributes = array(); 
  1269.  
  1270. foreach ( $data['attributes'] as $attribute ) { 
  1271. $is_taxonomy = 0; 
  1272. $taxonomy = 0; 
  1273.  
  1274. if ( ! isset( $attribute['name'] ) ) { 
  1275. continue; 
  1276.  
  1277. $attribute_slug = sanitize_title( $attribute['name'] ); 
  1278.  
  1279. if ( isset( $attribute['slug'] ) ) { 
  1280. $taxonomy = $this->get_attribute_taxonomy_by_slug( $attribute['slug'] ); 
  1281. $attribute_slug = sanitize_title( $attribute['slug'] ); 
  1282.  
  1283. if ( $taxonomy ) { 
  1284. $is_taxonomy = 1; 
  1285.  
  1286. if ( $is_taxonomy ) { 
  1287.  
  1288. $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute['name'] ); 
  1289.  
  1290. if ( isset( $attribute['options'] ) ) { 
  1291. $options = $attribute['options']; 
  1292.  
  1293. if ( ! is_array( $attribute['options'] ) ) { 
  1294. // Text based attributes - Posted values are term names. 
  1295. $options = explode( WC_DELIMITER, $options ); 
  1296.  
  1297. $values = array_map( 'wc_sanitize_term_text_based', $options ); 
  1298. $values = array_filter( $values, 'strlen' ); 
  1299. } else { 
  1300. $values = array(); 
  1301.  
  1302. // Update post terms 
  1303. if ( taxonomy_exists( $taxonomy ) ) { 
  1304. wp_set_object_terms( $product->get_id(), $values, $taxonomy ); 
  1305.  
  1306. if ( ! empty( $values ) ) { 
  1307. // Add attribute to array, but don't set values. 
  1308. $attribute_object = new WC_Product_Attribute(); 
  1309. $attribute_object->set_id( $attribute_id ); 
  1310. $attribute_object->set_name( $taxonomy ); 
  1311. $attribute_object->set_options( $values ); 
  1312. $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 ); 
  1313. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); 
  1314. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); 
  1315. $attributes[] = $attribute_object; 
  1316. } elseif ( isset( $attribute['options'] ) ) { 
  1317. // Array based. 
  1318. if ( is_array( $attribute['options'] ) ) { 
  1319. $values = $attribute['options']; 
  1320.  
  1321. // Text based, separate by pipe. 
  1322. } else { 
  1323. $values = array_map( 'wc_clean', explode( WC_DELIMITER, $attribute['options'] ) ); 
  1324.  
  1325. // Custom attribute - Add attribute to array and set the values. 
  1326. $attribute_object = new WC_Product_Attribute(); 
  1327. $attribute_object->set_name( $attribute['name'] ); 
  1328. $attribute_object->set_options( $values ); 
  1329. $attribute_object->set_position( isset( $attribute['position'] ) ? absint( $attribute['position'] ) : 0 ); 
  1330. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); 
  1331. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); 
  1332. $attributes[] = $attribute_object; 
  1333.  
  1334. uasort( $attributes, 'wc_product_attribute_uasort_comparison' ); 
  1335.  
  1336. $product->set_attributes( $attributes ); 
  1337.  
  1338. // Sales and prices. 
  1339. if ( in_array( $product->get_type(), array( 'variable', 'grouped' ) ) ) { 
  1340.  
  1341. // Variable and grouped products have no prices. 
  1342. $product->set_regular_price( '' ); 
  1343. $product->set_sale_price( '' ); 
  1344. $product->set_date_on_sale_to( '' ); 
  1345. $product->set_date_on_sale_from( '' ); 
  1346. $product->set_price( '' ); 
  1347.  
  1348. } else { 
  1349.  
  1350. // Regular Price 
  1351. if ( isset( $data['regular_price'] ) ) { 
  1352. $regular_price = ( '' === $data['regular_price'] ) ? '' : $data['regular_price']; 
  1353. } else { 
  1354. $regular_price = $product->get_regular_price(); 
  1355.  
  1356. // Sale Price 
  1357. if ( isset( $data['sale_price'] ) ) { 
  1358. $sale_price = ( '' === $data['sale_price'] ) ? '' : $data['sale_price']; 
  1359. } else { 
  1360. $sale_price = $product->get_sale_price(); 
  1361.  
  1362. $product->set_regular_price( $regular_price ); 
  1363. $product->set_sale_price( $sale_price ); 
  1364.  
  1365. if ( isset( $data['sale_price_dates_from'] ) ) { 
  1366. $date_from = $data['sale_price_dates_from']; 
  1367. } else { 
  1368. $date_from = $product->get_date_on_sale_from() ? date( 'Y-m-d', $product->get_date_on_sale_from()->getTimestamp() ) : ''; 
  1369.  
  1370. if ( isset( $data['sale_price_dates_to'] ) ) { 
  1371. $date_to = $data['sale_price_dates_to']; 
  1372. } else { 
  1373. $date_to = $product->get_date_on_sale_to() ? date( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() ) : ''; 
  1374.  
  1375. if ( $date_to && ! $date_from ) { 
  1376. $date_from = strtotime( 'NOW', current_time( 'timestamp', true ) ); 
  1377.  
  1378. $product->set_date_on_sale_to( $date_to ); 
  1379. $product->set_date_on_sale_from( $date_from ); 
  1380. if ( $product->is_on_sale() ) { 
  1381. $product->set_price( $product->get_sale_price() ); 
  1382. } else { 
  1383. $product->set_price( $product->get_regular_price() ); 
  1384.  
  1385. // Product parent ID for groups. 
  1386. if ( isset( $data['parent_id'] ) ) { 
  1387. $product->set_parent_id( absint( $data['parent_id'] ) ); 
  1388.  
  1389. // Sold Individually. 
  1390. if ( isset( $data['sold_individually'] ) ) { 
  1391. $product->set_sold_individually( true === $data['sold_individually'] ? 'yes' : '' ); 
  1392.  
  1393. // Stock status. 
  1394. if ( isset( $data['in_stock'] ) ) { 
  1395. $stock_status = ( true === $data['in_stock'] ) ? 'instock' : 'outofstock'; 
  1396. } else { 
  1397. $stock_status = $product->get_stock_status(); 
  1398.  
  1399. if ( '' === $stock_status ) { 
  1400. $stock_status = 'instock'; 
  1401.  
  1402. // Stock Data. 
  1403. if ( 'yes' == get_option( 'woocommerce_manage_stock' ) ) { 
  1404. // Manage stock. 
  1405. if ( isset( $data['managing_stock'] ) ) { 
  1406. $managing_stock = ( true === $data['managing_stock'] ) ? 'yes' : 'no'; 
  1407. $product->set_manage_stock( $managing_stock ); 
  1408. } else { 
  1409. $managing_stock = $product->get_manage_stock() ? 'yes' : 'no'; 
  1410.  
  1411. // Backorders. 
  1412. if ( isset( $data['backorders'] ) ) { 
  1413. if ( 'notify' === $data['backorders'] ) { 
  1414. $backorders = 'notify'; 
  1415. } else { 
  1416. $backorders = ( true === $data['backorders'] ) ? 'yes' : 'no'; 
  1417.  
  1418. $product->set_backorders( $backorders ); 
  1419. } else { 
  1420. $backorders = $product->get_backorders(); 
  1421.  
  1422. if ( $product->is_type( 'grouped' ) ) { 
  1423. $product->set_manage_stock( 'no' ); 
  1424. $product->set_backorders( 'no' ); 
  1425. $product->set_stock_quantity( '' ); 
  1426. $product->set_stock_status( $stock_status ); 
  1427. } elseif ( $product->is_type( 'external' ) ) { 
  1428. $product->set_manage_stock( 'no' ); 
  1429. $product->set_backorders( 'no' ); 
  1430. $product->set_stock_quantity( '' ); 
  1431. $product->set_stock_status( 'instock' ); 
  1432. } elseif ( 'yes' == $managing_stock ) { 
  1433. $product->set_backorders( $backorders ); 
  1434.  
  1435. // Stock status is always determined by children so sync later. 
  1436. if ( ! $product->is_type( 'variable' ) ) { 
  1437. $product->set_stock_status( $stock_status ); 
  1438.  
  1439. // Stock quantity. 
  1440. if ( isset( $data['stock_quantity'] ) ) { 
  1441. $product->set_stock_quantity( wc_stock_amount( $data['stock_quantity'] ) ); 
  1442. } elseif ( isset( $data['inventory_delta'] ) ) { 
  1443. $stock_quantity = wc_stock_amount( $product->get_stock_quantity() ); 
  1444. $stock_quantity += wc_stock_amount( $data['inventory_delta'] ); 
  1445. $product->set_stock_quantity( wc_stock_amount( $stock_quantity ) ); 
  1446. } else { 
  1447. // Don't manage stock. 
  1448. $product->set_manage_stock( 'no' ); 
  1449. $product->set_backorders( $backorders ); 
  1450. $product->set_stock_quantity( '' ); 
  1451. $product->set_stock_status( $stock_status ); 
  1452. } elseif ( ! $product->is_type( 'variable' ) ) { 
  1453. $product->set_stock_status( $stock_status ); 
  1454.  
  1455. // Upsells. 
  1456. if ( isset( $data['upsell_ids'] ) ) { 
  1457. $upsells = array(); 
  1458. $ids = $data['upsell_ids']; 
  1459.  
  1460. if ( ! empty( $ids ) ) { 
  1461. foreach ( $ids as $id ) { 
  1462. if ( $id && $id > 0 ) { 
  1463. $upsells[] = $id; 
  1464.  
  1465. $product->set_upsell_ids( $upsells ); 
  1466. } else { 
  1467. $product->set_upsell_ids( array() ); 
  1468.  
  1469. // Cross sells. 
  1470. if ( isset( $data['cross_sell_ids'] ) ) { 
  1471. $crosssells = array(); 
  1472. $ids = $data['cross_sell_ids']; 
  1473.  
  1474. if ( ! empty( $ids ) ) { 
  1475. foreach ( $ids as $id ) { 
  1476. if ( $id && $id > 0 ) { 
  1477. $crosssells[] = $id; 
  1478.  
  1479. $product->set_cross_sell_ids( $crosssells ); 
  1480. } else { 
  1481. $product->set_cross_sell_ids( array() ); 
  1482.  
  1483. // Product categories. 
  1484. if ( isset( $data['categories'] ) && is_array( $data['categories'] ) ) { 
  1485. $product->set_category_ids( $data['categories'] ); 
  1486.  
  1487. // Product tags. 
  1488. if ( isset( $data['tags'] ) && is_array( $data['tags'] ) ) { 
  1489. $product->set_tag_ids( $data['tags'] ); 
  1490.  
  1491. // Downloadable. 
  1492. if ( isset( $data['downloadable'] ) ) { 
  1493. $is_downloadable = ( true === $data['downloadable'] ) ? 'yes' : 'no'; 
  1494. $product->set_downloadable( $is_downloadable ); 
  1495. } else { 
  1496. $is_downloadable = $product->get_downloadable() ? 'yes' : 'no'; 
  1497.  
  1498. // Downloadable options. 
  1499. if ( 'yes' == $is_downloadable ) { 
  1500.  
  1501. // Downloadable files. 
  1502. if ( isset( $data['downloads'] ) && is_array( $data['downloads'] ) ) { 
  1503. $product = $this->save_downloadable_files( $product, $data['downloads'] ); 
  1504.  
  1505. // Download limit. 
  1506. if ( isset( $data['download_limit'] ) ) { 
  1507. $product->set_download_limit( $data['download_limit'] ); 
  1508.  
  1509. // Download expiry. 
  1510. if ( isset( $data['download_expiry'] ) ) { 
  1511. $product->set_download_expiry( $data['download_expiry'] ); 
  1512.  
  1513. // Product url. 
  1514. if ( $product->is_type( 'external' ) ) { 
  1515. if ( isset( $data['product_url'] ) ) { 
  1516. $product->set_product_url( $data['product_url'] ); 
  1517.  
  1518. if ( isset( $data['button_text'] ) ) { 
  1519. $product->set_button_text( $data['button_text'] ); 
  1520.  
  1521. // Reviews allowed. 
  1522. if ( isset( $data['reviews_allowed'] ) ) { 
  1523. $product->set_reviews_allowed( $data['reviews_allowed'] ); 
  1524.  
  1525. // Save default attributes for variable products. 
  1526. if ( $product->is_type( 'variable' ) ) { 
  1527. $product = $this->save_default_attributes( $product, $data ); 
  1528.  
  1529. // Do action for product type 
  1530. do_action( 'woocommerce_api_process_product_meta_' . $product->get_type(), $product->get_id(), $data ); 
  1531.  
  1532. return $product; 
  1533.  
  1534. /** 
  1535. * Save variations. 
  1536. * 
  1537. * @since 2.2 
  1538. * @param WC_Product $product 
  1539. * @param array $request 
  1540. * @return WC_Product 
  1541. * @throws WC_API_Exception 
  1542. */ 
  1543. protected function save_variations( $product, $request ) { 
  1544. global $wpdb; 
  1545.  
  1546. $id = $product->get_id(); 
  1547. $variations = $request['variations']; 
  1548. $attributes = $product->get_attributes(); 
  1549.  
  1550. foreach ( $variations as $menu_order => $data ) { 
  1551. $variation_id = isset( $data['id'] ) ? absint( $data['id'] ) : 0; 
  1552. $variation = new WC_Product_Variation( $variation_id ); 
  1553.  
  1554. // Create initial name and status. 
  1555. if ( ! $variation->get_slug() ) { 
  1556. /** translators: 1: variation id 2: product name */ 
  1557. $variation->set_name( sprintf( __( 'Variation #%1$s of %2$s', 'woocommerce' ), $variation->get_id(), $product->get_name() ) ); 
  1558. $variation->set_status( isset( $data['visible'] ) && false === $data['visible'] ? 'private' : 'publish' ); 
  1559.  
  1560. // Parent ID. 
  1561. $variation->set_parent_id( $product->get_id() ); 
  1562.  
  1563. // Menu order. 
  1564. $variation->set_menu_order( $menu_order ); 
  1565.  
  1566. // Status. 
  1567. if ( isset( $data['visible'] ) ) { 
  1568. $variation->set_status( false === $data['visible'] ? 'private' : 'publish' ); 
  1569.  
  1570. // SKU. 
  1571. if ( isset( $data['sku'] ) ) { 
  1572. $variation->set_sku( wc_clean( $data['sku'] ) ); 
  1573.  
  1574. // Thumbnail. 
  1575. if ( isset( $data['image'] ) && is_array( $data['image'] ) ) { 
  1576. $image = current( $data['image'] ); 
  1577. if ( is_array( $image ) ) { 
  1578. $image['position'] = 0; 
  1579.  
  1580. $variation = $this->save_product_images( $variation, array( $image ) ); 
  1581.  
  1582. // Virtual variation. 
  1583. if ( isset( $data['virtual'] ) ) { 
  1584. $variation->set_virtual( $data['virtual'] ); 
  1585.  
  1586. // Downloadable variation. 
  1587. if ( isset( $data['downloadable'] ) ) { 
  1588. $is_downloadable = $data['downloadable']; 
  1589. $variation->set_downloadable( $is_downloadable ); 
  1590. } else { 
  1591. $is_downloadable = $variation->get_downloadable(); 
  1592.  
  1593. // Downloads. 
  1594. if ( $is_downloadable ) { 
  1595. // Downloadable files. 
  1596. if ( isset( $data['downloads'] ) && is_array( $data['downloads'] ) ) { 
  1597. $variation = $this->save_downloadable_files( $variation, $data['downloads'] ); 
  1598.  
  1599. // Download limit. 
  1600. if ( isset( $data['download_limit'] ) ) { 
  1601. $variation->set_download_limit( $data['download_limit'] ); 
  1602.  
  1603. // Download expiry. 
  1604. if ( isset( $data['download_expiry'] ) ) { 
  1605. $variation->set_download_expiry( $data['download_expiry'] ); 
  1606.  
  1607. // Shipping data. 
  1608. $variation = $this->save_product_shipping_data( $variation, $data ); 
  1609.  
  1610. // Stock handling. 
  1611. $manage_stock = (bool) $variation->get_manage_stock(); 
  1612. if ( isset( $data['managing_stock'] ) ) { 
  1613. $manage_stock = $data['managing_stock']; 
  1614. $variation->set_manage_stock( $manage_stock ); 
  1615.  
  1616. $stock_status = $variation->get_stock_status(); 
  1617. if ( isset( $data['in_stock'] ) ) { 
  1618. $stock_status = true === $data['in_stock'] ? 'instock' : 'outofstock'; 
  1619. $variation->set_stock_status( $stock_status ); 
  1620.  
  1621. $backorders = $variation->get_backorders(); 
  1622. if ( isset( $data['backorders'] ) ) { 
  1623. $backorders = $data['backorders']; 
  1624. $variation->set_backorders( $backorders ); 
  1625.  
  1626. if ( $manage_stock ) { 
  1627. if ( isset( $data['stock_quantity'] ) ) { 
  1628. $variation->set_stock_quantity( $data['stock_quantity'] ); 
  1629. } elseif ( isset( $data['inventory_delta'] ) ) { 
  1630. $stock_quantity = wc_stock_amount( $variation->get_stock_quantity() ); 
  1631. $stock_quantity += wc_stock_amount( $data['inventory_delta'] ); 
  1632. $variation->set_stock_quantity( $stock_quantity ); 
  1633. } else { 
  1634. $variation->set_backorders( 'no' ); 
  1635. $variation->set_stock_quantity( '' ); 
  1636.  
  1637. // Regular Price
  1638. if ( isset( $data['regular_price'] ) ) { 
  1639. $variation->set_regular_price( $data['regular_price'] ); 
  1640.  
  1641. // Sale Price
  1642. if ( isset( $data['sale_price'] ) ) { 
  1643. $variation->set_sale_price( $data['sale_price'] ); 
  1644.  
  1645. if ( isset( $data['sale_price_dates_from'] ) ) { 
  1646. $variation->set_date_on_sale_from( $data['sale_price_dates_from'] ); 
  1647.  
  1648. if ( isset( $data['sale_price_dates_to'] ) ) { 
  1649. $variation->set_date_on_sale_to( $data['sale_price_dates_to'] ); 
  1650.  
  1651. // Tax class. 
  1652. if ( isset( $data['tax_class'] ) ) { 
  1653. $variation->set_tax_class( $data['tax_class'] ); 
  1654.  
  1655. // Description. 
  1656. if ( isset( $data['description'] ) ) { 
  1657. $variation->set_description( wp_kses_post( $data['description'] ) ); 
  1658.  
  1659. // Update taxonomies. 
  1660. if ( isset( $data['attributes'] ) ) { 
  1661. $_attributes = array(); 
  1662.  
  1663. foreach ( $data['attributes'] as $attribute_key => $attribute ) { 
  1664. if ( ! isset( $attribute['name'] ) ) { 
  1665. continue; 
  1666.  
  1667. $taxonomy = 0; 
  1668. $_attribute = array(); 
  1669.  
  1670. if ( isset( $attribute['slug'] ) ) { 
  1671. $taxonomy = $this->get_attribute_taxonomy_by_slug( $attribute['slug'] ); 
  1672.  
  1673. if ( ! $taxonomy ) { 
  1674. $taxonomy = sanitize_title( $attribute['name'] ); 
  1675.  
  1676. if ( isset( $attributes[ $taxonomy ] ) ) { 
  1677. $_attribute = $attributes[ $taxonomy ]; 
  1678.  
  1679. if ( isset( $_attribute['is_variation'] ) && $_attribute['is_variation'] ) { 
  1680. $_attribute_key = sanitize_title( $_attribute['name'] ); 
  1681.  
  1682. if ( isset( $_attribute['is_taxonomy'] ) && $_attribute['is_taxonomy'] ) { 
  1683. // Don't use wc_clean as it destroys sanitized characters
  1684. $_attribute_value = isset( $attribute['option'] ) ? sanitize_title( stripslashes( $attribute['option'] ) ) : ''; 
  1685. } else { 
  1686. $_attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; 
  1687.  
  1688. $_attributes[ $_attribute_key ] = $_attribute_value; 
  1689.  
  1690. $variation->set_attributes( $_attributes ); 
  1691.  
  1692. $variation->save(); 
  1693.  
  1694. do_action( 'woocommerce_api_save_product_variation', $variation_id, $menu_order, $variation ); 
  1695.  
  1696. return true; 
  1697.  
  1698. /** 
  1699. * Save product shipping data 
  1700. * 
  1701. * @since 2.2 
  1702. * @param WC_Product $product 
  1703. * @param array $data 
  1704. * @return WC_Product 
  1705. */ 
  1706. private function save_product_shipping_data( $product, $data ) { 
  1707. if ( isset( $data['weight'] ) ) { 
  1708. $product->set_weight( '' === $data['weight'] ? '' : wc_format_decimal( $data['weight'] ) ); 
  1709.  
  1710. // Product dimensions 
  1711. if ( isset( $data['dimensions'] ) ) { 
  1712. // Height 
  1713. if ( isset( $data['dimensions']['height'] ) ) { 
  1714. $product->set_height( '' === $data['dimensions']['height'] ? '' : wc_format_decimal( $data['dimensions']['height'] ) ); 
  1715.  
  1716. // Width 
  1717. if ( isset( $data['dimensions']['width'] ) ) { 
  1718. $product->set_width( '' === $data['dimensions']['width'] ? '' : wc_format_decimal( $data['dimensions']['width'] ) ); 
  1719.  
  1720. // Length 
  1721. if ( isset( $data['dimensions']['length'] ) ) { 
  1722. $product->set_length( '' === $data['dimensions']['length'] ? '' : wc_format_decimal( $data['dimensions']['length'] ) ); 
  1723.  
  1724. // Virtual 
  1725. if ( isset( $data['virtual'] ) ) { 
  1726. $virtual = ( true === $data['virtual'] ) ? 'yes' : 'no'; 
  1727.  
  1728. if ( 'yes' == $virtual ) { 
  1729. $product->set_weight( '' ); 
  1730. $product->set_height( '' ); 
  1731. $product->set_length( '' ); 
  1732. $product->set_width( '' ); 
  1733.  
  1734. // Shipping class 
  1735. if ( isset( $data['shipping_class'] ) ) { 
  1736. $data_store = $product->get_data_store(); 
  1737. $shipping_class_id = $data_store->get_shipping_class_id_by_slug( wc_clean( $data['shipping_class'] ) ); 
  1738. if ( $shipping_class_id ) { 
  1739. $product->set_shipping_class_id( $shipping_class_id ); 
  1740.  
  1741. return $product; 
  1742.  
  1743. /** 
  1744. * Save downloadable files 
  1745. * 
  1746. * @since 2.2 
  1747. * @param WC_Product $product 
  1748. * @param array $downloads 
  1749. * @param int $deprecated Deprecated since 3.0. 
  1750. * @return WC_Product 
  1751. */ 
  1752. private function save_downloadable_files( $product, $downloads, $deprecated = 0 ) { 
  1753. if ( $deprecated ) { 
  1754. wc_deprecated_argument( 'variation_id', '3.0', 'save_downloadable_files() does not require a variation_id anymore.' ); 
  1755.  
  1756. $files = array(); 
  1757. foreach ( $downloads as $key => $file ) { 
  1758. if ( isset( $file['url'] ) ) { 
  1759. $file['file'] = $file['url']; 
  1760.  
  1761. if ( empty( $file['file'] ) ) { 
  1762. continue; 
  1763.  
  1764. $download = new WC_Product_Download(); 
  1765. $download->set_id( $key ); 
  1766. $download->set_name( $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['file'] ) ); 
  1767. $download->set_file( apply_filters( 'woocommerce_file_download_path', $file['file'], $product, $key ) ); 
  1768. $files[] = $download; 
  1769. $product->set_downloads( $files ); 
  1770.  
  1771. return $product; 
  1772.  
  1773. /** 
  1774. * Get attribute taxonomy by slug. 
  1775. * 
  1776. * @since 2.2 
  1777. * @param string $slug 
  1778. * @return string|null 
  1779. */ 
  1780. private function get_attribute_taxonomy_by_slug( $slug ) { 
  1781. $taxonomy = null; 
  1782. $attribute_taxonomies = wc_get_attribute_taxonomies(); 
  1783.  
  1784. foreach ( $attribute_taxonomies as $key => $tax ) { 
  1785. if ( $slug == $tax->attribute_name ) { 
  1786. $taxonomy = 'pa_' . $tax->attribute_name; 
  1787.  
  1788. break; 
  1789.  
  1790. return $taxonomy; 
  1791.  
  1792. /** 
  1793. * Get the images for a product or product variation 
  1794. * 
  1795. * @since 2.1 
  1796. * @param WC_Product|WC_Product_Variation $product 
  1797. * @return array 
  1798. */ 
  1799. private function get_images( $product ) { 
  1800. $images = $attachment_ids = array(); 
  1801. $product_image = $product->get_image_id(); 
  1802.  
  1803. // Add featured image. 
  1804. if ( ! empty( $product_image ) ) { 
  1805. $attachment_ids[] = $product_image; 
  1806.  
  1807. // Add gallery images. 
  1808. $attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() ); 
  1809.  
  1810. // Build image data. 
  1811. foreach ( $attachment_ids as $position => $attachment_id ) { 
  1812.  
  1813. $attachment_post = get_post( $attachment_id ); 
  1814.  
  1815. if ( is_null( $attachment_post ) ) { 
  1816. continue; 
  1817.  
  1818. $attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); 
  1819.  
  1820. if ( ! is_array( $attachment ) ) { 
  1821. continue; 
  1822.  
  1823. $images[] = array( 
  1824. 'id' => (int) $attachment_id,  
  1825. 'created_at' => $this->server->format_datetime( $attachment_post->post_date_gmt ),  
  1826. 'updated_at' => $this->server->format_datetime( $attachment_post->post_modified_gmt ),  
  1827. 'src' => current( $attachment ),  
  1828. 'title' => get_the_title( $attachment_id ),  
  1829. 'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),  
  1830. 'position' => (int) $position,  
  1831. ); 
  1832.  
  1833. // Set a placeholder image if the product has no images set. 
  1834. if ( empty( $images ) ) { 
  1835.  
  1836. $images[] = array( 
  1837. 'id' => 0,  
  1838. 'created_at' => $this->server->format_datetime( time() ), // Default to now. 
  1839. 'updated_at' => $this->server->format_datetime( time() ),  
  1840. 'src' => wc_placeholder_img_src(),  
  1841. 'title' => __( 'Placeholder', 'woocommerce' ),  
  1842. 'alt' => __( 'Placeholder', 'woocommerce' ),  
  1843. 'position' => 0,  
  1844. ); 
  1845.  
  1846. return $images; 
  1847.  
  1848. /** 
  1849. * Save product images. 
  1850. * 
  1851. * @since 2.2 
  1852. * @param WC_Product $product 
  1853. * @param array $images 
  1854. * @throws WC_API_Exception 
  1855. * @return WC_Product 
  1856. */ 
  1857. protected function save_product_images( $product, $images ) { 
  1858. if ( is_array( $images ) ) { 
  1859. $gallery = array(); 
  1860.  
  1861. foreach ( $images as $image ) { 
  1862. if ( isset( $image['position'] ) && 0 == $image['position'] ) { 
  1863. $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; 
  1864.  
  1865. if ( 0 === $attachment_id && isset( $image['src'] ) ) { 
  1866. $upload = $this->upload_product_image( esc_url_raw( $image['src'] ) ); 
  1867.  
  1868. if ( is_wp_error( $upload ) ) { 
  1869. throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 ); 
  1870.  
  1871. $attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() ); 
  1872.  
  1873. $product->set_image_id( $attachment_id ); 
  1874. } else { 
  1875. $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; 
  1876.  
  1877. if ( 0 === $attachment_id && isset( $image['src'] ) ) { 
  1878. $upload = $this->upload_product_image( esc_url_raw( $image['src'] ) ); 
  1879.  
  1880. if ( is_wp_error( $upload ) ) { 
  1881. throw new WC_API_Exception( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400 ); 
  1882.  
  1883. $attachment_id = $this->set_product_image_as_attachment( $upload, $product->get_id() ); 
  1884.  
  1885. $gallery[] = $attachment_id; 
  1886.  
  1887. // Set the image alt if present. 
  1888. if ( ! empty( $image['alt'] ) && $attachment_id ) { 
  1889. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) ); 
  1890.  
  1891. // Set the image title if present. 
  1892. if ( ! empty( $image['title'] ) && $attachment_id ) { 
  1893. wp_update_post( array( 'ID' => $attachment_id, 'post_title' => $image['title'] ) ); 
  1894.  
  1895. if ( ! empty( $gallery ) ) { 
  1896. $product->set_gallery_image_ids( $gallery ); 
  1897. } else { 
  1898. $product->set_image_id( '' ); 
  1899. $product->set_gallery_image_ids( array() ); 
  1900.  
  1901. return $product; 
  1902.  
  1903. /** 
  1904. * Upload image from URL 
  1905. * 
  1906. * @since 2.2 
  1907. * @param string $image_url 
  1908. * @return int|WP_Error attachment id 
  1909. */ 
  1910. public function upload_product_image( $image_url ) { 
  1911. return $this->upload_image_from_url( $image_url, 'product_image' ); 
  1912.  
  1913. /** 
  1914. * Upload product category image from URL. 
  1915. * 
  1916. * @since 2.5.0 
  1917. * @param string $image_url 
  1918. * @return int|WP_Error attachment id 
  1919. */ 
  1920. public function upload_product_category_image( $image_url ) { 
  1921. return $this->upload_image_from_url( $image_url, 'product_category_image' ); 
  1922.  
  1923. /** 
  1924. * Upload image from URL. 
  1925. * 
  1926. * @throws WC_API_Exception 
  1927. * 
  1928. * @since 2.5.0 
  1929. * @param string $image_url 
  1930. * @param string $upload_for 
  1931. * @return int|WP_Error Attachment id 
  1932. */ 
  1933. protected function upload_image_from_url( $image_url, $upload_for = 'product_image' ) { 
  1934. $file_name = basename( current( explode( '?', $image_url ) ) ); 
  1935. $parsed_url = @parse_url( $image_url ); 
  1936.  
  1937. // Check parsed URL. 
  1938. if ( ! $parsed_url || ! is_array( $parsed_url ) ) { 
  1939. throw new WC_API_Exception( 'woocommerce_api_invalid_' . $upload_for, sprintf( __( 'Invalid URL %s.', 'woocommerce' ), $image_url ), 400 ); 
  1940.  
  1941. // Ensure url is valid. 
  1942. $image_url = str_replace( ' ', '%20', $image_url ); 
  1943.  
  1944. // Get the file. 
  1945. $response = wp_safe_remote_get( $image_url, array( 
  1946. 'timeout' => 10,  
  1947. ) ); 
  1948.  
  1949. if ( is_wp_error( $response ) ) { 
  1950. throw new WC_API_Exception( 'woocommerce_api_invalid_remote_' . $upload_for, sprintf( __( 'Error getting remote image %s.', 'woocommerce' ), $image_url ) . ' ' . sprintf( __( 'Error: %s.', 'woocommerce' ), $response->get_error_message() ), 400 ); 
  1951. } elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) { 
  1952. throw new WC_API_Exception( 'woocommerce_api_invalid_remote_' . $upload_for, sprintf( __( 'Error getting remote image %s.', 'woocommerce' ), $image_url ), 400 ); 
  1953.  
  1954. // Ensure we have a file name and type. 
  1955. $wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() ); 
  1956.  
  1957. if ( ! $wp_filetype['type'] ) { 
  1958. $headers = wp_remote_retrieve_headers( $response ); 
  1959. if ( isset( $headers['content-disposition'] ) && strstr( $headers['content-disposition'], 'filename=' ) ) { 
  1960. $disposition = end( explode( 'filename=', $headers['content-disposition'] ) ); 
  1961. $disposition = sanitize_file_name( $disposition ); 
  1962. $file_name = $disposition; 
  1963. } elseif ( isset( $headers['content-type'] ) && strstr( $headers['content-type'], 'image/' ) ) { 
  1964. $file_name = 'image.' . str_replace( 'image/', '', $headers['content-type'] ); 
  1965. unset( $headers ); 
  1966.  
  1967. // Recheck filetype 
  1968. $wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() ); 
  1969.  
  1970. if ( ! $wp_filetype['type'] ) { 
  1971. throw new WC_API_Exception( 'woocommerce_api_invalid_' . $upload_for, __( 'Invalid image type.', 'woocommerce' ), 400 ); 
  1972.  
  1973. // Upload the file. 
  1974. $upload = wp_upload_bits( $file_name, '', wp_remote_retrieve_body( $response ) ); 
  1975.  
  1976. if ( $upload['error'] ) { 
  1977. throw new WC_API_Exception( 'woocommerce_api_' . $upload_for . '_upload_error', $upload['error'], 400 ); 
  1978.  
  1979. // Get filesize. 
  1980. $filesize = filesize( $upload['file'] ); 
  1981.  
  1982. if ( 0 == $filesize ) { 
  1983. @unlink( $upload['file'] ); 
  1984. unset( $upload ); 
  1985. throw new WC_API_Exception( 'woocommerce_api_' . $upload_for . '_upload_file_error', __( 'Zero size file downloaded.', 'woocommerce' ), 400 ); 
  1986.  
  1987. unset( $response ); 
  1988.  
  1989. do_action( 'woocommerce_api_uploaded_image_from_url', $upload, $image_url, $upload_for ); 
  1990.  
  1991. return $upload; 
  1992.  
  1993. /** 
  1994. * Sets product image as attachment and returns the attachment ID. 
  1995. * 
  1996. * @since 2.2 
  1997. * @param array $upload 
  1998. * @param int $id 
  1999. * @return int 
  2000. */ 
  2001. protected function set_product_image_as_attachment( $upload, $id ) { 
  2002. return $this->set_uploaded_image_as_attachment( $upload, $id ); 
  2003.  
  2004. /** 
  2005. * Sets uploaded category image as attachment and returns the attachment ID. 
  2006. * 
  2007. * @since 2.5.0 
  2008. * @param integer $upload Upload information from wp_upload_bits 
  2009. * @return int Attachment ID 
  2010. */ 
  2011. protected function set_product_category_image_as_attachment( $upload ) { 
  2012. return $this->set_uploaded_image_as_attachment( $upload ); 
  2013.  
  2014. /** 
  2015. * Set uploaded image as attachment. 
  2016. * 
  2017. * @since 2.5.0 
  2018. * @param array $upload Upload information from wp_upload_bits 
  2019. * @param int $id Post ID. Default to 0. 
  2020. * @return int Attachment ID 
  2021. */ 
  2022. protected function set_uploaded_image_as_attachment( $upload, $id = 0 ) { 
  2023. $info = wp_check_filetype( $upload['file'] ); 
  2024. $title = ''; 
  2025. $content = ''; 
  2026.  
  2027. if ( $image_meta = @wp_read_image_metadata( $upload['file'] ) ) { 
  2028. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { 
  2029. $title = wc_clean( $image_meta['title'] ); 
  2030. if ( trim( $image_meta['caption'] ) ) { 
  2031. $content = wc_clean( $image_meta['caption'] ); 
  2032.  
  2033. $attachment = array( 
  2034. 'post_mime_type' => $info['type'],  
  2035. 'guid' => $upload['url'],  
  2036. 'post_parent' => $id,  
  2037. 'post_title' => $title,  
  2038. 'post_content' => $content,  
  2039. ); 
  2040.  
  2041. $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $id ); 
  2042. if ( ! is_wp_error( $attachment_id ) ) { 
  2043. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) ); 
  2044.  
  2045. return $attachment_id; 
  2046.  
  2047. /** 
  2048. * Get attribute options. 
  2049. * 
  2050. * @param int $product_id 
  2051. * @param array $attribute 
  2052. * @return array 
  2053. */ 
  2054. protected function get_attribute_options( $product_id, $attribute ) { 
  2055. if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) { 
  2056. return wc_get_product_terms( $product_id, $attribute['name'], array( 'fields' => 'names' ) ); 
  2057. } elseif ( isset( $attribute['value'] ) ) { 
  2058. return array_map( 'trim', explode( '|', $attribute['value'] ) ); 
  2059.  
  2060. return array(); 
  2061.  
  2062. /** 
  2063. * Get the attributes for a product or product variation 
  2064. * 
  2065. * @since 2.1 
  2066. * @param WC_Product|WC_Product_Variation $product 
  2067. * @return array 
  2068. */ 
  2069. private function get_attributes( $product ) { 
  2070.  
  2071. $attributes = array(); 
  2072.  
  2073. if ( $product->is_type( 'variation' ) ) { 
  2074.  
  2075. // variation attributes 
  2076. foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) { 
  2077.  
  2078. // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` 
  2079. $attributes[] = array( 
  2080. 'name' => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $product ),  
  2081. 'slug' => str_replace( 'attribute_', '', str_replace( 'pa_', '', $attribute_name ) ),  
  2082. 'option' => $attribute,  
  2083. ); 
  2084. } else { 
  2085.  
  2086. foreach ( $product->get_attributes() as $attribute ) { 
  2087. $attributes[] = array( 
  2088. 'name' => wc_attribute_label( $attribute['name'], $product ),  
  2089. 'slug' => str_replace( 'pa_', '', $attribute['name'] ),  
  2090. 'position' => (int) $attribute['position'],  
  2091. 'visible' => (bool) $attribute['is_visible'],  
  2092. 'variation' => (bool) $attribute['is_variation'],  
  2093. 'options' => $this->get_attribute_options( $product->get_id(), $attribute ),  
  2094. ); 
  2095.  
  2096. return $attributes; 
  2097.  
  2098. /** 
  2099. * Get the downloads for a product or product variation 
  2100. * 
  2101. * @since 2.1 
  2102. * @param WC_Product|WC_Product_Variation $product 
  2103. * @return array 
  2104. */ 
  2105. private function get_downloads( $product ) { 
  2106.  
  2107. $downloads = array(); 
  2108.  
  2109. if ( $product->is_downloadable() ) { 
  2110.  
  2111. foreach ( $product->get_downloads() as $file_id => $file ) { 
  2112.  
  2113. $downloads[] = array( 
  2114. 'id' => $file_id, // do not cast as int as this is a hash 
  2115. 'name' => $file['name'],  
  2116. 'file' => $file['file'],  
  2117. ); 
  2118.  
  2119. return $downloads; 
  2120.  
  2121. /** 
  2122. * Get a listing of product attributes 
  2123. * 
  2124. * @since 2.5.0 
  2125. * @param string|null $fields fields to limit response to 
  2126. * @return array 
  2127. */ 
  2128. public function get_product_attributes( $fields = null ) { 
  2129. try { 
  2130. // Permissions check
  2131. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2132. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_attributes', __( 'You do not have permission to read product attributes', 'woocommerce' ), 401 ); 
  2133.  
  2134. $product_attributes = array(); 
  2135. $attribute_taxonomies = wc_get_attribute_taxonomies(); 
  2136.  
  2137. foreach ( $attribute_taxonomies as $attribute ) { 
  2138. $product_attributes[] = array( 
  2139. 'id' => intval( $attribute->attribute_id ),  
  2140. 'name' => $attribute->attribute_label,  
  2141. 'slug' => wc_attribute_taxonomy_name( $attribute->attribute_name ),  
  2142. 'type' => $attribute->attribute_type,  
  2143. 'order_by' => $attribute->attribute_orderby,  
  2144. 'has_archives' => (bool) $attribute->attribute_public,  
  2145. ); 
  2146.  
  2147. return array( 'product_attributes' => apply_filters( 'woocommerce_api_product_attributes_response', $product_attributes, $attribute_taxonomies, $fields, $this ) ); 
  2148. } catch ( WC_API_Exception $e ) { 
  2149. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2150.  
  2151. /** 
  2152. * Get the product attribute for the given ID 
  2153. * 
  2154. * @since 2.5.0 
  2155. * @param string $id product attribute term ID 
  2156. * @param string|null $fields fields to limit response to 
  2157. * @return array 
  2158. */ 
  2159. public function get_product_attribute( $id, $fields = null ) { 
  2160. global $wpdb; 
  2161.  
  2162. try { 
  2163. $id = absint( $id ); 
  2164.  
  2165. // Validate ID 
  2166. if ( empty( $id ) ) { 
  2167. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'Invalid product attribute ID', 'woocommerce' ), 400 ); 
  2168.  
  2169. // Permissions check 
  2170. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2171. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_attributes', __( 'You do not have permission to read product attributes', 'woocommerce' ), 401 ); 
  2172.  
  2173. $attribute = $wpdb->get_row( $wpdb->prepare( " 
  2174. SELECT * 
  2175. FROM {$wpdb->prefix}woocommerce_attribute_taxonomies 
  2176. WHERE attribute_id = %d 
  2177. ", $id ) ); 
  2178.  
  2179. if ( is_wp_error( $attribute ) || is_null( $attribute ) ) { 
  2180. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2181.  
  2182. $product_attribute = array( 
  2183. 'id' => intval( $attribute->attribute_id ),  
  2184. 'name' => $attribute->attribute_label,  
  2185. 'slug' => wc_attribute_taxonomy_name( $attribute->attribute_name ),  
  2186. 'type' => $attribute->attribute_type,  
  2187. 'order_by' => $attribute->attribute_orderby,  
  2188. 'has_archives' => (bool) $attribute->attribute_public,  
  2189. ); 
  2190.  
  2191. return array( 'product_attribute' => apply_filters( 'woocommerce_api_product_attribute_response', $product_attribute, $id, $fields, $attribute, $this ) ); 
  2192. } catch ( WC_API_Exception $e ) { 
  2193. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2194.  
  2195. /** 
  2196. * Validate attribute data. 
  2197. * 
  2198. * @since 2.5.0 
  2199. * @param string $name 
  2200. * @param string $slug 
  2201. * @param string $type 
  2202. * @param string $order_by 
  2203. * @param bool $new_data 
  2204. * @return bool 
  2205. * @throws WC_API_Exception 
  2206. */ 
  2207. protected function validate_attribute_data( $name, $slug, $type, $order_by, $new_data = true ) { 
  2208. if ( empty( $name ) ) { 
  2209. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 ); 
  2210.  
  2211. if ( strlen( $slug ) >= 28 ) { 
  2212. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), 400 ); 
  2213. } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) { 
  2214. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), 400 ); 
  2215. } elseif ( $new_data && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) { 
  2216. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), 400 ); 
  2217.  
  2218. // Validate the attribute type 
  2219. if ( ! in_array( wc_clean( $type ), array_keys( wc_get_attribute_types() ) ) ) { 
  2220. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_type', sprintf( __( 'Invalid product attribute type - the product attribute type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_attribute_types() ) ) ), 400 ); 
  2221.  
  2222. // Validate the attribute order by 
  2223. if ( ! in_array( wc_clean( $order_by ), array( 'menu_order', 'name', 'name_num', 'id' ) ) ) { 
  2224. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_order_by', sprintf( __( 'Invalid product attribute order_by type - the product attribute order_by type must be any of these: %s', 'woocommerce' ), implode( ', ', array( 'menu_order', 'name', 'name_num', 'id' ) ) ), 400 ); 
  2225.  
  2226. return true; 
  2227.  
  2228. /** 
  2229. * Create a new product attribute. 
  2230. * 
  2231. * @since 2.5.0 
  2232. * @param array $data Posted data. 
  2233. * @return array 
  2234. */ 
  2235. public function create_product_attribute( $data ) { 
  2236. global $wpdb; 
  2237.  
  2238. try { 
  2239. if ( ! isset( $data['product_attribute'] ) ) { 
  2240. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_attribute' ), 400 ); 
  2241.  
  2242. $data = $data['product_attribute']; 
  2243.  
  2244. // Check permissions. 
  2245. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2246. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_attribute', __( 'You do not have permission to create product attributes', 'woocommerce' ), 401 ); 
  2247.  
  2248. $data = apply_filters( 'woocommerce_api_create_product_attribute_data', $data, $this ); 
  2249.  
  2250. if ( ! isset( $data['name'] ) ) { 
  2251. $data['name'] = ''; 
  2252.  
  2253. // Set the attribute slug. 
  2254. if ( ! isset( $data['slug'] ) ) { 
  2255. $data['slug'] = wc_sanitize_taxonomy_name( stripslashes( $data['name'] ) ); 
  2256. } else { 
  2257. $data['slug'] = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( stripslashes( $data['slug'] ) ) ); 
  2258.  
  2259. // Set attribute type when not sent. 
  2260. if ( ! isset( $data['type'] ) ) { 
  2261. $data['type'] = 'select'; 
  2262.  
  2263. // Set order by when not sent. 
  2264. if ( ! isset( $data['order_by'] ) ) { 
  2265. $data['order_by'] = 'menu_order'; 
  2266.  
  2267. // Validate the attribute data. 
  2268. $this->validate_attribute_data( $data['name'], $data['slug'], $data['type'], $data['order_by'], true ); 
  2269.  
  2270. $insert = $wpdb->insert( 
  2271. $wpdb->prefix . 'woocommerce_attribute_taxonomies',  
  2272. array( 
  2273. 'attribute_label' => $data['name'],  
  2274. 'attribute_name' => $data['slug'],  
  2275. 'attribute_type' => $data['type'],  
  2276. 'attribute_orderby' => $data['order_by'],  
  2277. 'attribute_public' => isset( $data['has_archives'] ) && true === $data['has_archives'] ? 1 : 0,  
  2278. ),  
  2279. array( '%s', '%s', '%s', '%s', '%d' ) 
  2280. ); 
  2281.  
  2282. // Checks for an error in the product creation. 
  2283. if ( is_wp_error( $insert ) ) { 
  2284. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_attribute', $insert->get_error_message(), 400 ); 
  2285.  
  2286. $id = $wpdb->insert_id; 
  2287.  
  2288. do_action( 'woocommerce_api_create_product_attribute', $id, $data ); 
  2289.  
  2290. // Clear transients. 
  2291. wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' ); 
  2292. delete_transient( 'wc_attribute_taxonomies' ); 
  2293.  
  2294. $this->server->send_status( 201 ); 
  2295.  
  2296. return $this->get_product_attribute( $id ); 
  2297. } catch ( WC_API_Exception $e ) { 
  2298. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2299.  
  2300. /** 
  2301. * Edit a product attribute. 
  2302. * 
  2303. * @since 2.5.0 
  2304. * @param int $id the attribute ID. 
  2305. * @param array $data 
  2306. * @return array 
  2307. */ 
  2308. public function edit_product_attribute( $id, $data ) { 
  2309. global $wpdb; 
  2310.  
  2311. try { 
  2312. if ( ! isset( $data['product_attribute'] ) ) { 
  2313. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_attribute' ), 400 ); 
  2314.  
  2315. $id = absint( $id ); 
  2316. $data = $data['product_attribute']; 
  2317.  
  2318. // Check permissions. 
  2319. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2320. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_attribute', __( 'You do not have permission to edit product attributes', 'woocommerce' ), 401 ); 
  2321.  
  2322. $data = apply_filters( 'woocommerce_api_edit_product_attribute_data', $data, $this ); 
  2323. $attribute = $this->get_product_attribute( $id ); 
  2324.  
  2325. if ( is_wp_error( $attribute ) ) { 
  2326. return $attribute; 
  2327.  
  2328. $attribute_name = isset( $data['name'] ) ? $data['name'] : $attribute['product_attribute']['name']; 
  2329. $attribute_type = isset( $data['type'] ) ? $data['type'] : $attribute['product_attribute']['type']; 
  2330. $attribute_order_by = isset( $data['order_by'] ) ? $data['order_by'] : $attribute['product_attribute']['order_by']; 
  2331.  
  2332. if ( isset( $data['slug'] ) ) { 
  2333. $attribute_slug = wc_sanitize_taxonomy_name( stripslashes( $data['slug'] ) ); 
  2334. } else { 
  2335. $attribute_slug = $attribute['product_attribute']['slug']; 
  2336. $attribute_slug = preg_replace( '/^pa\_/', '', $attribute_slug ); 
  2337.  
  2338. if ( isset( $data['has_archives'] ) ) { 
  2339. $attribute_public = true === $data['has_archives'] ? 1 : 0; 
  2340. } else { 
  2341. $attribute_public = $attribute['product_attribute']['has_archives']; 
  2342.  
  2343. // Validate the attribute data. 
  2344. $this->validate_attribute_data( $attribute_name, $attribute_slug, $attribute_type, $attribute_order_by, false ); 
  2345.  
  2346. $update = $wpdb->update( 
  2347. $wpdb->prefix . 'woocommerce_attribute_taxonomies',  
  2348. array( 
  2349. 'attribute_label' => $attribute_name,  
  2350. 'attribute_name' => $attribute_slug,  
  2351. 'attribute_type' => $attribute_type,  
  2352. 'attribute_orderby' => $attribute_order_by,  
  2353. 'attribute_public' => $attribute_public,  
  2354. ),  
  2355. array( 'attribute_id' => $id ),  
  2356. array( '%s', '%s', '%s', '%s', '%d' ),  
  2357. array( '%d' ) 
  2358. ); 
  2359.  
  2360. // Checks for an error in the product creation. 
  2361. if ( false === $update ) { 
  2362. throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_attribute', __( 'Could not edit the attribute', 'woocommerce' ), 400 ); 
  2363.  
  2364. do_action( 'woocommerce_api_edit_product_attribute', $id, $data ); 
  2365.  
  2366. // Clear transients. 
  2367. wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' ); 
  2368. delete_transient( 'wc_attribute_taxonomies' ); 
  2369.  
  2370. return $this->get_product_attribute( $id ); 
  2371. } catch ( WC_API_Exception $e ) { 
  2372. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2373.  
  2374. /** 
  2375. * Delete a product attribute. 
  2376. * 
  2377. * @since 2.5.0 
  2378. * @param int $id the product attribute ID. 
  2379. * @return array 
  2380. */ 
  2381. public function delete_product_attribute( $id ) { 
  2382. global $wpdb; 
  2383.  
  2384. try { 
  2385. // Check permissions. 
  2386. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2387. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_attribute', __( 'You do not have permission to delete product attributes', 'woocommerce' ), 401 ); 
  2388.  
  2389. $id = absint( $id ); 
  2390.  
  2391. $attribute_name = $wpdb->get_var( $wpdb->prepare( " 
  2392. SELECT attribute_name 
  2393. FROM {$wpdb->prefix}woocommerce_attribute_taxonomies 
  2394. WHERE attribute_id = %d 
  2395. ", $id ) ); 
  2396.  
  2397. if ( is_null( $attribute_name ) ) { 
  2398. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2399.  
  2400. $deleted = $wpdb->delete( 
  2401. $wpdb->prefix . 'woocommerce_attribute_taxonomies',  
  2402. array( 'attribute_id' => $id ),  
  2403. array( '%d' ) 
  2404. ); 
  2405.  
  2406. if ( false === $deleted ) { 
  2407. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute', __( 'Could not delete the attribute', 'woocommerce' ), 401 ); 
  2408.  
  2409. $taxonomy = wc_attribute_taxonomy_name( $attribute_name ); 
  2410.  
  2411. if ( taxonomy_exists( $taxonomy ) ) { 
  2412. $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' ); 
  2413. foreach ( $terms as $term ) { 
  2414. wp_delete_term( $term->term_id, $taxonomy ); 
  2415.  
  2416. do_action( 'woocommerce_attribute_deleted', $id, $attribute_name, $taxonomy ); 
  2417. do_action( 'woocommerce_api_delete_product_attribute', $id, $this ); 
  2418.  
  2419. // Clear transients. 
  2420. wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' ); 
  2421. delete_transient( 'wc_attribute_taxonomies' ); 
  2422.  
  2423. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_attribute' ) ); 
  2424. } catch ( WC_API_Exception $e ) { 
  2425. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2426.  
  2427. /** 
  2428. * Get a listing of product attribute terms. 
  2429. * 
  2430. * @since 2.5.0 
  2431. * @param int $attribute_id Attribute ID. 
  2432. * @param string|null $fields Fields to limit response to. 
  2433. * @return array 
  2434. */ 
  2435. public function get_product_attribute_terms( $attribute_id, $fields = null ) { 
  2436. try { 
  2437. // Permissions check
  2438. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2439. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_attribute_terms', __( 'You do not have permission to read product attribute terms', 'woocommerce' ), 401 ); 
  2440.  
  2441. $taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  2442.  
  2443. if ( ! $taxonomy ) { 
  2444. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2445.  
  2446. $args = array( 'hide_empty' => false ); 
  2447. $orderby = wc_attribute_orderby( $taxonomy ); 
  2448.  
  2449. switch ( $orderby ) { 
  2450. case 'name' : 
  2451. $args['orderby'] = 'name'; 
  2452. $args['menu_order'] = false; 
  2453. break; 
  2454. case 'id' : 
  2455. $args['orderby'] = 'id'; 
  2456. $args['order'] = 'ASC'; 
  2457. $args['menu_order'] = false; 
  2458. break; 
  2459. case 'menu_order' : 
  2460. $args['menu_order'] = 'ASC'; 
  2461. break; 
  2462.  
  2463. $terms = get_terms( $taxonomy, $args ); 
  2464. $attribute_terms = array(); 
  2465.  
  2466. foreach ( $terms as $term ) { 
  2467. $attribute_terms[] = array( 
  2468. 'id' => $term->term_id,  
  2469. 'slug' => $term->slug,  
  2470. 'name' => $term->name,  
  2471. 'count' => $term->count,  
  2472. ); 
  2473.  
  2474. return array( 'product_attribute_terms' => apply_filters( 'woocommerce_api_product_attribute_terms_response', $attribute_terms, $terms, $fields, $this ) ); 
  2475. } catch ( WC_API_Exception $e ) { 
  2476. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2477.  
  2478. /** 
  2479. * Get the product attribute term for the given ID. 
  2480. * 
  2481. * @since 2.5.0 
  2482. * @param int $attribute_id Attribute ID. 
  2483. * @param string $id Product attribute term ID. 
  2484. * @param string|null $fields Fields to limit response to. 
  2485. * @return array 
  2486. */ 
  2487. public function get_product_attribute_term( $attribute_id, $id, $fields = null ) { 
  2488. global $wpdb; 
  2489.  
  2490. try { 
  2491. $id = absint( $id ); 
  2492.  
  2493. // Validate ID 
  2494. if ( empty( $id ) ) { 
  2495. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_term_id', __( 'Invalid product attribute ID', 'woocommerce' ), 400 ); 
  2496.  
  2497. // Permissions check 
  2498. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2499. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_attribute_terms', __( 'You do not have permission to read product attribute terms', 'woocommerce' ), 401 ); 
  2500.  
  2501. $taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  2502.  
  2503. if ( ! $taxonomy ) { 
  2504. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2505.  
  2506. $term = get_term( $id, $taxonomy ); 
  2507.  
  2508. if ( is_wp_error( $term ) || is_null( $term ) ) { 
  2509. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_term_id', __( 'A product attribute term with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2510.  
  2511. $attribute_term = array( 
  2512. 'id' => $term->term_id,  
  2513. 'name' => $term->name,  
  2514. 'slug' => $term->slug,  
  2515. 'count' => $term->count,  
  2516. ); 
  2517.  
  2518. return array( 'product_attribute_term' => apply_filters( 'woocommerce_api_product_attribute_response', $attribute_term, $id, $fields, $term, $this ) ); 
  2519. } catch ( WC_API_Exception $e ) { 
  2520. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2521.  
  2522. /** 
  2523. * Create a new product attribute term. 
  2524. * 
  2525. * @since 2.5.0 
  2526. * @param int $attribute_id Attribute ID. 
  2527. * @param array $data Posted data. 
  2528. * @return array 
  2529. */ 
  2530. public function create_product_attribute_term( $attribute_id, $data ) { 
  2531. global $wpdb; 
  2532.  
  2533. try { 
  2534. if ( ! isset( $data['product_attribute_term'] ) ) { 
  2535. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_term_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_attribute_term' ), 400 ); 
  2536.  
  2537. $data = $data['product_attribute_term']; 
  2538.  
  2539. // Check permissions. 
  2540. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2541. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_attribute', __( 'You do not have permission to create product attributes', 'woocommerce' ), 401 ); 
  2542.  
  2543. $taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  2544.  
  2545. if ( ! $taxonomy ) { 
  2546. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2547.  
  2548. $data = apply_filters( 'woocommerce_api_create_product_attribute_term_data', $data, $this ); 
  2549.  
  2550. // Check if attribute term name is specified. 
  2551. if ( ! isset( $data['name'] ) ) { 
  2552. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_term_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 ); 
  2553.  
  2554. $args = array(); 
  2555.  
  2556. // Set the attribute term slug. 
  2557. if ( isset( $data['slug'] ) ) { 
  2558. $args['slug'] = sanitize_title( wp_unslash( $data['slug'] ) ); 
  2559.  
  2560. $term = wp_insert_term( $data['name'], $taxonomy, $args ); 
  2561.  
  2562. // Checks for an error in the term creation. 
  2563. if ( is_wp_error( $term ) ) { 
  2564. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_attribute', $term->get_error_message(), 400 ); 
  2565.  
  2566. $id = $term['term_id']; 
  2567.  
  2568. do_action( 'woocommerce_api_create_product_attribute_term', $id, $data ); 
  2569.  
  2570. $this->server->send_status( 201 ); 
  2571.  
  2572. return $this->get_product_attribute_term( $attribute_id, $id ); 
  2573. } catch ( WC_API_Exception $e ) { 
  2574. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2575.  
  2576. /** 
  2577. * Edit a product attribute term. 
  2578. * 
  2579. * @since 2.5.0 
  2580. * @param int $attribute_id Attribute ID. 
  2581. * @param int $id the attribute ID. 
  2582. * @param array $data 
  2583. * @return array 
  2584. */ 
  2585. public function edit_product_attribute_term( $attribute_id, $id, $data ) { 
  2586. global $wpdb; 
  2587.  
  2588. try { 
  2589. if ( ! isset( $data['product_attribute_term'] ) ) { 
  2590. throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_term_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_attribute_term' ), 400 ); 
  2591.  
  2592. $id = absint( $id ); 
  2593. $data = $data['product_attribute_term']; 
  2594.  
  2595. // Check permissions. 
  2596. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2597. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_attribute', __( 'You do not have permission to edit product attributes', 'woocommerce' ), 401 ); 
  2598.  
  2599. $taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  2600.  
  2601. if ( ! $taxonomy ) { 
  2602. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2603.  
  2604. $data = apply_filters( 'woocommerce_api_edit_product_attribute_term_data', $data, $this ); 
  2605.  
  2606. $args = array(); 
  2607.  
  2608. // Update name. 
  2609. if ( isset( $data['name'] ) ) { 
  2610. $args['name'] = wc_clean( wp_unslash( $data['name'] ) ); 
  2611.  
  2612. // Update slug. 
  2613. if ( isset( $data['slug'] ) ) { 
  2614. $args['slug'] = sanitize_title( wp_unslash( $data['slug'] ) ); 
  2615.  
  2616. $term = wp_update_term( $id, $taxonomy, $args ); 
  2617.  
  2618. if ( is_wp_error( $term ) ) { 
  2619. throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_attribute_term', $term->get_error_message(), 400 ); 
  2620.  
  2621. do_action( 'woocommerce_api_edit_product_attribute_term', $id, $data ); 
  2622.  
  2623. return $this->get_product_attribute_term( $attribute_id, $id ); 
  2624. } catch ( WC_API_Exception $e ) { 
  2625. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2626.  
  2627. /** 
  2628. * Delete a product attribute term. 
  2629. * 
  2630. * @since 2.5.0 
  2631. * @param int $attribute_id Attribute ID. 
  2632. * @param int $id the product attribute ID. 
  2633. * @return array 
  2634. */ 
  2635. public function delete_product_attribute_term( $attribute_id, $id ) { 
  2636. global $wpdb; 
  2637.  
  2638. try { 
  2639. // Check permissions. 
  2640. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2641. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_attribute_term', __( 'You do not have permission to delete product attribute terms', 'woocommerce' ), 401 ); 
  2642.  
  2643. $taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id ); 
  2644.  
  2645. if ( ! $taxonomy ) { 
  2646. throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2647.  
  2648. $id = absint( $id ); 
  2649. $term = wp_delete_term( $id, $taxonomy ); 
  2650.  
  2651. if ( ! $term ) { 
  2652. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute_term', sprintf( __( 'This %s cannot be deleted', 'woocommerce' ), 'product_attribute_term' ), 500 ); 
  2653. } elseif ( is_wp_error( $term ) ) { 
  2654. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute_term', $term->get_error_message(), 400 ); 
  2655.  
  2656. do_action( 'woocommerce_api_delete_product_attribute_term', $id, $this ); 
  2657.  
  2658. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_attribute' ) ); 
  2659. } catch ( WC_API_Exception $e ) { 
  2660. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2661.  
  2662. /** 
  2663. * Clear product 
  2664. */ 
  2665. protected function clear_product( $product_id ) { 
  2666. if ( ! is_numeric( $product_id ) || 0 >= $product_id ) { 
  2667. return; 
  2668.  
  2669. // Delete product attachments 
  2670. $attachments = get_children( array( 
  2671. 'post_parent' => $product_id,  
  2672. 'post_status' => 'any',  
  2673. 'post_type' => 'attachment',  
  2674. ) ); 
  2675.  
  2676. foreach ( (array) $attachments as $attachment ) { 
  2677. wp_delete_attachment( $attachment->ID, true ); 
  2678.  
  2679. // Delete product 
  2680. $product = wc_get_product( $product_id ); 
  2681. $product->delete( true ); 
  2682.  
  2683. /** 
  2684. * Bulk update or insert products 
  2685. * Accepts an array with products in the formats supported by 
  2686. * WC_API_Products->create_product() and WC_API_Products->edit_product() 
  2687. * 
  2688. * @since 2.4.0 
  2689. * @param array $data 
  2690. * @return array 
  2691. */ 
  2692. public function bulk( $data ) { 
  2693.  
  2694. try { 
  2695. if ( ! isset( $data['products'] ) ) { 
  2696. throw new WC_API_Exception( 'woocommerce_api_missing_products_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'products' ), 400 ); 
  2697.  
  2698. $data = $data['products']; 
  2699. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'products' ); 
  2700.  
  2701. // Limit bulk operation 
  2702. if ( count( $data ) > $limit ) { 
  2703. throw new WC_API_Exception( 'woocommerce_api_products_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 ); 
  2704.  
  2705. $products = array(); 
  2706.  
  2707. foreach ( $data as $_product ) { 
  2708. $product_id = 0; 
  2709. $product_sku = ''; 
  2710.  
  2711. // Try to get the product ID 
  2712. if ( isset( $_product['id'] ) ) { 
  2713. $product_id = intval( $_product['id'] ); 
  2714.  
  2715. if ( ! $product_id && isset( $_product['sku'] ) ) { 
  2716. $product_sku = wc_clean( $_product['sku'] ); 
  2717. $product_id = wc_get_product_id_by_sku( $product_sku ); 
  2718.  
  2719. if ( $product_id ) { 
  2720.  
  2721. // Product exists / edit product 
  2722. $edit = $this->edit_product( $product_id, array( 'product' => $_product ) ); 
  2723.  
  2724. if ( is_wp_error( $edit ) ) { 
  2725. $products[] = array( 
  2726. 'id' => $product_id,  
  2727. 'sku' => $product_sku,  
  2728. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),  
  2729. ); 
  2730. } else { 
  2731. $products[] = $edit['product']; 
  2732. } else { 
  2733.  
  2734. // Product don't exists / create product 
  2735. $new = $this->create_product( array( 'product' => $_product ) ); 
  2736.  
  2737. if ( is_wp_error( $new ) ) { 
  2738. $products[] = array( 
  2739. 'id' => $product_id,  
  2740. 'sku' => $product_sku,  
  2741. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),  
  2742. ); 
  2743. } else { 
  2744. $products[] = $new['product']; 
  2745.  
  2746. return array( 'products' => apply_filters( 'woocommerce_api_products_bulk_response', $products, $this ) ); 
  2747. } catch ( WC_API_Exception $e ) { 
  2748. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2749.  
  2750. /** 
  2751. * Get a listing of product shipping classes. 
  2752. * 
  2753. * @since 2.5.0 
  2754. * @param string|null $fields Fields to limit response to 
  2755. * @return array|WP_Error List of product shipping classes if succeed,  
  2756. * otherwise WP_Error will be returned 
  2757. */ 
  2758. public function get_product_shipping_classes( $fields = null ) { 
  2759. try { 
  2760. // Permissions check 
  2761. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2762. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_shipping_classes', __( 'You do not have permission to read product shipping classes', 'woocommerce' ), 401 ); 
  2763.  
  2764. $product_shipping_classes = array(); 
  2765.  
  2766. $terms = get_terms( 'product_shipping_class', array( 'hide_empty' => false, 'fields' => 'ids' ) ); 
  2767.  
  2768. foreach ( $terms as $term_id ) { 
  2769. $product_shipping_classes[] = current( $this->get_product_shipping_class( $term_id, $fields ) ); 
  2770.  
  2771. return array( 'product_shipping_classes' => apply_filters( 'woocommerce_api_product_shipping_classes_response', $product_shipping_classes, $terms, $fields, $this ) ); 
  2772. } catch ( WC_API_Exception $e ) { 
  2773. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2774.  
  2775. /** 
  2776. * Get the product shipping class for the given ID. 
  2777. * 
  2778. * @since 2.5.0 
  2779. * @param string $id Product shipping class term ID 
  2780. * @param string|null $fields Fields to limit response to 
  2781. * @return array|WP_Error Product shipping class if succeed, otherwise 
  2782. * WP_Error will be returned 
  2783. */ 
  2784. public function get_product_shipping_class( $id, $fields = null ) { 
  2785. try { 
  2786. $id = absint( $id ); 
  2787. if ( ! $id ) { 
  2788. throw new WC_API_Exception( 'woocommerce_api_invalid_product_shipping_class_id', __( 'Invalid product shipping class ID', 'woocommerce' ), 400 ); 
  2789.  
  2790. // Permissions check 
  2791. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2792. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_shipping_classes', __( 'You do not have permission to read product shipping classes', 'woocommerce' ), 401 ); 
  2793.  
  2794. $term = get_term( $id, 'product_shipping_class' ); 
  2795.  
  2796. if ( is_wp_error( $term ) || is_null( $term ) ) { 
  2797. throw new WC_API_Exception( 'woocommerce_api_invalid_product_shipping_class_id', __( 'A product shipping class with the provided ID could not be found', 'woocommerce' ), 404 ); 
  2798.  
  2799. $term_id = intval( $term->term_id ); 
  2800.  
  2801. $product_shipping_class = array( 
  2802. 'id' => $term_id,  
  2803. 'name' => $term->name,  
  2804. 'slug' => $term->slug,  
  2805. 'parent' => $term->parent,  
  2806. 'description' => $term->description,  
  2807. 'count' => intval( $term->count ),  
  2808. ); 
  2809.  
  2810. return array( 'product_shipping_class' => apply_filters( 'woocommerce_api_product_shipping_class_response', $product_shipping_class, $id, $fields, $term, $this ) ); 
  2811. } catch ( WC_API_Exception $e ) { 
  2812. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2813.  
  2814. /** 
  2815. * Create a new product shipping class. 
  2816. * 
  2817. * @since 2.5.0 
  2818. * @param array $data Posted data 
  2819. * @return array|WP_Error Product shipping class if succeed, otherwise 
  2820. * WP_Error will be returned 
  2821. */ 
  2822. public function create_product_shipping_class( $data ) { 
  2823. global $wpdb; 
  2824.  
  2825. try { 
  2826. if ( ! isset( $data['product_shipping_class'] ) ) { 
  2827. throw new WC_API_Exception( 'woocommerce_api_missing_product_shipping_class_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'product_shipping_class' ), 400 ); 
  2828.  
  2829. // Check permissions 
  2830. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2831. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_product_shipping_class', __( 'You do not have permission to create product shipping classes', 'woocommerce' ), 401 ); 
  2832.  
  2833. $defaults = array( 
  2834. 'name' => '',  
  2835. 'slug' => '',  
  2836. 'description' => '',  
  2837. 'parent' => 0,  
  2838. ); 
  2839.  
  2840. $data = wp_parse_args( $data['product_shipping_class'], $defaults ); 
  2841. $data = apply_filters( 'woocommerce_api_create_product_shipping_class_data', $data, $this ); 
  2842.  
  2843. // Check parent. 
  2844. $data['parent'] = absint( $data['parent'] ); 
  2845. if ( $data['parent'] ) { 
  2846. $parent = get_term_by( 'id', $data['parent'], 'product_shipping_class' ); 
  2847. if ( ! $parent ) { 
  2848. throw new WC_API_Exception( 'woocommerce_api_invalid_product_shipping_class_parent', __( 'Product shipping class parent is invalid', 'woocommerce' ), 400 ); 
  2849.  
  2850. $insert = wp_insert_term( $data['name'], 'product_shipping_class', $data ); 
  2851. if ( is_wp_error( $insert ) ) { 
  2852. throw new WC_API_Exception( 'woocommerce_api_cannot_create_product_shipping_class', $insert->get_error_message(), 400 ); 
  2853.  
  2854. $id = $insert['term_id']; 
  2855.  
  2856. do_action( 'woocommerce_api_create_product_shipping_class', $id, $data ); 
  2857.  
  2858. $this->server->send_status( 201 ); 
  2859.  
  2860. return $this->get_product_shipping_class( $id ); 
  2861. } catch ( WC_API_Exception $e ) { 
  2862. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2863.  
  2864. /** 
  2865. * Edit a product shipping class. 
  2866. * 
  2867. * @since 2.5.0 
  2868. * @param int $id Product shipping class term ID 
  2869. * @param array $data Posted data 
  2870. * @return array|WP_Error Product shipping class if succeed, otherwise 
  2871. * WP_Error will be returned 
  2872. */ 
  2873. public function edit_product_shipping_class( $id, $data ) { 
  2874. global $wpdb; 
  2875.  
  2876. try { 
  2877. if ( ! isset( $data['product_shipping_class'] ) ) { 
  2878. throw new WC_API_Exception( 'woocommerce_api_missing_product_shipping_class', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_shipping_class' ), 400 ); 
  2879.  
  2880. $id = absint( $id ); 
  2881. $data = $data['product_shipping_class']; 
  2882.  
  2883. // Check permissions 
  2884. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2885. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_shipping_class', __( 'You do not have permission to edit product shipping classes', 'woocommerce' ), 401 ); 
  2886.  
  2887. $data = apply_filters( 'woocommerce_api_edit_product_shipping_class_data', $data, $this ); 
  2888. $shipping_class = $this->get_product_shipping_class( $id ); 
  2889.  
  2890. if ( is_wp_error( $shipping_class ) ) { 
  2891. return $shipping_class; 
  2892.  
  2893. $update = wp_update_term( $id, 'product_shipping_class', $data ); 
  2894. if ( is_wp_error( $update ) ) { 
  2895. throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_shipping_class', __( 'Could not edit the shipping class', 'woocommerce' ), 400 ); 
  2896.  
  2897. do_action( 'woocommerce_api_edit_product_shipping_class', $id, $data ); 
  2898.  
  2899. return $this->get_product_shipping_class( $id ); 
  2900. } catch ( WC_API_Exception $e ) { 
  2901. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  2902.  
  2903. /** 
  2904. * Delete a product shipping class. 
  2905. * 
  2906. * @since 2.5.0 
  2907. * @param int $id Product shipping class term ID 
  2908. * @return array|WP_Error Success message if succeed, otherwise WP_Error 
  2909. * will be returned 
  2910. */ 
  2911. public function delete_product_shipping_class( $id ) { 
  2912. global $wpdb; 
  2913.  
  2914. try { 
  2915. // Check permissions 
  2916. if ( ! current_user_can( 'manage_product_terms' ) ) { 
  2917. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_shipping_class', __( 'You do not have permission to delete product shipping classes', 'woocommerce' ), 401 ); 
  2918.  
  2919. $id = absint( $id ); 
  2920. $deleted = wp_delete_term( $id, 'product_shipping_class' ); 
  2921. if ( ! $deleted || is_wp_error( $deleted ) ) { 
  2922. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_shipping_class', __( 'Could not delete the shipping class', 'woocommerce' ), 401 ); 
  2923.  
  2924. do_action( 'woocommerce_api_delete_product_shipping_class', $id, $this ); 
  2925.  
  2926. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_shipping_class' ) ); 
  2927. } catch ( WC_API_Exception $e ) { 
  2928. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
.