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
|
|
|
if ( is_callable( [ 'WC_Cache_Helper', 'invalidate_cache_group' ] ) ) { |
228
|
|
|
WC_Cache_Helper::invalidate_cache_group('woocommerce-attributes'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// These are exported as labels, so convert the label to a name if possible first. |
232
|
|
|
$attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); |
233
|
|
|
$attribute_name = array_search( $raw_name, $attribute_labels, true ); |
234
|
|
|
|
235
|
|
|
if ( ! $attribute_name ) { |
236
|
|
|
$attribute_name = wc_sanitize_taxonomy_name( $raw_name ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ); |
240
|
|
|
|
241
|
|
|
if ( ! $attribute_id ) { |
242
|
|
|
$taxonomy_name = wc_attribute_taxonomy_name( $attribute_name ); |
243
|
|
|
|
244
|
|
|
// Degister taxonomy which other tests may have created... |
245
|
|
|
unregister_taxonomy( $taxonomy_name ); |
246
|
|
|
|
247
|
|
|
$attribute_id = wc_create_attribute( |
248
|
|
|
[ |
249
|
|
|
'name' => $raw_name, |
250
|
|
|
'slug' => $attribute_name, |
251
|
|
|
'type' => 'select', |
252
|
|
|
'order_by' => 'menu_order', |
253
|
|
|
'has_archives' => 0, |
254
|
|
|
] |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
// Register as taxonomy. |
258
|
|
|
register_taxonomy( |
259
|
|
|
$taxonomy_name, |
260
|
|
|
apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, [ 'product' ] ), |
261
|
|
|
apply_filters( |
262
|
|
|
'woocommerce_taxonomy_args_' . $taxonomy_name, |
263
|
|
|
[ |
264
|
|
|
'labels' => [ |
265
|
|
|
'name' => $raw_name, |
266
|
|
|
], |
267
|
|
|
'hierarchical' => false, |
268
|
|
|
'show_ui' => false, |
269
|
|
|
'query_var' => true, |
270
|
|
|
'rewrite' => false, |
271
|
|
|
] |
272
|
|
|
) |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
// Set product attributes global. |
276
|
|
|
$wc_product_attributes = []; |
277
|
|
|
|
278
|
|
|
foreach ( wc_get_attribute_taxonomies() as $taxonomy ) { |
279
|
|
|
$wc_product_attributes[ wc_attribute_taxonomy_name( $taxonomy->attribute_name ) ] = $taxonomy; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$attribute = wc_get_attribute( $attribute_id ); |
284
|
|
|
$return = [ |
285
|
|
|
'attribute_name' => $attribute->name, |
286
|
|
|
'attribute_taxonomy' => $attribute->slug, |
287
|
|
|
'attribute_id' => $attribute_id, |
288
|
|
|
'term_ids' => [], |
289
|
|
|
]; |
290
|
|
|
|
291
|
|
|
foreach ( $terms as $term ) { |
292
|
|
|
$result = term_exists( $term, $attribute->slug ); |
293
|
|
|
|
294
|
|
|
if ( ! $result ) { |
295
|
|
|
$result = wp_insert_term( $term, $attribute->slug ); |
296
|
|
|
$return['term_ids'][] = $result['term_id']; |
297
|
|
|
} else { |
298
|
|
|
$return['term_ids'][] = $result['term_id']; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $return; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Delete an attribute. |
307
|
|
|
* |
308
|
|
|
* @param int $attribute_id ID to delete. |
309
|
|
|
* |
310
|
|
|
* @since 2.3 |
311
|
|
|
*/ |
312
|
|
|
public static function delete_attribute( $attribute_id ) { |
313
|
|
|
global $wpdb; |
314
|
|
|
|
315
|
|
|
$attribute_id = absint( $attribute_id ); |
316
|
|
|
|
317
|
|
|
$wpdb->query( |
318
|
|
|
$wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id ) |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Creates a new product review on a specific product. |
324
|
|
|
* |
325
|
|
|
* @since 3.0 |
326
|
|
|
* @param int $product_id integer Product ID that the review is for. |
327
|
|
|
* @param string $review_content string Content to use for the product review. |
328
|
|
|
* @return integer Product Review ID. |
329
|
|
|
*/ |
330
|
|
|
public static function create_product_review( $product_id, $review_content = 'Review content here' ) { |
331
|
|
|
$data = [ |
332
|
|
|
'comment_post_ID' => $product_id, |
333
|
|
|
'comment_author' => 'admin', |
334
|
|
|
'comment_author_email' => '[email protected]', |
335
|
|
|
'comment_author_url' => '', |
336
|
|
|
'comment_date' => '2016-01-01T11:11:11', |
337
|
|
|
'comment_content' => $review_content, |
338
|
|
|
'comment_approved' => 1, |
339
|
|
|
'comment_type' => 'review', |
340
|
|
|
]; |
341
|
|
|
return wp_insert_comment( $data ); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* A helper function for hooking into save_post during the test_product_meta_save_post test. |
346
|
|
|
* @since 3.0.1 |
347
|
|
|
* |
348
|
|
|
* @param int $id ID to update. |
349
|
|
|
*/ |
350
|
|
|
public static function save_post_test_update_meta_data_direct( $id ) { |
351
|
|
|
update_post_meta( $id, '_test2', 'world' ); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
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.