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