|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Product helpers. |
|
4
|
|
|
* |
|
5
|
|
|
* @package woocommerce/tests |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class WC_Helper_Product. |
|
10
|
|
|
* |
|
11
|
|
|
* This helper class should ONLY be used for unit tests!. |
|
12
|
|
|
*/ |
|
13
|
|
|
class WC_Helper_Product { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Delete a product. |
|
17
|
|
|
* |
|
18
|
|
|
* @param int $product_id ID to delete. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function delete_product( $product_id ) { |
|
21
|
|
|
$product = wc_get_product( $product_id ); |
|
22
|
|
|
if ( $product ) { |
|
23
|
|
|
$product->delete( true ); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create simple product. |
|
29
|
|
|
* |
|
30
|
|
|
* @since 2.3 |
|
31
|
|
|
* @param bool $save Save or return object. |
|
32
|
|
|
* @return WC_Product_Simple |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function create_simple_product( $save = true ) { |
|
35
|
|
|
$product = new WC_Product_Simple(); |
|
36
|
|
|
$product->set_props( |
|
37
|
|
|
[ |
|
38
|
|
|
'name' => 'Dummy Product', |
|
39
|
|
|
'regular_price' => 10, |
|
40
|
|
|
'price' => 10, |
|
41
|
|
|
'sku' => 'DUMMY SKU', |
|
42
|
|
|
'manage_stock' => false, |
|
43
|
|
|
'tax_status' => 'taxable', |
|
44
|
|
|
'downloadable' => false, |
|
45
|
|
|
'virtual' => false, |
|
46
|
|
|
'stock_status' => 'instock', |
|
47
|
|
|
'weight' => '1.1', |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
if ( $save ) { |
|
52
|
|
|
$product->save(); |
|
53
|
|
|
return wc_get_product( $product->get_id() ); |
|
54
|
|
|
} else { |
|
55
|
|
|
return $product; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create external product. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 3.0.0 |
|
63
|
|
|
* @return WC_Product_External |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function create_external_product() { |
|
66
|
|
|
$product = new WC_Product_External(); |
|
67
|
|
|
$product->set_props( |
|
68
|
|
|
[ |
|
69
|
|
|
'name' => 'Dummy External Product', |
|
70
|
|
|
'regular_price' => 10, |
|
71
|
|
|
'sku' => 'DUMMY EXTERNAL SKU', |
|
72
|
|
|
'product_url' => 'http://woocommerce.com', |
|
73
|
|
|
'button_text' => 'Buy external product', |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
|
|
$product->save(); |
|
77
|
|
|
|
|
78
|
|
|
return wc_get_product( $product->get_id() ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Create grouped product. |
|
83
|
|
|
* |
|
84
|
|
|
* @since 3.0.0 |
|
85
|
|
|
* @return WC_Product_Grouped |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function create_grouped_product() { |
|
88
|
|
|
$simple_product_1 = self::create_simple_product(); |
|
89
|
|
|
$simple_product_2 = self::create_simple_product(); |
|
90
|
|
|
$product = new WC_Product_Grouped(); |
|
91
|
|
|
$product->set_props( |
|
92
|
|
|
[ |
|
93
|
|
|
'name' => 'Dummy Grouped Product', |
|
94
|
|
|
'sku' => 'DUMMY GROUPED SKU', |
|
95
|
|
|
] |
|
96
|
|
|
); |
|
97
|
|
|
$product->set_children( [ $simple_product_1->get_id(), $simple_product_2->get_id() ] ); |
|
98
|
|
|
$product->save(); |
|
99
|
|
|
|
|
100
|
|
|
return wc_get_product( $product->get_id() ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Create a dummy variation product. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 2.3 |
|
107
|
|
|
* |
|
108
|
|
|
* @return WC_Product_Variable |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function create_variation_product() { |
|
111
|
|
|
$product = new WC_Product_Variable(); |
|
112
|
|
|
$product->set_props( |
|
113
|
|
|
[ |
|
114
|
|
|
'name' => 'Dummy Variable Product', |
|
115
|
|
|
'sku' => 'DUMMY VARIABLE SKU', |
|
116
|
|
|
] |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$attributes = []; |
|
120
|
|
|
|
|
121
|
|
|
$attribute = new WC_Product_Attribute(); |
|
122
|
|
|
$attribute_data = self::create_attribute( 'size', [ 'small', 'large', 'huge' ] ); |
|
123
|
|
|
$attribute->set_id( $attribute_data['attribute_id'] ); |
|
124
|
|
|
$attribute->set_name( $attribute_data['attribute_taxonomy'] ); |
|
125
|
|
|
$attribute->set_options( $attribute_data['term_ids'] ); |
|
126
|
|
|
$attribute->set_position( 1 ); |
|
127
|
|
|
$attribute->set_visible( true ); |
|
128
|
|
|
$attribute->set_variation( true ); |
|
129
|
|
|
$attributes[] = $attribute; |
|
130
|
|
|
|
|
131
|
|
|
$attribute = new WC_Product_Attribute(); |
|
132
|
|
|
$attribute_data = self::create_attribute( 'colour', [ 'red', 'blue' ] ); |
|
133
|
|
|
$attribute->set_id( $attribute_data['attribute_id'] ); |
|
134
|
|
|
$attribute->set_name( $attribute_data['attribute_taxonomy'] ); |
|
135
|
|
|
$attribute->set_options( $attribute_data['term_ids'] ); |
|
136
|
|
|
$attribute->set_position( 1 ); |
|
137
|
|
|
$attribute->set_visible( true ); |
|
138
|
|
|
$attribute->set_variation( true ); |
|
139
|
|
|
$attributes[] = $attribute; |
|
140
|
|
|
|
|
141
|
|
|
$attribute = new WC_Product_Attribute(); |
|
142
|
|
|
$attribute_data = self::create_attribute( 'number', [ '0', '1', '2' ] ); |
|
143
|
|
|
$attribute->set_id( $attribute_data['attribute_id'] ); |
|
144
|
|
|
$attribute->set_name( $attribute_data['attribute_taxonomy'] ); |
|
145
|
|
|
$attribute->set_options( $attribute_data['term_ids'] ); |
|
146
|
|
|
$attribute->set_position( 1 ); |
|
147
|
|
|
$attribute->set_visible( true ); |
|
148
|
|
|
$attribute->set_variation( true ); |
|
149
|
|
|
$attributes[] = $attribute; |
|
150
|
|
|
|
|
151
|
|
|
$product->set_attributes( $attributes ); |
|
152
|
|
|
$product->save(); |
|
153
|
|
|
|
|
154
|
|
|
$variation_1 = new WC_Product_Variation(); |
|
155
|
|
|
$variation_1->set_props( |
|
156
|
|
|
[ |
|
157
|
|
|
'parent_id' => $product->get_id(), |
|
158
|
|
|
'sku' => 'DUMMY SKU VARIABLE SMALL', |
|
159
|
|
|
'regular_price' => 10, |
|
160
|
|
|
] |
|
161
|
|
|
); |
|
162
|
|
|
$variation_1->set_attributes( [ 'pa_size' => 'small' ] ); |
|
163
|
|
|
$variation_1->save(); |
|
164
|
|
|
|
|
165
|
|
|
$variation_2 = new WC_Product_Variation(); |
|
166
|
|
|
$variation_2->set_props( |
|
167
|
|
|
[ |
|
168
|
|
|
'parent_id' => $product->get_id(), |
|
169
|
|
|
'sku' => 'DUMMY SKU VARIABLE LARGE', |
|
170
|
|
|
'regular_price' => 15, |
|
171
|
|
|
] |
|
172
|
|
|
); |
|
173
|
|
|
$variation_2->set_attributes( [ 'pa_size' => 'large' ] ); |
|
174
|
|
|
$variation_2->save(); |
|
175
|
|
|
|
|
176
|
|
|
$variation_3 = new WC_Product_Variation(); |
|
177
|
|
|
$variation_3->set_props( |
|
178
|
|
|
[ |
|
179
|
|
|
'parent_id' => $product->get_id(), |
|
180
|
|
|
'sku' => 'DUMMY SKU VARIABLE HUGE RED 0', |
|
181
|
|
|
'regular_price' => 16, |
|
182
|
|
|
] |
|
183
|
|
|
); |
|
184
|
|
|
$variation_3->set_attributes( |
|
185
|
|
|
[ |
|
186
|
|
|
'pa_size' => 'huge', |
|
187
|
|
|
'pa_colour' => 'red', |
|
188
|
|
|
'pa_number' => '0', |
|
189
|
|
|
] |
|
190
|
|
|
); |
|
191
|
|
|
$variation_3->save(); |
|
192
|
|
|
|
|
193
|
|
|
$variation_4 = new WC_Product_Variation(); |
|
194
|
|
|
$variation_4->set_props( |
|
195
|
|
|
[ |
|
196
|
|
|
'parent_id' => $product->get_id(), |
|
197
|
|
|
'sku' => 'DUMMY SKU VARIABLE HUGE RED 2', |
|
198
|
|
|
'regular_price' => 17, |
|
199
|
|
|
] |
|
200
|
|
|
); |
|
201
|
|
|
$variation_4->set_attributes( |
|
202
|
|
|
[ |
|
203
|
|
|
'pa_size' => 'huge', |
|
204
|
|
|
'pa_colour' => 'red', |
|
205
|
|
|
'pa_number' => '2', |
|
206
|
|
|
] |
|
207
|
|
|
); |
|
208
|
|
|
$variation_4->save(); |
|
209
|
|
|
|
|
210
|
|
|
return wc_get_product( $product->get_id() ); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Create a dummy attribute. |
|
215
|
|
|
* |
|
216
|
|
|
* @since 2.3 |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $raw_name Name of attribute to create. |
|
219
|
|
|
* @param array(string) $terms Terms to create for the attribute. |
|
|
|
|
|
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
public static function create_attribute( $raw_name = 'size', $terms = [ 'small' ] ) { |
|
223
|
|
|
global $wpdb, $wc_product_attributes; |
|
224
|
|
|
|
|
225
|
|
|
// Make sure caches are clean. |
|
226
|
|
|
delete_transient( 'wc_attribute_taxonomies' ); |
|
227
|
|
|
WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' ); |
|
228
|
|
|
|
|
229
|
|
|
// These are exported as labels, so convert the label to a name if possible first. |
|
230
|
|
|
$attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); |
|
231
|
|
|
$attribute_name = array_search( $raw_name, $attribute_labels, true ); |
|
232
|
|
|
|
|
233
|
|
|
if ( ! $attribute_name ) { |
|
234
|
|
|
$attribute_name = wc_sanitize_taxonomy_name( $raw_name ); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ); |
|
238
|
|
|
|
|
239
|
|
|
if ( ! $attribute_id ) { |
|
240
|
|
|
$taxonomy_name = wc_attribute_taxonomy_name( $attribute_name ); |
|
241
|
|
|
|
|
242
|
|
|
// Degister taxonomy which other tests may have created... |
|
243
|
|
|
unregister_taxonomy( $taxonomy_name ); |
|
244
|
|
|
|
|
245
|
|
|
$attribute_id = wc_create_attribute( |
|
246
|
|
|
[ |
|
247
|
|
|
'name' => $raw_name, |
|
248
|
|
|
'slug' => $attribute_name, |
|
249
|
|
|
'type' => 'select', |
|
250
|
|
|
'order_by' => 'menu_order', |
|
251
|
|
|
'has_archives' => 0, |
|
252
|
|
|
] |
|
253
|
|
|
); |
|
254
|
|
|
|
|
255
|
|
|
// Register as taxonomy. |
|
256
|
|
|
register_taxonomy( |
|
257
|
|
|
$taxonomy_name, |
|
258
|
|
|
apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, [ 'product' ] ), |
|
259
|
|
|
apply_filters( |
|
260
|
|
|
'woocommerce_taxonomy_args_' . $taxonomy_name, |
|
261
|
|
|
[ |
|
262
|
|
|
'labels' => [ |
|
263
|
|
|
'name' => $raw_name, |
|
264
|
|
|
], |
|
265
|
|
|
'hierarchical' => false, |
|
266
|
|
|
'show_ui' => false, |
|
267
|
|
|
'query_var' => true, |
|
268
|
|
|
'rewrite' => false, |
|
269
|
|
|
] |
|
270
|
|
|
) |
|
271
|
|
|
); |
|
272
|
|
|
|
|
273
|
|
|
// Set product attributes global. |
|
274
|
|
|
$wc_product_attributes = []; |
|
275
|
|
|
|
|
276
|
|
|
foreach ( wc_get_attribute_taxonomies() as $taxonomy ) { |
|
277
|
|
|
$wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$attribute = wc_get_attribute( $attribute_id ); |
|
282
|
|
|
$return = [ |
|
283
|
|
|
'attribute_name' => $attribute->name, |
|
284
|
|
|
'attribute_taxonomy' => $attribute->slug, |
|
285
|
|
|
'attribute_id' => $attribute_id, |
|
286
|
|
|
'term_ids' => [], |
|
287
|
|
|
]; |
|
288
|
|
|
|
|
289
|
|
|
foreach ( $terms as $term ) { |
|
290
|
|
|
$result = term_exists( $term, $attribute->slug ); |
|
291
|
|
|
|
|
292
|
|
|
if ( ! $result ) { |
|
293
|
|
|
$result = wp_insert_term( $term, $attribute->slug ); |
|
294
|
|
|
$return['term_ids'][] = $result['term_id']; |
|
295
|
|
|
} else { |
|
296
|
|
|
$return['term_ids'][] = $result['term_id']; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return $return; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Delete an attribute. |
|
305
|
|
|
* |
|
306
|
|
|
* @param int $attribute_id ID to delete. |
|
307
|
|
|
* |
|
308
|
|
|
* @since 2.3 |
|
309
|
|
|
*/ |
|
310
|
|
|
public static function delete_attribute( $attribute_id ) { |
|
311
|
|
|
global $wpdb; |
|
312
|
|
|
|
|
313
|
|
|
$attribute_id = absint( $attribute_id ); |
|
314
|
|
|
|
|
315
|
|
|
$wpdb->query( |
|
316
|
|
|
$wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id ) |
|
317
|
|
|
); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Creates a new product review on a specific product. |
|
322
|
|
|
* |
|
323
|
|
|
* @since 3.0 |
|
324
|
|
|
* @param int $product_id integer Product ID that the review is for. |
|
325
|
|
|
* @param string $review_content string Content to use for the product review. |
|
326
|
|
|
* @return integer Product Review ID. |
|
327
|
|
|
*/ |
|
328
|
|
|
public static function create_product_review( $product_id, $review_content = 'Review content here' ) { |
|
329
|
|
|
$data = [ |
|
330
|
|
|
'comment_post_ID' => $product_id, |
|
331
|
|
|
'comment_author' => 'admin', |
|
332
|
|
|
'comment_author_email' => '[email protected]', |
|
333
|
|
|
'comment_author_url' => '', |
|
334
|
|
|
'comment_date' => '2016-01-01T11:11:11', |
|
335
|
|
|
'comment_content' => $review_content, |
|
336
|
|
|
'comment_approved' => 1, |
|
337
|
|
|
'comment_type' => 'review', |
|
338
|
|
|
]; |
|
339
|
|
|
return wp_insert_comment( $data ); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* A helper function for hooking into save_post during the test_product_meta_save_post test. |
|
344
|
|
|
* @since 3.0.1 |
|
345
|
|
|
* |
|
346
|
|
|
* @param int $id ID to update. |
|
347
|
|
|
*/ |
|
348
|
|
|
public static function save_post_test_update_meta_data_direct( $id ) { |
|
349
|
|
|
update_post_meta( $id, '_test2', 'world' ); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.