class-wc-rest-products-controller.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. <?php
  2. /**
  3. * REST API Products controller
  4. *
  5. * Handles requests to the /products endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Products controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_CRUD_Controller
  16. */
  17. class WC_REST_Products_Controller extends WC_REST_Legacy_Products_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Route base.
  26. *
  27. * @var string
  28. */
  29. protected $rest_base = 'products';
  30. /**
  31. * Post type.
  32. *
  33. * @var string
  34. */
  35. protected $post_type = 'product';
  36. /**
  37. * If object is hierarchical.
  38. *
  39. * @var bool
  40. */
  41. protected $hierarchical = true;
  42. /**
  43. * Initialize product actions.
  44. */
  45. public function __construct() {
  46. add_action( "woocommerce_rest_insert_{$this->post_type}_object", array( $this, 'clear_transients' ) );
  47. }
  48. /**
  49. * Register the routes for products.
  50. */
  51. public function register_routes() {
  52. register_rest_route(
  53. $this->namespace, '/' . $this->rest_base, array(
  54. array(
  55. 'methods' => WP_REST_Server::READABLE,
  56. 'callback' => array( $this, 'get_items' ),
  57. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  58. 'args' => $this->get_collection_params(),
  59. ),
  60. array(
  61. 'methods' => WP_REST_Server::CREATABLE,
  62. 'callback' => array( $this, 'create_item' ),
  63. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  64. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
  65. ),
  66. 'schema' => array( $this, 'get_public_item_schema' ),
  67. )
  68. );
  69. register_rest_route(
  70. $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
  71. 'args' => array(
  72. 'id' => array(
  73. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  74. 'type' => 'integer',
  75. ),
  76. ),
  77. array(
  78. 'methods' => WP_REST_Server::READABLE,
  79. 'callback' => array( $this, 'get_item' ),
  80. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  81. 'args' => array(
  82. 'context' => $this->get_context_param(
  83. array(
  84. 'default' => 'view',
  85. )
  86. ),
  87. ),
  88. ),
  89. array(
  90. 'methods' => WP_REST_Server::EDITABLE,
  91. 'callback' => array( $this, 'update_item' ),
  92. 'permission_callback' => array( $this, 'update_item_permissions_check' ),
  93. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  94. ),
  95. array(
  96. 'methods' => WP_REST_Server::DELETABLE,
  97. 'callback' => array( $this, 'delete_item' ),
  98. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  99. 'args' => array(
  100. 'force' => array(
  101. 'default' => false,
  102. 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
  103. 'type' => 'boolean',
  104. ),
  105. ),
  106. ),
  107. 'schema' => array( $this, 'get_public_item_schema' ),
  108. )
  109. );
  110. register_rest_route(
  111. $this->namespace, '/' . $this->rest_base . '/batch', array(
  112. array(
  113. 'methods' => WP_REST_Server::EDITABLE,
  114. 'callback' => array( $this, 'batch_items' ),
  115. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),
  116. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  117. ),
  118. 'schema' => array( $this, 'get_public_batch_schema' ),
  119. )
  120. );
  121. }
  122. /**
  123. * Get object.
  124. *
  125. * @since 3.0.0
  126. * @param int $id Object ID.
  127. * @return WC_Data
  128. */
  129. protected function get_object( $id ) {
  130. return wc_get_product( $id );
  131. }
  132. /**
  133. * Prepare a single product output for response.
  134. *
  135. * @since 3.0.0
  136. * @param WC_Data $object Object data.
  137. * @param WP_REST_Request $request Request object.
  138. * @return WP_REST_Response
  139. */
  140. public function prepare_object_for_response( $object, $request ) {
  141. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  142. $data = $this->get_product_data( $object, $context );
  143. // Add variations to variable products.
  144. if ( $object->is_type( 'variable' ) && $object->has_child() ) {
  145. $data['variations'] = $object->get_children();
  146. }
  147. // Add grouped products data.
  148. if ( $object->is_type( 'grouped' ) && $object->has_child() ) {
  149. $data['grouped_products'] = $object->get_children();
  150. }
  151. $data = $this->add_additional_fields_to_object( $data, $request );
  152. $data = $this->filter_response_by_context( $data, $context );
  153. $response = rest_ensure_response( $data );
  154. $response->add_links( $this->prepare_links( $object, $request ) );
  155. /**
  156. * Filter the data for a response.
  157. *
  158. * The dynamic portion of the hook name, $this->post_type,
  159. * refers to object type being prepared for the response.
  160. *
  161. * @param WP_REST_Response $response The response object.
  162. * @param WC_Data $object Object data.
  163. * @param WP_REST_Request $request Request object.
  164. */
  165. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
  166. }
  167. /**
  168. * Prepare objects query.
  169. *
  170. * @since 3.0.0
  171. * @param WP_REST_Request $request Full details about the request.
  172. * @return array
  173. */
  174. protected function prepare_objects_query( $request ) {
  175. $args = parent::prepare_objects_query( $request );
  176. // Set post_status.
  177. $args['post_status'] = $request['status'];
  178. // Taxonomy query to filter products by type, category,
  179. // tag, shipping class, and attribute.
  180. $tax_query = array();
  181. // Map between taxonomy name and arg's key.
  182. $taxonomies = array(
  183. 'product_cat' => 'category',
  184. 'product_tag' => 'tag',
  185. 'product_shipping_class' => 'shipping_class',
  186. );
  187. // Set tax_query for each passed arg.
  188. foreach ( $taxonomies as $taxonomy => $key ) {
  189. if ( ! empty( $request[ $key ] ) ) {
  190. $tax_query[] = array(
  191. 'taxonomy' => $taxonomy,
  192. 'field' => 'term_id',
  193. 'terms' => $request[ $key ],
  194. );
  195. }
  196. }
  197. // Filter product type by slug.
  198. if ( ! empty( $request['type'] ) ) {
  199. $tax_query[] = array(
  200. 'taxonomy' => 'product_type',
  201. 'field' => 'slug',
  202. 'terms' => $request['type'],
  203. );
  204. }
  205. // Filter by attribute and term.
  206. if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) {
  207. if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
  208. $tax_query[] = array(
  209. 'taxonomy' => $request['attribute'],
  210. 'field' => 'term_id',
  211. 'terms' => $request['attribute_term'],
  212. );
  213. }
  214. }
  215. if ( ! empty( $tax_query ) ) {
  216. $args['tax_query'] = $tax_query; // WPCS: slow query ok.
  217. }
  218. // Filter featured.
  219. if ( is_bool( $request['featured'] ) ) {
  220. $args['tax_query'][] = array(
  221. 'taxonomy' => 'product_visibility',
  222. 'field' => 'name',
  223. 'terms' => 'featured',
  224. 'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
  225. );
  226. }
  227. // Filter by sku.
  228. if ( ! empty( $request['sku'] ) ) {
  229. $skus = explode( ',', $request['sku'] );
  230. // Include the current string as a SKU too.
  231. if ( 1 < count( $skus ) ) {
  232. $skus[] = $request['sku'];
  233. }
  234. $args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
  235. $args, array(
  236. 'key' => '_sku',
  237. 'value' => $skus,
  238. 'compare' => 'IN',
  239. )
  240. );
  241. }
  242. // Filter by tax class.
  243. if ( ! empty( $request['tax_class'] ) ) {
  244. $args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
  245. $args, array(
  246. 'key' => '_tax_class',
  247. 'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
  248. )
  249. );
  250. }
  251. // Price filter.
  252. if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) {
  253. $args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); // WPCS: slow query ok.
  254. }
  255. // Filter product in stock or out of stock.
  256. if ( is_bool( $request['in_stock'] ) ) {
  257. $args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
  258. $args, array(
  259. 'key' => '_stock_status',
  260. 'value' => true === $request['in_stock'] ? 'instock' : 'outofstock',
  261. )
  262. );
  263. }
  264. // Filter by on sale products.
  265. if ( is_bool( $request['on_sale'] ) ) {
  266. $on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in';
  267. $on_sale_ids = wc_get_product_ids_on_sale();
  268. // Use 0 when there's no on sale products to avoid return all products.
  269. $on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids;
  270. $args[ $on_sale_key ] += $on_sale_ids;
  271. }
  272. // Force the post_type argument, since it's not a user input variable.
  273. if ( ! empty( $request['sku'] ) ) {
  274. $args['post_type'] = array( 'product', 'product_variation' );
  275. } else {
  276. $args['post_type'] = $this->post_type;
  277. }
  278. return $args;
  279. }
  280. /**
  281. * Get the downloads for a product or product variation.
  282. *
  283. * @param WC_Product|WC_Product_Variation $product Product instance.
  284. * @return array
  285. */
  286. protected function get_downloads( $product ) {
  287. $downloads = array();
  288. if ( $product->is_downloadable() ) {
  289. foreach ( $product->get_downloads() as $file_id => $file ) {
  290. $downloads[] = array(
  291. 'id' => $file_id, // MD5 hash.
  292. 'name' => $file['name'],
  293. 'file' => $file['file'],
  294. );
  295. }
  296. }
  297. return $downloads;
  298. }
  299. /**
  300. * Get taxonomy terms.
  301. *
  302. * @param WC_Product $product Product instance.
  303. * @param string $taxonomy Taxonomy slug.
  304. * @return array
  305. */
  306. protected function get_taxonomy_terms( $product, $taxonomy = 'cat' ) {
  307. $terms = array();
  308. foreach ( wc_get_object_terms( $product->get_id(), 'product_' . $taxonomy ) as $term ) {
  309. $terms[] = array(
  310. 'id' => $term->term_id,
  311. 'name' => $term->name,
  312. 'slug' => $term->slug,
  313. );
  314. }
  315. return $terms;
  316. }
  317. /**
  318. * Get the images for a product or product variation.
  319. *
  320. * @param WC_Product|WC_Product_Variation $product Product instance.
  321. * @return array
  322. */
  323. protected function get_images( $product ) {
  324. $images = array();
  325. $attachment_ids = array();
  326. // Add featured image.
  327. if ( has_post_thumbnail( $product->get_id() ) ) {
  328. $attachment_ids[] = $product->get_image_id();
  329. }
  330. // Add gallery images.
  331. $attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );
  332. // Build image data.
  333. foreach ( $attachment_ids as $position => $attachment_id ) {
  334. $attachment_post = get_post( $attachment_id );
  335. if ( is_null( $attachment_post ) ) {
  336. continue;
  337. }
  338. $attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
  339. if ( ! is_array( $attachment ) ) {
  340. continue;
  341. }
  342. $images[] = array(
  343. 'id' => (int) $attachment_id,
  344. 'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
  345. 'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
  346. 'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
  347. 'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
  348. 'src' => current( $attachment ),
  349. 'name' => get_the_title( $attachment_id ),
  350. 'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
  351. 'position' => (int) $position,
  352. );
  353. }
  354. // Set a placeholder image if the product has no images set.
  355. if ( empty( $images ) ) {
  356. $images[] = array(
  357. 'id' => 0,
  358. 'date_created' => wc_rest_prepare_date_response( current_time( 'mysql' ), false ), // Default to now.
  359. 'date_created_gmt' => wc_rest_prepare_date_response( current_time( 'timestamp', true ) ), // Default to now.
  360. 'date_modified' => wc_rest_prepare_date_response( current_time( 'mysql' ), false ),
  361. 'date_modified_gmt' => wc_rest_prepare_date_response( current_time( 'timestamp', true ) ),
  362. 'src' => wc_placeholder_img_src(),
  363. 'name' => __( 'Placeholder', 'woocommerce' ),
  364. 'alt' => __( 'Placeholder', 'woocommerce' ),
  365. 'position' => 0,
  366. );
  367. }
  368. return $images;
  369. }
  370. /**
  371. * Get attribute taxonomy label.
  372. *
  373. * @deprecated 3.0.0
  374. *
  375. * @param string $name Taxonomy name.
  376. * @return string
  377. */
  378. protected function get_attribute_taxonomy_label( $name ) {
  379. $tax = get_taxonomy( $name );
  380. $labels = get_taxonomy_labels( $tax );
  381. return $labels->singular_name;
  382. }
  383. /**
  384. * Get product attribute taxonomy name.
  385. *
  386. * @since 3.0.0
  387. * @param string $slug Taxonomy name.
  388. * @param WC_Product $product Product data.
  389. * @return string
  390. */
  391. protected function get_attribute_taxonomy_name( $slug, $product ) {
  392. $attributes = $product->get_attributes();
  393. if ( ! isset( $attributes[ $slug ] ) ) {
  394. return str_replace( 'pa_', '', $slug );
  395. }
  396. $attribute = $attributes[ $slug ];
  397. // Taxonomy attribute name.
  398. if ( $attribute->is_taxonomy() ) {
  399. $taxonomy = $attribute->get_taxonomy_object();
  400. return $taxonomy->attribute_label;
  401. }
  402. // Custom product attribute name.
  403. return $attribute->get_name();
  404. }
  405. /**
  406. * Get default attributes.
  407. *
  408. * @param WC_Product $product Product instance.
  409. * @return array
  410. */
  411. protected function get_default_attributes( $product ) {
  412. $default = array();
  413. if ( $product->is_type( 'variable' ) ) {
  414. foreach ( array_filter( (array) $product->get_default_attributes(), 'strlen' ) as $key => $value ) {
  415. if ( 0 === strpos( $key, 'pa_' ) ) {
  416. $default[] = array(
  417. 'id' => wc_attribute_taxonomy_id_by_name( $key ),
  418. 'name' => $this->get_attribute_taxonomy_name( $key, $product ),
  419. 'option' => $value,
  420. );
  421. } else {
  422. $default[] = array(
  423. 'id' => 0,
  424. 'name' => $this->get_attribute_taxonomy_name( $key, $product ),
  425. 'option' => $value,
  426. );
  427. }
  428. }
  429. }
  430. return $default;
  431. }
  432. /**
  433. * Get attribute options.
  434. *
  435. * @param int $product_id Product ID.
  436. * @param array $attribute Attribute data.
  437. * @return array
  438. */
  439. protected function get_attribute_options( $product_id, $attribute ) {
  440. if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
  441. return wc_get_product_terms(
  442. $product_id, $attribute['name'], array(
  443. 'fields' => 'names',
  444. )
  445. );
  446. } elseif ( isset( $attribute['value'] ) ) {
  447. return array_map( 'trim', explode( '|', $attribute['value'] ) );
  448. }
  449. return array();
  450. }
  451. /**
  452. * Get the attributes for a product or product variation.
  453. *
  454. * @param WC_Product|WC_Product_Variation $product Product instance.
  455. * @return array
  456. */
  457. protected function get_attributes( $product ) {
  458. $attributes = array();
  459. if ( $product->is_type( 'variation' ) ) {
  460. $_product = wc_get_product( $product->get_parent_id() );
  461. foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) {
  462. $name = str_replace( 'attribute_', '', $attribute_name );
  463. if ( ! $attribute ) {
  464. continue;
  465. }
  466. // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
  467. if ( 0 === strpos( $attribute_name, 'attribute_pa_' ) ) {
  468. $option_term = get_term_by( 'slug', $attribute, $name );
  469. $attributes[] = array(
  470. 'id' => wc_attribute_taxonomy_id_by_name( $name ),
  471. 'name' => $this->get_attribute_taxonomy_name( $name, $_product ),
  472. 'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute,
  473. );
  474. } else {
  475. $attributes[] = array(
  476. 'id' => 0,
  477. 'name' => $this->get_attribute_taxonomy_name( $name, $_product ),
  478. 'option' => $attribute,
  479. );
  480. }
  481. }
  482. } else {
  483. foreach ( $product->get_attributes() as $attribute ) {
  484. $attributes[] = array(
  485. 'id' => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name( $attribute['name'] ) : 0,
  486. 'name' => $this->get_attribute_taxonomy_name( $attribute['name'], $product ),
  487. 'position' => (int) $attribute['position'],
  488. 'visible' => (bool) $attribute['is_visible'],
  489. 'variation' => (bool) $attribute['is_variation'],
  490. 'options' => $this->get_attribute_options( $product->get_id(), $attribute ),
  491. );
  492. }
  493. }
  494. return $attributes;
  495. }
  496. /**
  497. * Get product data.
  498. *
  499. * @param WC_Product $product Product instance.
  500. * @param string $context Request context.
  501. * Options: 'view' and 'edit'.
  502. * @return array
  503. */
  504. protected function get_product_data( $product, $context = 'view' ) {
  505. $data = array(
  506. 'id' => $product->get_id(),
  507. 'name' => $product->get_name( $context ),
  508. 'slug' => $product->get_slug( $context ),
  509. 'permalink' => $product->get_permalink(),
  510. 'date_created' => wc_rest_prepare_date_response( $product->get_date_created( $context ), false ),
  511. 'date_created_gmt' => wc_rest_prepare_date_response( $product->get_date_created( $context ) ),
  512. 'date_modified' => wc_rest_prepare_date_response( $product->get_date_modified( $context ), false ),
  513. 'date_modified_gmt' => wc_rest_prepare_date_response( $product->get_date_modified( $context ) ),
  514. 'type' => $product->get_type(),
  515. 'status' => $product->get_status( $context ),
  516. 'featured' => $product->is_featured(),
  517. 'catalog_visibility' => $product->get_catalog_visibility( $context ),
  518. 'description' => 'view' === $context ? wpautop( do_shortcode( $product->get_description() ) ) : $product->get_description( $context ),
  519. 'short_description' => 'view' === $context ? apply_filters( 'woocommerce_short_description', $product->get_short_description() ) : $product->get_short_description( $context ),
  520. 'sku' => $product->get_sku( $context ),
  521. 'price' => $product->get_price( $context ),
  522. 'regular_price' => $product->get_regular_price( $context ),
  523. 'sale_price' => $product->get_sale_price( $context ) ? $product->get_sale_price( $context ) : '',
  524. 'date_on_sale_from' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ), false ),
  525. 'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_from( $context ) ),
  526. 'date_on_sale_to' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ), false ),
  527. 'date_on_sale_to_gmt' => wc_rest_prepare_date_response( $product->get_date_on_sale_to( $context ) ),
  528. 'price_html' => $product->get_price_html(),
  529. 'on_sale' => $product->is_on_sale( $context ),
  530. 'purchasable' => $product->is_purchasable(),
  531. 'total_sales' => $product->get_total_sales( $context ),
  532. 'virtual' => $product->is_virtual(),
  533. 'downloadable' => $product->is_downloadable(),
  534. 'downloads' => $this->get_downloads( $product ),
  535. 'download_limit' => $product->get_download_limit( $context ),
  536. 'download_expiry' => $product->get_download_expiry( $context ),
  537. 'external_url' => $product->is_type( 'external' ) ? $product->get_product_url( $context ) : '',
  538. 'button_text' => $product->is_type( 'external' ) ? $product->get_button_text( $context ) : '',
  539. 'tax_status' => $product->get_tax_status( $context ),
  540. 'tax_class' => $product->get_tax_class( $context ),
  541. 'manage_stock' => $product->managing_stock(),
  542. 'stock_quantity' => $product->get_stock_quantity( $context ),
  543. 'in_stock' => $product->is_in_stock(),
  544. 'backorders' => $product->get_backorders( $context ),
  545. 'backorders_allowed' => $product->backorders_allowed(),
  546. 'backordered' => $product->is_on_backorder(),
  547. 'sold_individually' => $product->is_sold_individually(),
  548. 'weight' => $product->get_weight( $context ),
  549. 'dimensions' => array(
  550. 'length' => $product->get_length( $context ),
  551. 'width' => $product->get_width( $context ),
  552. 'height' => $product->get_height( $context ),
  553. ),
  554. 'shipping_required' => $product->needs_shipping(),
  555. 'shipping_taxable' => $product->is_shipping_taxable(),
  556. 'shipping_class' => $product->get_shipping_class(),
  557. 'shipping_class_id' => $product->get_shipping_class_id( $context ),
  558. 'reviews_allowed' => $product->get_reviews_allowed( $context ),
  559. 'average_rating' => 'view' === $context ? wc_format_decimal( $product->get_average_rating(), 2 ) : $product->get_average_rating( $context ),
  560. 'rating_count' => $product->get_rating_count(),
  561. 'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
  562. 'upsell_ids' => array_map( 'absint', $product->get_upsell_ids( $context ) ),
  563. 'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids( $context ) ),
  564. 'parent_id' => $product->get_parent_id( $context ),
  565. 'purchase_note' => 'view' === $context ? wpautop( do_shortcode( wp_kses_post( $product->get_purchase_note() ) ) ) : $product->get_purchase_note( $context ),
  566. 'categories' => $this->get_taxonomy_terms( $product ),
  567. 'tags' => $this->get_taxonomy_terms( $product, 'tag' ),
  568. 'images' => $this->get_images( $product ),
  569. 'attributes' => $this->get_attributes( $product ),
  570. 'default_attributes' => $this->get_default_attributes( $product ),
  571. 'variations' => array(),
  572. 'grouped_products' => array(),
  573. 'menu_order' => $product->get_menu_order( $context ),
  574. 'meta_data' => $product->get_meta_data(),
  575. );
  576. return $data;
  577. }
  578. /**
  579. * Prepare links for the request.
  580. *
  581. * @param WC_Data $object Object data.
  582. * @param WP_REST_Request $request Request object.
  583. * @return array Links for the given post.
  584. */
  585. protected function prepare_links( $object, $request ) {
  586. $links = array(
  587. 'self' => array(
  588. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
  589. ),
  590. 'collection' => array(
  591. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  592. ),
  593. );
  594. if ( $object->get_parent_id() ) {
  595. $links['up'] = array(
  596. 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $object->get_parent_id() ) ),
  597. );
  598. }
  599. return $links;
  600. }
  601. /**
  602. * Prepare a single product for create or update.
  603. *
  604. * @param WP_REST_Request $request Request object.
  605. * @param bool $creating If is creating a new object.
  606. * @return WP_Error|WC_Data
  607. */
  608. protected function prepare_object_for_database( $request, $creating = false ) {
  609. $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
  610. // Type is the most important part here because we need to be using the correct class and methods.
  611. if ( isset( $request['type'] ) ) {
  612. $classname = WC_Product_Factory::get_classname_from_product_type( $request['type'] );
  613. if ( ! class_exists( $classname ) ) {
  614. $classname = 'WC_Product_Simple';
  615. }
  616. $product = new $classname( $id );
  617. } elseif ( isset( $request['id'] ) ) {
  618. $product = wc_get_product( $id );
  619. } else {
  620. $product = new WC_Product_Simple();
  621. }
  622. if ( 'variation' === $product->get_type() ) {
  623. return new WP_Error(
  624. "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
  625. 'status' => 404,
  626. )
  627. );
  628. }
  629. // Post title.
  630. if ( isset( $request['name'] ) ) {
  631. $product->set_name( wp_filter_post_kses( $request['name'] ) );
  632. }
  633. // Post content.
  634. if ( isset( $request['description'] ) ) {
  635. $product->set_description( wp_filter_post_kses( $request['description'] ) );
  636. }
  637. // Post excerpt.
  638. if ( isset( $request['short_description'] ) ) {
  639. $product->set_short_description( wp_filter_post_kses( $request['short_description'] ) );
  640. }
  641. // Post status.
  642. if ( isset( $request['status'] ) ) {
  643. $product->set_status( get_post_status_object( $request['status'] ) ? $request['status'] : 'draft' );
  644. }
  645. // Post slug.
  646. if ( isset( $request['slug'] ) ) {
  647. $product->set_slug( $request['slug'] );
  648. }
  649. // Menu order.
  650. if ( isset( $request['menu_order'] ) ) {
  651. $product->set_menu_order( $request['menu_order'] );
  652. }
  653. // Comment status.
  654. if ( isset( $request['reviews_allowed'] ) ) {
  655. $product->set_reviews_allowed( $request['reviews_allowed'] );
  656. }
  657. // Virtual.
  658. if ( isset( $request['virtual'] ) ) {
  659. $product->set_virtual( $request['virtual'] );
  660. }
  661. // Tax status.
  662. if ( isset( $request['tax_status'] ) ) {
  663. $product->set_tax_status( $request['tax_status'] );
  664. }
  665. // Tax Class.
  666. if ( isset( $request['tax_class'] ) ) {
  667. $product->set_tax_class( $request['tax_class'] );
  668. }
  669. // Catalog Visibility.
  670. if ( isset( $request['catalog_visibility'] ) ) {
  671. $product->set_catalog_visibility( $request['catalog_visibility'] );
  672. }
  673. // Purchase Note.
  674. if ( isset( $request['purchase_note'] ) ) {
  675. $product->set_purchase_note( wp_kses_post( wp_unslash( $request['purchase_note'] ) ) );
  676. }
  677. // Featured Product.
  678. if ( isset( $request['featured'] ) ) {
  679. $product->set_featured( $request['featured'] );
  680. }
  681. // Shipping data.
  682. $product = $this->save_product_shipping_data( $product, $request );
  683. // SKU.
  684. if ( isset( $request['sku'] ) ) {
  685. $product->set_sku( wc_clean( $request['sku'] ) );
  686. }
  687. // Attributes.
  688. if ( isset( $request['attributes'] ) ) {
  689. $attributes = array();
  690. foreach ( $request['attributes'] as $attribute ) {
  691. $attribute_id = 0;
  692. $attribute_name = '';
  693. // Check ID for global attributes or name for product attributes.
  694. if ( ! empty( $attribute['id'] ) ) {
  695. $attribute_id = absint( $attribute['id'] );
  696. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
  697. } elseif ( ! empty( $attribute['name'] ) ) {
  698. $attribute_name = wc_clean( $attribute['name'] );
  699. }
  700. if ( ! $attribute_id && ! $attribute_name ) {
  701. continue;
  702. }
  703. if ( $attribute_id ) {
  704. if ( isset( $attribute['options'] ) ) {
  705. $options = $attribute['options'];
  706. if ( ! is_array( $attribute['options'] ) ) {
  707. // Text based attributes - Posted values are term names.
  708. $options = explode( WC_DELIMITER, $options );
  709. }
  710. $values = array_map( 'wc_sanitize_term_text_based', $options );
  711. $values = array_filter( $values, 'strlen' );
  712. } else {
  713. $values = array();
  714. }
  715. if ( ! empty( $values ) ) {
  716. // Add attribute to array, but don't set values.
  717. $attribute_object = new WC_Product_Attribute();
  718. $attribute_object->set_id( $attribute_id );
  719. $attribute_object->set_name( $attribute_name );
  720. $attribute_object->set_options( $values );
  721. $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
  722. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
  723. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
  724. $attributes[] = $attribute_object;
  725. }
  726. } elseif ( isset( $attribute['options'] ) ) {
  727. // Custom attribute - Add attribute to array and set the values.
  728. if ( is_array( $attribute['options'] ) ) {
  729. $values = $attribute['options'];
  730. } else {
  731. $values = explode( WC_DELIMITER, $attribute['options'] );
  732. }
  733. $attribute_object = new WC_Product_Attribute();
  734. $attribute_object->set_name( $attribute_name );
  735. $attribute_object->set_options( $values );
  736. $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' );
  737. $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 );
  738. $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 );
  739. $attributes[] = $attribute_object;
  740. }
  741. }
  742. $product->set_attributes( $attributes );
  743. }
  744. // Sales and prices.
  745. if ( in_array( $product->get_type(), array( 'variable', 'grouped' ), true ) ) {
  746. $product->set_regular_price( '' );
  747. $product->set_sale_price( '' );
  748. $product->set_date_on_sale_to( '' );
  749. $product->set_date_on_sale_from( '' );
  750. $product->set_price( '' );
  751. } else {
  752. // Regular Price.
  753. if ( isset( $request['regular_price'] ) ) {
  754. $product->set_regular_price( $request['regular_price'] );
  755. }
  756. // Sale Price.
  757. if ( isset( $request['sale_price'] ) ) {
  758. $product->set_sale_price( $request['sale_price'] );
  759. }
  760. if ( isset( $request['date_on_sale_from'] ) ) {
  761. $product->set_date_on_sale_from( $request['date_on_sale_from'] );
  762. }
  763. if ( isset( $request['date_on_sale_from_gmt'] ) ) {
  764. $product->set_date_on_sale_from( $request['date_on_sale_from_gmt'] ? strtotime( $request['date_on_sale_from_gmt'] ) : null );
  765. }
  766. if ( isset( $request['date_on_sale_to'] ) ) {
  767. $product->set_date_on_sale_to( $request['date_on_sale_to'] );
  768. }
  769. if ( isset( $request['date_on_sale_to_gmt'] ) ) {
  770. $product->set_date_on_sale_to( $request['date_on_sale_to_gmt'] ? strtotime( $request['date_on_sale_to_gmt'] ) : null );
  771. }
  772. }
  773. // Product parent ID for groups.
  774. if ( isset( $request['parent_id'] ) ) {
  775. $product->set_parent_id( $request['parent_id'] );
  776. }
  777. // Sold individually.
  778. if ( isset( $request['sold_individually'] ) ) {
  779. $product->set_sold_individually( $request['sold_individually'] );
  780. }
  781. // Stock status.
  782. if ( isset( $request['in_stock'] ) ) {
  783. $stock_status = true === $request['in_stock'] ? 'instock' : 'outofstock';
  784. } else {
  785. $stock_status = $product->get_stock_status();
  786. }
  787. // Stock data.
  788. if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
  789. // Manage stock.
  790. if ( isset( $request['manage_stock'] ) ) {
  791. $product->set_manage_stock( $request['manage_stock'] );
  792. }
  793. // Backorders.
  794. if ( isset( $request['backorders'] ) ) {
  795. $product->set_backorders( $request['backorders'] );
  796. }
  797. if ( $product->is_type( 'grouped' ) ) {
  798. $product->set_manage_stock( 'no' );
  799. $product->set_backorders( 'no' );
  800. $product->set_stock_quantity( '' );
  801. $product->set_stock_status( $stock_status );
  802. } elseif ( $product->is_type( 'external' ) ) {
  803. $product->set_manage_stock( 'no' );
  804. $product->set_backorders( 'no' );
  805. $product->set_stock_quantity( '' );
  806. $product->set_stock_status( 'instock' );
  807. } elseif ( $product->get_manage_stock() ) {
  808. // Stock status is always determined by children so sync later.
  809. if ( ! $product->is_type( 'variable' ) ) {
  810. $product->set_stock_status( $stock_status );
  811. }
  812. // Stock quantity.
  813. if ( isset( $request['stock_quantity'] ) ) {
  814. $product->set_stock_quantity( wc_stock_amount( $request['stock_quantity'] ) );
  815. } elseif ( isset( $request['inventory_delta'] ) ) {
  816. $stock_quantity = wc_stock_amount( $product->get_stock_quantity() );
  817. $stock_quantity += wc_stock_amount( $request['inventory_delta'] );
  818. $product->set_stock_quantity( wc_stock_amount( $stock_quantity ) );
  819. }
  820. } else {
  821. // Don't manage stock.
  822. $product->set_manage_stock( 'no' );
  823. $product->set_stock_quantity( '' );
  824. $product->set_stock_status( $stock_status );
  825. }
  826. } elseif ( ! $product->is_type( 'variable' ) ) {
  827. $product->set_stock_status( $stock_status );
  828. }
  829. // Upsells.
  830. if ( isset( $request['upsell_ids'] ) ) {
  831. $upsells = array();
  832. $ids = $request['upsell_ids'];
  833. if ( ! empty( $ids ) ) {
  834. foreach ( $ids as $id ) {
  835. if ( $id && $id > 0 ) {
  836. $upsells[] = $id;
  837. }
  838. }
  839. }
  840. $product->set_upsell_ids( $upsells );
  841. }
  842. // Cross sells.
  843. if ( isset( $request['cross_sell_ids'] ) ) {
  844. $crosssells = array();
  845. $ids = $request['cross_sell_ids'];
  846. if ( ! empty( $ids ) ) {
  847. foreach ( $ids as $id ) {
  848. if ( $id && $id > 0 ) {
  849. $crosssells[] = $id;
  850. }
  851. }
  852. }
  853. $product->set_cross_sell_ids( $crosssells );
  854. }
  855. // Product categories.
  856. if ( isset( $request['categories'] ) && is_array( $request['categories'] ) ) {
  857. $product = $this->save_taxonomy_terms( $product, $request['categories'] );
  858. }
  859. // Product tags.
  860. if ( isset( $request['tags'] ) && is_array( $request['tags'] ) ) {
  861. $product = $this->save_taxonomy_terms( $product, $request['tags'], 'tag' );
  862. }
  863. // Downloadable.
  864. if ( isset( $request['downloadable'] ) ) {
  865. $product->set_downloadable( $request['downloadable'] );
  866. }
  867. // Downloadable options.
  868. if ( $product->get_downloadable() ) {
  869. // Downloadable files.
  870. if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) {
  871. $product = $this->save_downloadable_files( $product, $request['downloads'] );
  872. }
  873. // Download limit.
  874. if ( isset( $request['download_limit'] ) ) {
  875. $product->set_download_limit( $request['download_limit'] );
  876. }
  877. // Download expiry.
  878. if ( isset( $request['download_expiry'] ) ) {
  879. $product->set_download_expiry( $request['download_expiry'] );
  880. }
  881. }
  882. // Product url and button text for external products.
  883. if ( $product->is_type( 'external' ) ) {
  884. if ( isset( $request['external_url'] ) ) {
  885. $product->set_product_url( $request['external_url'] );
  886. }
  887. if ( isset( $request['button_text'] ) ) {
  888. $product->set_button_text( $request['button_text'] );
  889. }
  890. }
  891. // Save default attributes for variable products.
  892. if ( $product->is_type( 'variable' ) ) {
  893. $product = $this->save_default_attributes( $product, $request );
  894. }
  895. // Set children for a grouped product.
  896. if ( $product->is_type( 'grouped' ) && isset( $request['grouped_products'] ) ) {
  897. $product->set_children( $request['grouped_products'] );
  898. }
  899. // Check for featured/gallery images, upload it and set it.
  900. if ( isset( $request['images'] ) ) {
  901. $product = $this->set_product_images( $product, $request['images'] );
  902. }
  903. // Allow set meta_data.
  904. if ( is_array( $request['meta_data'] ) ) {
  905. foreach ( $request['meta_data'] as $meta ) {
  906. $product->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
  907. }
  908. }
  909. /**
  910. * Filters an object before it is inserted via the REST API.
  911. *
  912. * The dynamic portion of the hook name, `$this->post_type`,
  913. * refers to the object type slug.
  914. *
  915. * @param WC_Data $product Object object.
  916. * @param WP_REST_Request $request Request object.
  917. * @param bool $creating If is creating a new object.
  918. */
  919. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $product, $request, $creating );
  920. }
  921. /**
  922. * Set product images.
  923. *
  924. * @throws WC_REST_Exception REST API exceptions.
  925. * @param WC_Product $product Product instance.
  926. * @param array $images Images data.
  927. * @return WC_Product
  928. */
  929. protected function set_product_images( $product, $images ) {
  930. $images = is_array( $images ) ? array_filter( $images ) : array();
  931. if ( ! empty( $images ) ) {
  932. $gallery = array();
  933. foreach ( $images as $image ) {
  934. $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
  935. if ( 0 === $attachment_id && isset( $image['src'] ) ) {
  936. $upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
  937. if ( is_wp_error( $upload ) ) {
  938. if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images ) ) {
  939. throw new WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
  940. } else {
  941. continue;
  942. }
  943. }
  944. $attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $product->get_id() );
  945. }
  946. if ( ! wp_attachment_is_image( $attachment_id ) ) {
  947. /* translators: %s: attachment id */
  948. throw new WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
  949. }
  950. if ( isset( $image['position'] ) && 0 === absint( $image['position'] ) ) {
  951. $product->set_image_id( $attachment_id );
  952. } else {
  953. $gallery[] = $attachment_id;
  954. }
  955. // Set the image alt if present.
  956. if ( ! empty( $image['alt'] ) ) {
  957. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
  958. }
  959. // Set the image name if present.
  960. if ( ! empty( $image['name'] ) ) {
  961. wp_update_post(
  962. array(
  963. 'ID' => $attachment_id,
  964. 'post_title' => $image['name'],
  965. )
  966. );
  967. }
  968. // Set the image source if present, for future reference.
  969. if ( ! empty( $image['src'] ) ) {
  970. update_post_meta( $attachment_id, '_wc_attachment_source', esc_url_raw( $image['src'] ) );
  971. }
  972. }
  973. $product->set_gallery_image_ids( $gallery );
  974. } else {
  975. $product->set_image_id( '' );
  976. $product->set_gallery_image_ids( array() );
  977. }
  978. return $product;
  979. }
  980. /**
  981. * Save product shipping data.
  982. *
  983. * @param WC_Product $product Product instance.
  984. * @param array $data Shipping data.
  985. * @return WC_Product
  986. */
  987. protected function save_product_shipping_data( $product, $data ) {
  988. // Virtual.
  989. if ( isset( $data['virtual'] ) && true === $data['virtual'] ) {
  990. $product->set_weight( '' );
  991. $product->set_height( '' );
  992. $product->set_length( '' );
  993. $product->set_width( '' );
  994. } else {
  995. if ( isset( $data['weight'] ) ) {
  996. $product->set_weight( $data['weight'] );
  997. }
  998. // Height.
  999. if ( isset( $data['dimensions']['height'] ) ) {
  1000. $product->set_height( $data['dimensions']['height'] );
  1001. }
  1002. // Width.
  1003. if ( isset( $data['dimensions']['width'] ) ) {
  1004. $product->set_width( $data['dimensions']['width'] );
  1005. }
  1006. // Length.
  1007. if ( isset( $data['dimensions']['length'] ) ) {
  1008. $product->set_length( $data['dimensions']['length'] );
  1009. }
  1010. }
  1011. // Shipping class.
  1012. if ( isset( $data['shipping_class'] ) ) {
  1013. $data_store = $product->get_data_store();
  1014. $shipping_class_id = $data_store->get_shipping_class_id_by_slug( wc_clean( $data['shipping_class'] ) );
  1015. $product->set_shipping_class_id( $shipping_class_id );
  1016. }
  1017. return $product;
  1018. }
  1019. /**
  1020. * Save downloadable files.
  1021. *
  1022. * @param WC_Product $product Product instance.
  1023. * @param array $downloads Downloads data.
  1024. * @param int $deprecated Deprecated since 3.0.
  1025. * @return WC_Product
  1026. */
  1027. protected function save_downloadable_files( $product, $downloads, $deprecated = 0 ) {
  1028. if ( $deprecated ) {
  1029. wc_deprecated_argument( 'variation_id', '3.0', 'save_downloadable_files() not requires a variation_id anymore.' );
  1030. }
  1031. $files = array();
  1032. foreach ( $downloads as $key => $file ) {
  1033. if ( empty( $file['file'] ) ) {
  1034. continue;
  1035. }
  1036. $download = new WC_Product_Download();
  1037. $download->set_id( $file['id'] ? $file['id'] : wp_generate_uuid4() );
  1038. $download->set_name( $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['file'] ) );
  1039. $download->set_file( apply_filters( 'woocommerce_file_download_path', $file['file'], $product, $key ) );
  1040. $files[] = $download;
  1041. }
  1042. $product->set_downloads( $files );
  1043. return $product;
  1044. }
  1045. /**
  1046. * Save taxonomy terms.
  1047. *
  1048. * @param WC_Product $product Product instance.
  1049. * @param array $terms Terms data.
  1050. * @param string $taxonomy Taxonomy name.
  1051. * @return WC_Product
  1052. */
  1053. protected function save_taxonomy_terms( $product, $terms, $taxonomy = 'cat' ) {
  1054. $term_ids = wp_list_pluck( $terms, 'id' );
  1055. if ( 'cat' === $taxonomy ) {
  1056. $product->set_category_ids( $term_ids );
  1057. } elseif ( 'tag' === $taxonomy ) {
  1058. $product->set_tag_ids( $term_ids );
  1059. }
  1060. return $product;
  1061. }
  1062. /**
  1063. * Save default attributes.
  1064. *
  1065. * @since 3.0.0
  1066. *
  1067. * @param WC_Product $product Product instance.
  1068. * @param WP_REST_Request $request Request data.
  1069. * @return WC_Product
  1070. */
  1071. protected function save_default_attributes( $product, $request ) {
  1072. if ( isset( $request['default_attributes'] ) && is_array( $request['default_attributes'] ) ) {
  1073. $attributes = $product->get_attributes();
  1074. $default_attributes = array();
  1075. foreach ( $request['default_attributes'] as $attribute ) {
  1076. $attribute_id = 0;
  1077. $attribute_name = '';
  1078. // Check ID for global attributes or name for product attributes.
  1079. if ( ! empty( $attribute['id'] ) ) {
  1080. $attribute_id = absint( $attribute['id'] );
  1081. $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id );
  1082. } elseif ( ! empty( $attribute['name'] ) ) {
  1083. $attribute_name = sanitize_title( $attribute['name'] );
  1084. }
  1085. if ( ! $attribute_id && ! $attribute_name ) {
  1086. continue;
  1087. }
  1088. if ( isset( $attributes[ $attribute_name ] ) ) {
  1089. $_attribute = $attributes[ $attribute_name ];
  1090. if ( $_attribute['is_variation'] ) {
  1091. $value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : '';
  1092. if ( ! empty( $_attribute['is_taxonomy'] ) ) {
  1093. // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
  1094. $term = get_term_by( 'name', $value, $attribute_name );
  1095. if ( $term && ! is_wp_error( $term ) ) {
  1096. $value = $term->slug;
  1097. } else {
  1098. $value = sanitize_title( $value );
  1099. }
  1100. }
  1101. if ( $value ) {
  1102. $default_attributes[ $attribute_name ] = $value;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. $product->set_default_attributes( $default_attributes );
  1108. }
  1109. return $product;
  1110. }
  1111. /**
  1112. * Clear caches here so in sync with any new variations/children.
  1113. *
  1114. * @param WC_Data $object Object data.
  1115. */
  1116. public function clear_transients( $object ) {
  1117. wc_delete_product_transients( $object->get_id() );
  1118. wp_cache_delete( 'product-' . $object->get_id(), 'products' );
  1119. }
  1120. /**
  1121. * Delete a single item.
  1122. *
  1123. * @param WP_REST_Request $request Full details about the request.
  1124. * @return WP_REST_Response|WP_Error
  1125. */
  1126. public function delete_item( $request ) {
  1127. $id = (int) $request['id'];
  1128. $force = (bool) $request['force'];
  1129. $object = $this->get_object( (int) $request['id'] );
  1130. $result = false;
  1131. if ( ! $object || 0 === $object->get_id() ) {
  1132. return new WP_Error(
  1133. "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array(
  1134. 'status' => 404,
  1135. )
  1136. );
  1137. }
  1138. if ( 'variation' === $object->get_type() ) {
  1139. return new WP_Error(
  1140. "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/&lt;product_id&gt;/variations/&lt;id&gt; endpoint.', 'woocommerce' ), array(
  1141. 'status' => 404,
  1142. )
  1143. );
  1144. }
  1145. $supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) );
  1146. /**
  1147. * Filter whether an object is trashable.
  1148. *
  1149. * Return false to disable trash support for the object.
  1150. *
  1151. * @param boolean $supports_trash Whether the object type support trashing.
  1152. * @param WC_Data $object The object being considered for trashing support.
  1153. */
  1154. $supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object );
  1155. if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
  1156. return new WP_Error(
  1157. /* translators: %s: post type */
  1158. "woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array(
  1159. 'status' => rest_authorization_required_code(),
  1160. )
  1161. );
  1162. }
  1163. $request->set_param( 'context', 'edit' );
  1164. $response = $this->prepare_object_for_response( $object, $request );
  1165. // If we're forcing, then delete permanently.
  1166. if ( $force ) {
  1167. if ( $object->is_type( 'variable' ) ) {
  1168. foreach ( $object->get_children() as $child_id ) {
  1169. $child = wc_get_product( $child_id );
  1170. $child->delete( true );
  1171. }
  1172. } elseif ( $object->is_type( 'grouped' ) ) {
  1173. foreach ( $object->get_children() as $child_id ) {
  1174. $child = wc_get_product( $child_id );
  1175. $child->set_parent_id( 0 );
  1176. $child->save();
  1177. }
  1178. }
  1179. $object->delete( true );
  1180. $result = 0 === $object->get_id();
  1181. } else {
  1182. // If we don't support trashing for this type, error out.
  1183. if ( ! $supports_trash ) {
  1184. return new WP_Error(
  1185. /* translators: %s: post type */
  1186. 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array(
  1187. 'status' => 501,
  1188. )
  1189. );
  1190. }
  1191. // Otherwise, only trash if we haven't already.
  1192. if ( is_callable( array( $object, 'get_status' ) ) ) {
  1193. if ( 'trash' === $object->get_status() ) {
  1194. return new WP_Error(
  1195. /* translators: %s: post type */
  1196. 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array(
  1197. 'status' => 410,
  1198. )
  1199. );
  1200. }
  1201. $object->delete();
  1202. $result = 'trash' === $object->get_status();
  1203. }
  1204. }
  1205. if ( ! $result ) {
  1206. return new WP_Error(
  1207. /* translators: %s: post type */
  1208. 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array(
  1209. 'status' => 500,
  1210. )
  1211. );
  1212. }
  1213. // Delete parent product transients.
  1214. if ( 0 !== $object->get_parent_id() ) {
  1215. wc_delete_product_transients( $object->get_parent_id() );
  1216. }
  1217. /**
  1218. * Fires after a single object is deleted or trashed via the REST API.
  1219. *
  1220. * @param WC_Data $object The deleted or trashed object.
  1221. * @param WP_REST_Response $response The response data.
  1222. * @param WP_REST_Request $request The request sent to the API.
  1223. */
  1224. do_action( "woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request );
  1225. return $response;
  1226. }
  1227. /**
  1228. * Get the Product's schema, conforming to JSON Schema.
  1229. *
  1230. * @return array
  1231. */
  1232. public function get_item_schema() {
  1233. $weight_unit = get_option( 'woocommerce_weight_unit' );
  1234. $dimension_unit = get_option( 'woocommerce_dimension_unit' );
  1235. $schema = array(
  1236. '$schema' => 'http://json-schema.org/draft-04/schema#',
  1237. 'title' => $this->post_type,
  1238. 'type' => 'object',
  1239. 'properties' => array(
  1240. 'id' => array(
  1241. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  1242. 'type' => 'integer',
  1243. 'context' => array( 'view', 'edit' ),
  1244. 'readonly' => true,
  1245. ),
  1246. 'name' => array(
  1247. 'description' => __( 'Product name.', 'woocommerce' ),
  1248. 'type' => 'string',
  1249. 'context' => array( 'view', 'edit' ),
  1250. ),
  1251. 'slug' => array(
  1252. 'description' => __( 'Product slug.', 'woocommerce' ),
  1253. 'type' => 'string',
  1254. 'context' => array( 'view', 'edit' ),
  1255. ),
  1256. 'permalink' => array(
  1257. 'description' => __( 'Product URL.', 'woocommerce' ),
  1258. 'type' => 'string',
  1259. 'format' => 'uri',
  1260. 'context' => array( 'view', 'edit' ),
  1261. 'readonly' => true,
  1262. ),
  1263. 'date_created' => array(
  1264. 'description' => __( "The date the product was created, in the site's timezone.", 'woocommerce' ),
  1265. 'type' => 'date-time',
  1266. 'context' => array( 'view', 'edit' ),
  1267. 'readonly' => true,
  1268. ),
  1269. 'date_created_gmt' => array(
  1270. 'description' => __( 'The date the product was created, as GMT.', 'woocommerce' ),
  1271. 'type' => 'date-time',
  1272. 'context' => array( 'view', 'edit' ),
  1273. 'readonly' => true,
  1274. ),
  1275. 'date_modified' => array(
  1276. 'description' => __( "The date the product was last modified, in the site's timezone.", 'woocommerce' ),
  1277. 'type' => 'date-time',
  1278. 'context' => array( 'view', 'edit' ),
  1279. 'readonly' => true,
  1280. ),
  1281. 'date_modified_gmt' => array(
  1282. 'description' => __( 'The date the product was last modified, as GMT.', 'woocommerce' ),
  1283. 'type' => 'date-time',
  1284. 'context' => array( 'view', 'edit' ),
  1285. 'readonly' => true,
  1286. ),
  1287. 'type' => array(
  1288. 'description' => __( 'Product type.', 'woocommerce' ),
  1289. 'type' => 'string',
  1290. 'default' => 'simple',
  1291. 'enum' => array_keys( wc_get_product_types() ),
  1292. 'context' => array( 'view', 'edit' ),
  1293. ),
  1294. 'status' => array(
  1295. 'description' => __( 'Product status (post status).', 'woocommerce' ),
  1296. 'type' => 'string',
  1297. 'default' => 'publish',
  1298. 'enum' => array_keys( get_post_statuses() ),
  1299. 'context' => array( 'view', 'edit' ),
  1300. ),
  1301. 'featured' => array(
  1302. 'description' => __( 'Featured product.', 'woocommerce' ),
  1303. 'type' => 'boolean',
  1304. 'default' => false,
  1305. 'context' => array( 'view', 'edit' ),
  1306. ),
  1307. 'catalog_visibility' => array(
  1308. 'description' => __( 'Catalog visibility.', 'woocommerce' ),
  1309. 'type' => 'string',
  1310. 'default' => 'visible',
  1311. 'enum' => array( 'visible', 'catalog', 'search', 'hidden' ),
  1312. 'context' => array( 'view', 'edit' ),
  1313. ),
  1314. 'description' => array(
  1315. 'description' => __( 'Product description.', 'woocommerce' ),
  1316. 'type' => 'string',
  1317. 'context' => array( 'view', 'edit' ),
  1318. ),
  1319. 'short_description' => array(
  1320. 'description' => __( 'Product short description.', 'woocommerce' ),
  1321. 'type' => 'string',
  1322. 'context' => array( 'view', 'edit' ),
  1323. ),
  1324. 'sku' => array(
  1325. 'description' => __( 'Unique identifier.', 'woocommerce' ),
  1326. 'type' => 'string',
  1327. 'context' => array( 'view', 'edit' ),
  1328. ),
  1329. 'price' => array(
  1330. 'description' => __( 'Current product price.', 'woocommerce' ),
  1331. 'type' => 'string',
  1332. 'context' => array( 'view', 'edit' ),
  1333. 'readonly' => true,
  1334. ),
  1335. 'regular_price' => array(
  1336. 'description' => __( 'Product regular price.', 'woocommerce' ),
  1337. 'type' => 'string',
  1338. 'context' => array( 'view', 'edit' ),
  1339. ),
  1340. 'sale_price' => array(
  1341. 'description' => __( 'Product sale price.', 'woocommerce' ),
  1342. 'type' => 'string',
  1343. 'context' => array( 'view', 'edit' ),
  1344. ),
  1345. 'date_on_sale_from' => array(
  1346. 'description' => __( "Start date of sale price, in the site's timezone.", 'woocommerce' ),
  1347. 'type' => 'date-time',
  1348. 'context' => array( 'view', 'edit' ),
  1349. ),
  1350. 'date_on_sale_from_gmt' => array(
  1351. 'description' => __( 'Start date of sale price, as GMT.', 'woocommerce' ),
  1352. 'type' => 'date-time',
  1353. 'context' => array( 'view', 'edit' ),
  1354. ),
  1355. 'date_on_sale_to' => array(
  1356. 'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
  1357. 'type' => 'date-time',
  1358. 'context' => array( 'view', 'edit' ),
  1359. ),
  1360. 'date_on_sale_to_gmt' => array(
  1361. 'description' => __( 'End date of sale price, as GMT.', 'woocommerce' ),
  1362. 'type' => 'date-time',
  1363. 'context' => array( 'view', 'edit' ),
  1364. ),
  1365. 'price_html' => array(
  1366. 'description' => __( 'Price formatted in HTML.', 'woocommerce' ),
  1367. 'type' => 'string',
  1368. 'context' => array( 'view', 'edit' ),
  1369. 'readonly' => true,
  1370. ),
  1371. 'on_sale' => array(
  1372. 'description' => __( 'Shows if the product is on sale.', 'woocommerce' ),
  1373. 'type' => 'boolean',
  1374. 'context' => array( 'view', 'edit' ),
  1375. 'readonly' => true,
  1376. ),
  1377. 'purchasable' => array(
  1378. 'description' => __( 'Shows if the product can be bought.', 'woocommerce' ),
  1379. 'type' => 'boolean',
  1380. 'context' => array( 'view', 'edit' ),
  1381. 'readonly' => true,
  1382. ),
  1383. 'total_sales' => array(
  1384. 'description' => __( 'Amount of sales.', 'woocommerce' ),
  1385. 'type' => 'integer',
  1386. 'context' => array( 'view', 'edit' ),
  1387. 'readonly' => true,
  1388. ),
  1389. 'virtual' => array(
  1390. 'description' => __( 'If the product is virtual.', 'woocommerce' ),
  1391. 'type' => 'boolean',
  1392. 'default' => false,
  1393. 'context' => array( 'view', 'edit' ),
  1394. ),
  1395. 'downloadable' => array(
  1396. 'description' => __( 'If the product is downloadable.', 'woocommerce' ),
  1397. 'type' => 'boolean',
  1398. 'default' => false,
  1399. 'context' => array( 'view', 'edit' ),
  1400. ),
  1401. 'downloads' => array(
  1402. 'description' => __( 'List of downloadable files.', 'woocommerce' ),
  1403. 'type' => 'array',
  1404. 'context' => array( 'view', 'edit' ),
  1405. 'items' => array(
  1406. 'type' => 'object',
  1407. 'properties' => array(
  1408. 'id' => array(
  1409. 'description' => __( 'File MD5 hash.', 'woocommerce' ),
  1410. 'type' => 'string',
  1411. 'context' => array( 'view', 'edit' ),
  1412. 'readonly' => true,
  1413. ),
  1414. 'name' => array(
  1415. 'description' => __( 'File name.', 'woocommerce' ),
  1416. 'type' => 'string',
  1417. 'context' => array( 'view', 'edit' ),
  1418. ),
  1419. 'file' => array(
  1420. 'description' => __( 'File URL.', 'woocommerce' ),
  1421. 'type' => 'string',
  1422. 'context' => array( 'view', 'edit' ),
  1423. ),
  1424. ),
  1425. ),
  1426. ),
  1427. 'download_limit' => array(
  1428. 'description' => __( 'Number of times downloadable files can be downloaded after purchase.', 'woocommerce' ),
  1429. 'type' => 'integer',
  1430. 'default' => -1,
  1431. 'context' => array( 'view', 'edit' ),
  1432. ),
  1433. 'download_expiry' => array(
  1434. 'description' => __( 'Number of days until access to downloadable files expires.', 'woocommerce' ),
  1435. 'type' => 'integer',
  1436. 'default' => -1,
  1437. 'context' => array( 'view', 'edit' ),
  1438. ),
  1439. 'external_url' => array(
  1440. 'description' => __( 'Product external URL. Only for external products.', 'woocommerce' ),
  1441. 'type' => 'string',
  1442. 'format' => 'uri',
  1443. 'context' => array( 'view', 'edit' ),
  1444. ),
  1445. 'button_text' => array(
  1446. 'description' => __( 'Product external button text. Only for external products.', 'woocommerce' ),
  1447. 'type' => 'string',
  1448. 'context' => array( 'view', 'edit' ),
  1449. ),
  1450. 'tax_status' => array(
  1451. 'description' => __( 'Tax status.', 'woocommerce' ),
  1452. 'type' => 'string',
  1453. 'default' => 'taxable',
  1454. 'enum' => array( 'taxable', 'shipping', 'none' ),
  1455. 'context' => array( 'view', 'edit' ),
  1456. ),
  1457. 'tax_class' => array(
  1458. 'description' => __( 'Tax class.', 'woocommerce' ),
  1459. 'type' => 'string',
  1460. 'context' => array( 'view', 'edit' ),
  1461. ),
  1462. 'manage_stock' => array(
  1463. 'description' => __( 'Stock management at product level.', 'woocommerce' ),
  1464. 'type' => 'boolean',
  1465. 'default' => false,
  1466. 'context' => array( 'view', 'edit' ),
  1467. ),
  1468. 'stock_quantity' => array(
  1469. 'description' => __( 'Stock quantity.', 'woocommerce' ),
  1470. 'type' => 'integer',
  1471. 'context' => array( 'view', 'edit' ),
  1472. ),
  1473. 'in_stock' => array(
  1474. 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ),
  1475. 'type' => 'boolean',
  1476. 'default' => true,
  1477. 'context' => array( 'view', 'edit' ),
  1478. ),
  1479. 'backorders' => array(
  1480. 'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ),
  1481. 'type' => 'string',
  1482. 'default' => 'no',
  1483. 'enum' => array( 'no', 'notify', 'yes' ),
  1484. 'context' => array( 'view', 'edit' ),
  1485. ),
  1486. 'backorders_allowed' => array(
  1487. 'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ),
  1488. 'type' => 'boolean',
  1489. 'context' => array( 'view', 'edit' ),
  1490. 'readonly' => true,
  1491. ),
  1492. 'backordered' => array(
  1493. 'description' => __( 'Shows if the product is on backordered.', 'woocommerce' ),
  1494. 'type' => 'boolean',
  1495. 'context' => array( 'view', 'edit' ),
  1496. 'readonly' => true,
  1497. ),
  1498. 'sold_individually' => array(
  1499. 'description' => __( 'Allow one item to be bought in a single order.', 'woocommerce' ),
  1500. 'type' => 'boolean',
  1501. 'default' => false,
  1502. 'context' => array( 'view', 'edit' ),
  1503. ),
  1504. 'weight' => array(
  1505. /* translators: %s: weight unit */
  1506. 'description' => sprintf( __( 'Product weight (%s).', 'woocommerce' ), $weight_unit ),
  1507. 'type' => 'string',
  1508. 'context' => array( 'view', 'edit' ),
  1509. ),
  1510. 'dimensions' => array(
  1511. 'description' => __( 'Product dimensions.', 'woocommerce' ),
  1512. 'type' => 'object',
  1513. 'context' => array( 'view', 'edit' ),
  1514. 'properties' => array(
  1515. 'length' => array(
  1516. /* translators: %s: dimension unit */
  1517. 'description' => sprintf( __( 'Product length (%s).', 'woocommerce' ), $dimension_unit ),
  1518. 'type' => 'string',
  1519. 'context' => array( 'view', 'edit' ),
  1520. ),
  1521. 'width' => array(
  1522. /* translators: %s: dimension unit */
  1523. 'description' => sprintf( __( 'Product width (%s).', 'woocommerce' ), $dimension_unit ),
  1524. 'type' => 'string',
  1525. 'context' => array( 'view', 'edit' ),
  1526. ),
  1527. 'height' => array(
  1528. /* translators: %s: dimension unit */
  1529. 'description' => sprintf( __( 'Product height (%s).', 'woocommerce' ), $dimension_unit ),
  1530. 'type' => 'string',
  1531. 'context' => array( 'view', 'edit' ),
  1532. ),
  1533. ),
  1534. ),
  1535. 'shipping_required' => array(
  1536. 'description' => __( 'Shows if the product need to be shipped.', 'woocommerce' ),
  1537. 'type' => 'boolean',
  1538. 'context' => array( 'view', 'edit' ),
  1539. 'readonly' => true,
  1540. ),
  1541. 'shipping_taxable' => array(
  1542. 'description' => __( 'Shows whether or not the product shipping is taxable.', 'woocommerce' ),
  1543. 'type' => 'boolean',
  1544. 'context' => array( 'view', 'edit' ),
  1545. 'readonly' => true,
  1546. ),
  1547. 'shipping_class' => array(
  1548. 'description' => __( 'Shipping class slug.', 'woocommerce' ),
  1549. 'type' => 'string',
  1550. 'context' => array( 'view', 'edit' ),
  1551. ),
  1552. 'shipping_class_id' => array(
  1553. 'description' => __( 'Shipping class ID.', 'woocommerce' ),
  1554. 'type' => 'integer',
  1555. 'context' => array( 'view', 'edit' ),
  1556. 'readonly' => true,
  1557. ),
  1558. 'reviews_allowed' => array(
  1559. 'description' => __( 'Allow reviews.', 'woocommerce' ),
  1560. 'type' => 'boolean',
  1561. 'default' => true,
  1562. 'context' => array( 'view', 'edit' ),
  1563. ),
  1564. 'average_rating' => array(
  1565. 'description' => __( 'Reviews average rating.', 'woocommerce' ),
  1566. 'type' => 'string',
  1567. 'context' => array( 'view', 'edit' ),
  1568. 'readonly' => true,
  1569. ),
  1570. 'rating_count' => array(
  1571. 'description' => __( 'Amount of reviews that the product have.', 'woocommerce' ),
  1572. 'type' => 'integer',
  1573. 'context' => array( 'view', 'edit' ),
  1574. 'readonly' => true,
  1575. ),
  1576. 'related_ids' => array(
  1577. 'description' => __( 'List of related products IDs.', 'woocommerce' ),
  1578. 'type' => 'array',
  1579. 'items' => array(
  1580. 'type' => 'integer',
  1581. ),
  1582. 'context' => array( 'view', 'edit' ),
  1583. 'readonly' => true,
  1584. ),
  1585. 'upsell_ids' => array(
  1586. 'description' => __( 'List of up-sell products IDs.', 'woocommerce' ),
  1587. 'type' => 'array',
  1588. 'items' => array(
  1589. 'type' => 'integer',
  1590. ),
  1591. 'context' => array( 'view', 'edit' ),
  1592. ),
  1593. 'cross_sell_ids' => array(
  1594. 'description' => __( 'List of cross-sell products IDs.', 'woocommerce' ),
  1595. 'type' => 'array',
  1596. 'items' => array(
  1597. 'type' => 'integer',
  1598. ),
  1599. 'context' => array( 'view', 'edit' ),
  1600. ),
  1601. 'parent_id' => array(
  1602. 'description' => __( 'Product parent ID.', 'woocommerce' ),
  1603. 'type' => 'integer',
  1604. 'context' => array( 'view', 'edit' ),
  1605. ),
  1606. 'purchase_note' => array(
  1607. 'description' => __( 'Optional note to send the customer after purchase.', 'woocommerce' ),
  1608. 'type' => 'string',
  1609. 'context' => array( 'view', 'edit' ),
  1610. ),
  1611. 'categories' => array(
  1612. 'description' => __( 'List of categories.', 'woocommerce' ),
  1613. 'type' => 'array',
  1614. 'context' => array( 'view', 'edit' ),
  1615. 'items' => array(
  1616. 'type' => 'object',
  1617. 'properties' => array(
  1618. 'id' => array(
  1619. 'description' => __( 'Category ID.', 'woocommerce' ),
  1620. 'type' => 'integer',
  1621. 'context' => array( 'view', 'edit' ),
  1622. ),
  1623. 'name' => array(
  1624. 'description' => __( 'Category name.', 'woocommerce' ),
  1625. 'type' => 'string',
  1626. 'context' => array( 'view', 'edit' ),
  1627. 'readonly' => true,
  1628. ),
  1629. 'slug' => array(
  1630. 'description' => __( 'Category slug.', 'woocommerce' ),
  1631. 'type' => 'string',
  1632. 'context' => array( 'view', 'edit' ),
  1633. 'readonly' => true,
  1634. ),
  1635. ),
  1636. ),
  1637. ),
  1638. 'tags' => array(
  1639. 'description' => __( 'List of tags.', 'woocommerce' ),
  1640. 'type' => 'array',
  1641. 'context' => array( 'view', 'edit' ),
  1642. 'items' => array(
  1643. 'type' => 'object',
  1644. 'properties' => array(
  1645. 'id' => array(
  1646. 'description' => __( 'Tag ID.', 'woocommerce' ),
  1647. 'type' => 'integer',
  1648. 'context' => array( 'view', 'edit' ),
  1649. ),
  1650. 'name' => array(
  1651. 'description' => __( 'Tag name.', 'woocommerce' ),
  1652. 'type' => 'string',
  1653. 'context' => array( 'view', 'edit' ),
  1654. 'readonly' => true,
  1655. ),
  1656. 'slug' => array(
  1657. 'description' => __( 'Tag slug.', 'woocommerce' ),
  1658. 'type' => 'string',
  1659. 'context' => array( 'view', 'edit' ),
  1660. 'readonly' => true,
  1661. ),
  1662. ),
  1663. ),
  1664. ),
  1665. 'images' => array(
  1666. 'description' => __( 'List of images.', 'woocommerce' ),
  1667. 'type' => 'array',
  1668. 'context' => array( 'view', 'edit' ),
  1669. 'items' => array(
  1670. 'type' => 'object',
  1671. 'properties' => array(
  1672. 'id' => array(
  1673. 'description' => __( 'Image ID.', 'woocommerce' ),
  1674. 'type' => 'integer',
  1675. 'context' => array( 'view', 'edit' ),
  1676. ),
  1677. 'date_created' => array(
  1678. 'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
  1679. 'type' => 'date-time',
  1680. 'context' => array( 'view', 'edit' ),
  1681. 'readonly' => true,
  1682. ),
  1683. 'date_created_gmt' => array(
  1684. 'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),
  1685. 'type' => 'date-time',
  1686. 'context' => array( 'view', 'edit' ),
  1687. 'readonly' => true,
  1688. ),
  1689. 'date_modified' => array(
  1690. 'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
  1691. 'type' => 'date-time',
  1692. 'context' => array( 'view', 'edit' ),
  1693. 'readonly' => true,
  1694. ),
  1695. 'date_modified_gmt' => array(
  1696. 'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),
  1697. 'type' => 'date-time',
  1698. 'context' => array( 'view', 'edit' ),
  1699. 'readonly' => true,
  1700. ),
  1701. 'src' => array(
  1702. 'description' => __( 'Image URL.', 'woocommerce' ),
  1703. 'type' => 'string',
  1704. 'format' => 'uri',
  1705. 'context' => array( 'view', 'edit' ),
  1706. ),
  1707. 'name' => array(
  1708. 'description' => __( 'Image name.', 'woocommerce' ),
  1709. 'type' => 'string',
  1710. 'context' => array( 'view', 'edit' ),
  1711. ),
  1712. 'alt' => array(
  1713. 'description' => __( 'Image alternative text.', 'woocommerce' ),
  1714. 'type' => 'string',
  1715. 'context' => array( 'view', 'edit' ),
  1716. ),
  1717. 'position' => array(
  1718. 'description' => __( 'Image position. 0 means that the image is featured.', 'woocommerce' ),
  1719. 'type' => 'integer',
  1720. 'context' => array( 'view', 'edit' ),
  1721. ),
  1722. ),
  1723. ),
  1724. ),
  1725. 'attributes' => array(
  1726. 'description' => __( 'List of attributes.', 'woocommerce' ),
  1727. 'type' => 'array',
  1728. 'context' => array( 'view', 'edit' ),
  1729. 'items' => array(
  1730. 'type' => 'object',
  1731. 'properties' => array(
  1732. 'id' => array(
  1733. 'description' => __( 'Attribute ID.', 'woocommerce' ),
  1734. 'type' => 'integer',
  1735. 'context' => array( 'view', 'edit' ),
  1736. ),
  1737. 'name' => array(
  1738. 'description' => __( 'Attribute name.', 'woocommerce' ),
  1739. 'type' => 'string',
  1740. 'context' => array( 'view', 'edit' ),
  1741. ),
  1742. 'position' => array(
  1743. 'description' => __( 'Attribute position.', 'woocommerce' ),
  1744. 'type' => 'integer',
  1745. 'context' => array( 'view', 'edit' ),
  1746. ),
  1747. 'visible' => array(
  1748. 'description' => __( "Define if the attribute is visible on the \"Additional information\" tab in the product's page.", 'woocommerce' ),
  1749. 'type' => 'boolean',
  1750. 'default' => false,
  1751. 'context' => array( 'view', 'edit' ),
  1752. ),
  1753. 'variation' => array(
  1754. 'description' => __( 'Define if the attribute can be used as variation.', 'woocommerce' ),
  1755. 'type' => 'boolean',
  1756. 'default' => false,
  1757. 'context' => array( 'view', 'edit' ),
  1758. ),
  1759. 'options' => array(
  1760. 'description' => __( 'List of available term names of the attribute.', 'woocommerce' ),
  1761. 'type' => 'array',
  1762. 'context' => array( 'view', 'edit' ),
  1763. 'items' => array(
  1764. 'type' => 'string',
  1765. ),
  1766. ),
  1767. ),
  1768. ),
  1769. ),
  1770. 'default_attributes' => array(
  1771. 'description' => __( 'Defaults variation attributes.', 'woocommerce' ),
  1772. 'type' => 'array',
  1773. 'context' => array( 'view', 'edit' ),
  1774. 'items' => array(
  1775. 'type' => 'object',
  1776. 'properties' => array(
  1777. 'id' => array(
  1778. 'description' => __( 'Attribute ID.', 'woocommerce' ),
  1779. 'type' => 'integer',
  1780. 'context' => array( 'view', 'edit' ),
  1781. ),
  1782. 'name' => array(
  1783. 'description' => __( 'Attribute name.', 'woocommerce' ),
  1784. 'type' => 'string',
  1785. 'context' => array( 'view', 'edit' ),
  1786. ),
  1787. 'option' => array(
  1788. 'description' => __( 'Selected attribute term name.', 'woocommerce' ),
  1789. 'type' => 'string',
  1790. 'context' => array( 'view', 'edit' ),
  1791. ),
  1792. ),
  1793. ),
  1794. ),
  1795. 'variations' => array(
  1796. 'description' => __( 'List of variations IDs.', 'woocommerce' ),
  1797. 'type' => 'array',
  1798. 'context' => array( 'view', 'edit' ),
  1799. 'items' => array(
  1800. 'type' => 'integer',
  1801. ),
  1802. 'readonly' => true,
  1803. ),
  1804. 'grouped_products' => array(
  1805. 'description' => __( 'List of grouped products ID.', 'woocommerce' ),
  1806. 'type' => 'array',
  1807. 'items' => array(
  1808. 'type' => 'integer',
  1809. ),
  1810. 'context' => array( 'view', 'edit' ),
  1811. ),
  1812. 'menu_order' => array(
  1813. 'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
  1814. 'type' => 'integer',
  1815. 'context' => array( 'view', 'edit' ),
  1816. ),
  1817. 'meta_data' => array(
  1818. 'description' => __( 'Meta data.', 'woocommerce' ),
  1819. 'type' => 'array',
  1820. 'context' => array( 'view', 'edit' ),
  1821. 'items' => array(
  1822. 'type' => 'object',
  1823. 'properties' => array(
  1824. 'id' => array(
  1825. 'description' => __( 'Meta ID.', 'woocommerce' ),
  1826. 'type' => 'integer',
  1827. 'context' => array( 'view', 'edit' ),
  1828. 'readonly' => true,
  1829. ),
  1830. 'key' => array(
  1831. 'description' => __( 'Meta key.', 'woocommerce' ),
  1832. 'type' => 'string',
  1833. 'context' => array( 'view', 'edit' ),
  1834. ),
  1835. 'value' => array(
  1836. 'description' => __( 'Meta value.', 'woocommerce' ),
  1837. 'type' => 'mixed',
  1838. 'context' => array( 'view', 'edit' ),
  1839. ),
  1840. ),
  1841. ),
  1842. ),
  1843. ),
  1844. );
  1845. return $this->add_additional_fields_schema( $schema );
  1846. }
  1847. /**
  1848. * Get the query params for collections of attachments.
  1849. *
  1850. * @return array
  1851. */
  1852. public function get_collection_params() {
  1853. $params = parent::get_collection_params();
  1854. $params['slug'] = array(
  1855. 'description' => __( 'Limit result set to products with a specific slug.', 'woocommerce' ),
  1856. 'type' => 'string',
  1857. 'validate_callback' => 'rest_validate_request_arg',
  1858. );
  1859. $params['status'] = array(
  1860. 'default' => 'any',
  1861. 'description' => __( 'Limit result set to products assigned a specific status.', 'woocommerce' ),
  1862. 'type' => 'string',
  1863. 'enum' => array_merge( array( 'any' ), array_keys( get_post_statuses() ) ),
  1864. 'sanitize_callback' => 'sanitize_key',
  1865. 'validate_callback' => 'rest_validate_request_arg',
  1866. );
  1867. $params['type'] = array(
  1868. 'description' => __( 'Limit result set to products assigned a specific type.', 'woocommerce' ),
  1869. 'type' => 'string',
  1870. 'enum' => array_keys( wc_get_product_types() ),
  1871. 'sanitize_callback' => 'sanitize_key',
  1872. 'validate_callback' => 'rest_validate_request_arg',
  1873. );
  1874. $params['sku'] = array(
  1875. 'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce' ),
  1876. 'type' => 'string',
  1877. 'sanitize_callback' => 'sanitize_text_field',
  1878. 'validate_callback' => 'rest_validate_request_arg',
  1879. );
  1880. $params['featured'] = array(
  1881. 'description' => __( 'Limit result set to featured products.', 'woocommerce' ),
  1882. 'type' => 'boolean',
  1883. 'sanitize_callback' => 'wc_string_to_bool',
  1884. 'validate_callback' => 'rest_validate_request_arg',
  1885. );
  1886. $params['category'] = array(
  1887. 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ),
  1888. 'type' => 'string',
  1889. 'sanitize_callback' => 'wp_parse_id_list',
  1890. 'validate_callback' => 'rest_validate_request_arg',
  1891. );
  1892. $params['tag'] = array(
  1893. 'description' => __( 'Limit result set to products assigned a specific tag ID.', 'woocommerce' ),
  1894. 'type' => 'string',
  1895. 'sanitize_callback' => 'wp_parse_id_list',
  1896. 'validate_callback' => 'rest_validate_request_arg',
  1897. );
  1898. $params['shipping_class'] = array(
  1899. 'description' => __( 'Limit result set to products assigned a specific shipping class ID.', 'woocommerce' ),
  1900. 'type' => 'string',
  1901. 'sanitize_callback' => 'wp_parse_id_list',
  1902. 'validate_callback' => 'rest_validate_request_arg',
  1903. );
  1904. $params['attribute'] = array(
  1905. 'description' => __( 'Limit result set to products with a specific attribute. Use the taxonomy name/attribute slug.', 'woocommerce' ),
  1906. 'type' => 'string',
  1907. 'sanitize_callback' => 'sanitize_text_field',
  1908. 'validate_callback' => 'rest_validate_request_arg',
  1909. );
  1910. $params['attribute_term'] = array(
  1911. 'description' => __( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'woocommerce' ),
  1912. 'type' => 'string',
  1913. 'sanitize_callback' => 'wp_parse_id_list',
  1914. 'validate_callback' => 'rest_validate_request_arg',
  1915. );
  1916. if ( wc_tax_enabled() ) {
  1917. $params['tax_class'] = array(
  1918. 'description' => __( 'Limit result set to products with a specific tax class.', 'woocommerce' ),
  1919. 'type' => 'string',
  1920. 'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
  1921. 'sanitize_callback' => 'sanitize_text_field',
  1922. 'validate_callback' => 'rest_validate_request_arg',
  1923. );
  1924. }
  1925. $params['in_stock'] = array(
  1926. 'description' => __( 'Limit result set to products in stock or out of stock.', 'woocommerce' ),
  1927. 'type' => 'boolean',
  1928. 'sanitize_callback' => 'wc_string_to_bool',
  1929. 'validate_callback' => 'rest_validate_request_arg',
  1930. );
  1931. $params['on_sale'] = array(
  1932. 'description' => __( 'Limit result set to products on sale.', 'woocommerce' ),
  1933. 'type' => 'boolean',
  1934. 'sanitize_callback' => 'wc_string_to_bool',
  1935. 'validate_callback' => 'rest_validate_request_arg',
  1936. );
  1937. $params['min_price'] = array(
  1938. 'description' => __( 'Limit result set to products based on a minimum price.', 'woocommerce' ),
  1939. 'type' => 'string',
  1940. 'sanitize_callback' => 'sanitize_text_field',
  1941. 'validate_callback' => 'rest_validate_request_arg',
  1942. );
  1943. $params['max_price'] = array(
  1944. 'description' => __( 'Limit result set to products based on a maximum price.', 'woocommerce' ),
  1945. 'type' => 'string',
  1946. 'sanitize_callback' => 'sanitize_text_field',
  1947. 'validate_callback' => 'rest_validate_request_arg',
  1948. );
  1949. return $params;
  1950. }
  1951. }