|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Convert data in the product schema format to a product object. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WooCommerce/RestApi |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4\Requests; |
|
9
|
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) || exit; |
|
11
|
|
|
|
|
12
|
|
|
use \WooCommerce\RestApi\Utilities\ImageAttachment; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* ProductVariationRequest class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ProductVariationRequest extends ProductRequest { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Convert request to object. |
|
21
|
|
|
* |
|
22
|
|
|
* @throws \WC_REST_Exception Will throw an exception if the resulting product object is invalid. |
|
23
|
|
|
* @return \WC_Product_Variation |
|
24
|
|
|
*/ |
|
25
|
|
|
public function prepare_object() { |
|
26
|
|
|
$id = (int) $this->get_param( 'id', 0 ); |
|
27
|
|
|
$object = new \WC_Product_Variation( $id ); |
|
28
|
|
|
$object->set_parent_id( (int) $this->get_param( 'product_id', 0 ) ); |
|
29
|
|
|
$parent = wc_get_product( $object->get_parent_id() ); |
|
30
|
|
|
|
|
31
|
|
|
if ( ! $parent ) { |
|
|
|
|
|
|
32
|
|
|
throw new \WC_REST_Exception( 'woocommerce_rest_product_variation_invalid_parent', __( 'Invalid parent product.', 'woocommerce' ), 404 ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->set_common_props( $object ); |
|
36
|
|
|
$this->set_meta_data( $object ); |
|
37
|
|
|
|
|
38
|
|
|
if ( $object->get_downloadable() ) { |
|
39
|
|
|
$this->set_downloadable_props( $object ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $object; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set common product props. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \WC_Product_Variation $object Product object reference. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function set_common_props( &$object ) { |
|
51
|
|
|
$props = [ |
|
52
|
|
|
'status', |
|
53
|
|
|
'sku', |
|
54
|
|
|
'virtual', |
|
55
|
|
|
'downloadable', |
|
56
|
|
|
'download_limit', |
|
57
|
|
|
'download_expiry', |
|
58
|
|
|
'manage_stock', |
|
59
|
|
|
'stock_status', |
|
60
|
|
|
'backorders', |
|
61
|
|
|
'regular_price', |
|
62
|
|
|
'sale_price', |
|
63
|
|
|
'date_on_sale_from', |
|
64
|
|
|
'date_on_sale_from_gmt', |
|
65
|
|
|
'date_on_sale_to', |
|
66
|
|
|
'date_on_sale_to_gmt', |
|
67
|
|
|
'tax_class', |
|
68
|
|
|
'description', |
|
69
|
|
|
'menu_order', |
|
70
|
|
|
'stock_quantity', |
|
71
|
|
|
'image', |
|
72
|
|
|
'downloads', |
|
73
|
|
|
'attributes', |
|
74
|
|
|
'weight', |
|
75
|
|
|
'dimensions', |
|
76
|
|
|
'shipping_class', |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
|
80
|
|
|
$prop_values = []; |
|
81
|
|
|
|
|
82
|
|
|
foreach ( $request_props as $prop => $value ) { |
|
83
|
|
|
switch ( $prop ) { |
|
84
|
|
|
case 'image': |
|
85
|
|
|
$prop_values['image_id'] = $this->parse_image_field( $value, $object ); |
|
86
|
|
|
break; |
|
87
|
|
|
case 'attributes': |
|
88
|
|
|
$prop_values['attributes'] = $this->parse_attributes_field( $value, $object ); |
|
89
|
|
|
break; |
|
90
|
|
|
case 'dimensions': |
|
91
|
|
|
$dimensions = $this->parse_dimensions_fields( $value ); |
|
92
|
|
|
$prop_values = array_merge( $prop_values, $dimensions ); |
|
93
|
|
|
break; |
|
94
|
|
|
case 'shipping_class': |
|
95
|
|
|
$prop_values['shipping_class_id'] = $this->parse_shipping_class( $value, $object ); |
|
96
|
|
|
break; |
|
97
|
|
|
default: |
|
98
|
|
|
$prop_values[ $prop ] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ( $prop_values as $prop => $value ) { |
|
103
|
|
|
$object->{"set_$prop"}( $value ); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set product object's attributes. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $raw_attributes Attribute data from request. |
|
111
|
|
|
* @param \WC_Product_Variation $object Product object. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function parse_attributes_field( $raw_attributes, $object = null ) { |
|
114
|
|
|
$attributes = array(); |
|
115
|
|
|
$parent = wc_get_product( $object->get_parent_id() ); |
|
|
|
|
|
|
116
|
|
|
$parent_attributes = $parent->get_attributes(); |
|
117
|
|
|
|
|
118
|
|
|
foreach ( $raw_attributes as $attribute ) { |
|
119
|
|
|
$attribute_id = 0; |
|
120
|
|
|
$attribute_name = ''; |
|
121
|
|
|
|
|
122
|
|
|
// Check ID for global attributes or name for product attributes. |
|
123
|
|
|
if ( ! empty( $attribute['id'] ) ) { |
|
124
|
|
|
$attribute_id = absint( $attribute['id'] ); |
|
125
|
|
|
$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); |
|
126
|
|
|
} elseif ( ! empty( $attribute['name'] ) ) { |
|
127
|
|
|
$attribute_name = sanitize_title( $attribute['name'] ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ( ! $attribute_id && ! $attribute_name ) { |
|
131
|
|
|
continue; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) { |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() ); |
|
139
|
|
|
$attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; |
|
140
|
|
|
|
|
141
|
|
|
if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) { |
|
142
|
|
|
// If dealing with a taxonomy, we need to get the slug from the name posted to the API. |
|
143
|
|
|
$term = get_term_by( 'name', $attribute_value, $attribute_name ); |
|
144
|
|
|
|
|
145
|
|
|
if ( $term && ! is_wp_error( $term ) ) { |
|
146
|
|
|
$attribute_value = $term->slug; |
|
147
|
|
|
} else { |
|
148
|
|
|
$attribute_value = sanitize_title( $attribute_value ); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$attributes[ $attribute_key ] = $attribute_value; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $attributes; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set product images. |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \WC_REST_Exception REST API exceptions. |
|
162
|
|
|
* @param array $image Image data. |
|
163
|
|
|
* @param \WC_Product_Variation $object Product object. |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function parse_image_field( $image, $object ) { |
|
167
|
|
|
if ( empty( $image ) ) { |
|
168
|
|
|
return ''; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; |
|
172
|
|
|
$attachment = new ImageAttachment( $attachment_id, $object->get_id() ); |
|
173
|
|
|
|
|
174
|
|
|
if ( 0 === $attachment->id && ! empty( $image['src'] ) ) { |
|
175
|
|
|
$attachment->upload_image_from_src( $image['src'] ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if ( ! empty( $image['alt'] ) ) { |
|
179
|
|
|
$attachment->update_alt_text( $image['alt'] ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if ( ! empty( $image['name'] ) ) { |
|
183
|
|
|
$attachment->update_name( $image['name'] ); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $attachment->id; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|