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-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0-rc.1 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rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / admin / wc-admin-functions.php
woocommerce / includes / admin Last commit date
helper 3 weeks ago importers 1 month ago list-tables 1 month ago marketplace-suggestions 1 year ago meta-boxes 1 month ago notes 3 months ago plugin-updates 2 years ago reports 1 month ago settings 1 month ago views 1 month ago class-wc-admin-addons.php 10 months ago class-wc-admin-api-keys-table-list.php 3 years ago class-wc-admin-api-keys.php 1 month ago class-wc-admin-assets.php 1 month ago class-wc-admin-attributes.php 1 month ago class-wc-admin-brands.php 1 month ago class-wc-admin-customize.php 6 years ago class-wc-admin-dashboard-setup.php 1 month ago class-wc-admin-dashboard.php 1 month ago class-wc-admin-duplicate-product.php 1 month ago class-wc-admin-exporters.php 1 year ago class-wc-admin-help.php 2 years ago class-wc-admin-importers.php 1 year ago class-wc-admin-log-table-list.php 6 months ago class-wc-admin-marketplace-promotions.php 1 month ago class-wc-admin-menus.php 1 month ago class-wc-admin-meta-boxes.php 2 months ago class-wc-admin-notices.php 1 month ago class-wc-admin-permalink-settings.php 5 years ago class-wc-admin-pointers.php 3 years ago class-wc-admin-post-types.php 1 month ago class-wc-admin-profile.php 1 year ago class-wc-admin-reports.php 6 months ago class-wc-admin-settings.php 1 month ago class-wc-admin-setup-wizard.php 2 months ago class-wc-admin-status.php 1 year ago class-wc-admin-taxonomies.php 2 months ago class-wc-admin-upload-downloadable-product.php 2 years ago class-wc-admin-webhooks-table-list.php 2 months ago class-wc-admin-webhooks.php 1 month ago class-wc-admin.php 5 months ago wc-admin-functions.php 1 month ago wc-meta-box-functions.php 1 year ago woocommerce-legacy-reports.php 1 year ago
wc-admin-functions.php
609 lines
1 <?php
2 /**
3 * WooCommerce Admin Functions
4 *
5 * @package WooCommerce\Admin\Functions
6 * @version 2.4.0
7 */
8
9 use Automattic\WooCommerce\Enums\OrderStatus;
10 use Automattic\WooCommerce\Internal\Orders\OrderNoteGroup;
11 use Automattic\WooCommerce\Internal\Utilities\OrderItemMetaUtil;
12 use Automattic\WooCommerce\Utilities\OrderUtil;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Get all WooCommerce screen ids.
20 * Note, among other things, this is used to conditionally load some assets. See class-wc-admin-assets.php.
21 *
22 * @return array
23 */
24 function wc_get_screen_ids() {
25 $wc_screen_id = 'woocommerce';
26 $screen_ids = array(
27 'toplevel_page_' . $wc_screen_id,
28 $wc_screen_id . '_page_wc-orders',
29 $wc_screen_id . '_page_wc-reports',
30 $wc_screen_id . '_page_wc-shipping',
31 $wc_screen_id . '_page_wc-settings',
32 $wc_screen_id . '_page_wc-status',
33 $wc_screen_id . '_page_wc-addons',
34 'toplevel_page_wc-reports',
35 'product_page_product_attributes',
36 'product_page_product_exporter',
37 'product_page_product_importer',
38 'product_page_product-reviews',
39 'edit-product',
40 'product',
41 'edit-shop_coupon',
42 'shop_coupon',
43 'edit-product_cat',
44 'edit-product_tag',
45 'edit-product-brand',
46 'profile',
47 'user-edit',
48 );
49
50 foreach ( wc_get_order_types() as $type ) {
51 $screen_ids[] = $type;
52 $screen_ids[] = 'edit-' . $type;
53 $screen_ids[] = wc_get_page_screen_id( $type );
54 }
55
56 $attributes = wc_get_attribute_taxonomies();
57
58 if ( $attributes ) {
59 foreach ( $attributes as $attribute ) {
60 $screen_ids[] = 'edit-' . wc_attribute_taxonomy_name( $attribute->attribute_name );
61 }
62 }
63
64 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
65 return apply_filters( 'woocommerce_screen_ids', $screen_ids );
66 /* phpcs: enable */
67 }
68
69 /**
70 * Get page ID for a specific WC resource.
71 *
72 * @param string $for Name of the resource.
73 *
74 * @return string Page ID. Empty string if resource not found.
75 */
76 function wc_get_page_screen_id( $for ) {
77 $screen_id = '';
78 $for = str_replace( '-', '_', $for );
79
80 if ( in_array( $for, wc_get_order_types( 'admin-menu' ), true ) ) {
81 if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
82 $screen_id = ( \WC_Admin_Menus::can_view_woocommerce_menu_item() ? 'woocommerce_page_wc-orders' : 'admin_page_wc-orders' ) . ( 'shop_order' === $for ? '' : '--' . $for );
83 } else {
84 $screen_id = $for;
85 }
86 }
87
88 return $screen_id;
89 }
90
91 /**
92 * Create a page and store the ID in an option.
93 *
94 * @param mixed $slug Slug for the new page.
95 * @param string $option Option name to store the page's ID.
96 * @param string $page_title (default: '') Title for the new page.
97 * @param string $page_content (default: '') Content for the new page.
98 * @param int $post_parent (default: 0) Parent for the new page.
99 * @param string $post_status (default: publish) The post status of the new page.
100 * @return int page ID.
101 */
102 function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0, $post_status = 'publish' ) {
103 global $wpdb;
104
105 $option_value = get_option( $option );
106
107 if ( $option_value > 0 ) {
108 $page_object = get_post( $option_value );
109
110 if ( $page_object && 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ), true ) ) {
111 // Valid page is already in place.
112 return $page_object->ID;
113 }
114 }
115
116 if ( strlen( $page_content ) > 0 ) {
117 // Search for an existing page with the specified page content (typically a shortcode).
118 $shortcode = str_replace( array( '<!-- wp:shortcode -->', '<!-- /wp:shortcode -->' ), '', $page_content );
119 $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$shortcode}%" ) );
120 } else {
121 // Search for an existing page with the specified page slug.
122 $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) );
123 }
124
125 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
126 $valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
127 /* phpcs: enable */
128
129 if ( $valid_page_found ) {
130 if ( $option ) {
131 update_option( $option, $valid_page_found );
132 }
133 return $valid_page_found;
134 }
135
136 // Search for a matching valid trashed page.
137 if ( strlen( $page_content ) > 0 ) {
138 // Search for an existing page with the specified page content (typically a shortcode).
139 $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
140 } else {
141 // Search for an existing page with the specified page slug.
142 $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
143 }
144
145 if ( $trashed_page_found ) {
146 $page_id = $trashed_page_found;
147 $page_data = array(
148 'ID' => $page_id,
149 'post_status' => $post_status,
150 );
151 wp_update_post( $page_data );
152 } else {
153 $page_data = array(
154 'post_status' => $post_status,
155 'post_type' => 'page',
156 'post_author' => 1,
157 'post_name' => $slug,
158 'post_title' => $page_title,
159 'post_content' => $page_content,
160 'post_parent' => $post_parent,
161 'comment_status' => 'closed',
162 );
163 $page_id = wp_insert_post( $page_data );
164
165 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
166 do_action( 'woocommerce_page_created', $page_id, $page_data );
167 /* phpcs: enable */
168 }
169
170 if ( $option ) {
171 update_option( $option, $page_id );
172 }
173
174 return $page_id;
175 }
176
177 /**
178 * Output admin fields.
179 *
180 * Loops through the woocommerce options array and outputs each field.
181 *
182 * @param array $options Opens array to output.
183 */
184 function woocommerce_admin_fields( $options ) {
185
186 if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
187 include __DIR__ . '/class-wc-admin-settings.php';
188 }
189
190 WC_Admin_Settings::output_fields( $options );
191 }
192
193 /**
194 * Update all settings which are passed.
195 *
196 * @param array $options Option fields to save.
197 * @param array $data Passed data.
198 */
199 function woocommerce_update_options( $options, $data = null ) {
200
201 if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
202 include __DIR__ . '/class-wc-admin-settings.php';
203 }
204
205 WC_Admin_Settings::save_fields( $options, $data );
206 }
207
208 /**
209 * Get a setting from the settings API.
210 *
211 * @param mixed $option_name Option name to save.
212 * @param mixed $default Default value to save.
213 * @return string
214 */
215 function woocommerce_settings_get_option( $option_name, $default = '' ) {
216
217 if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
218 include __DIR__ . '/class-wc-admin-settings.php';
219 }
220
221 return WC_Admin_Settings::get_option( $option_name, $default );
222 }
223
224 /**
225 * Sees if line item stock has already reduced stock, and whether those values need adjusting e.g. after changing item qty.
226 *
227 * @since 3.6.0
228 * @param WC_Order_Item $item Item object.
229 * @param integer $item_quantity Optional quantity to check against. Read from object if not passed.
230 * @return boolean|array|WP_Error Array of changes or error object when stock is updated (@see wc_update_product_stock). False if nothing changes.
231 */
232 function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
233 if ( 'line_item' !== $item->get_type() ) {
234 return false;
235 }
236
237 /**
238 * Prevent adjust line item product stock.
239 *
240 * @since 3.7.1
241 * @param bool $prevent If should prevent.
242 * @param WC_Order_Item $item Item object.
243 * @param int $item_quantity Optional quantity to check against.
244 */
245 if ( apply_filters( 'woocommerce_prevent_adjust_line_item_product_stock', false, $item, $item_quantity ) ) {
246 return false;
247 }
248
249 $product = $item->get_product();
250
251 if ( ! $product || ! $product->managing_stock() ) {
252 return false;
253 }
254
255 $item_quantity = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() );
256 $already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
257 $restock_refunded_items = wc_stock_amount( $item->get_meta( '_restock_refunded_items', true ) );
258
259 $diff = $item_quantity - $restock_refunded_items - $already_reduced_stock;
260
261 /*
262 * 0 as $item_quantity usually indicates we're deleting the order item.
263 * Let's restore back the reduced count.
264 */
265 if ( 0 === $item_quantity ) {
266 $diff = $already_reduced_stock * -1;
267 }
268
269 if ( $diff < 0 ) {
270 $new_stock = wc_update_product_stock( $product, $diff * -1, 'increase' );
271 } elseif ( $diff > 0 ) {
272 $new_stock = wc_update_product_stock( $product, $diff, 'decrease' );
273 } else {
274 return false;
275 }
276
277 if ( is_wp_error( $new_stock ) ) {
278 return $new_stock;
279 }
280
281 $item->update_meta_data( '_reduced_stock', $item_quantity - $restock_refunded_items );
282 $item->save();
283
284 if ( $item_quantity > 0 ) {
285 // If stock was reduced, then we need to mark this on parent order object as well so that cancel logic works properly.
286 $order_data_store = WC_Data_Store::load( 'order' );
287 if ( $item->get_order_id() && ! $order_data_store->get_stock_reduced( $item->get_order_id() ) ) {
288 $order_data_store->set_stock_reduced( $item->get_order_id(), true );
289 }
290 }
291
292 return array(
293 'from' => $new_stock + $diff,
294 'to' => $new_stock,
295 );
296 }
297
298 /**
299 * Save order items. Uses the CRUD.
300 *
301 * @since 2.2
302 * @param int $order_id Order ID.
303 * @param array $items Order items to save.
304 */
305 function wc_save_order_items( $order_id, $items ) {
306 // Allow other plugins to check change in order items before they are saved.
307 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
308 do_action( 'woocommerce_before_save_order_items', $order_id, $items );
309 /* phpcs: enable */
310
311 $qty_change_order_notes = array();
312 $order = wc_get_order( $order_id );
313
314 // Line items and fees.
315 if ( isset( $items['order_item_id'] ) ) {
316 $data_keys = array(
317 'line_tax' => array(),
318 'line_subtotal_tax' => array(),
319 'order_item_name' => null,
320 'order_item_qty' => null,
321 'order_item_tax_class' => null,
322 'line_total' => null,
323 'line_subtotal' => null,
324 );
325 foreach ( $items['order_item_id'] as $item_id ) {
326 $item = WC_Order_Factory::get_order_item( absint( $item_id ) );
327
328 if ( ! $item ) {
329 continue;
330 }
331
332 $item_data = array();
333
334 foreach ( $data_keys as $key => $default ) {
335 $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_check_invalid_utf8( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
336 }
337
338 if ( '0' === $item_data['order_item_qty'] ) {
339 $changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 );
340 if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
341 $qty_change_order_notes[] = $item->get_name() . ' &ndash; ' . $changed_stock['from'] . '&rarr;' . $changed_stock['to'];
342 }
343 $item->delete();
344 continue;
345 }
346
347 $item->set_props(
348 array(
349 'name' => $item_data['order_item_name'],
350 'quantity' => $item_data['order_item_qty'],
351 'tax_class' => $item_data['order_item_tax_class'],
352 'total' => $item_data['line_total'],
353 'subtotal' => $item_data['line_subtotal'],
354 'taxes' => array(
355 'total' => $item_data['line_tax'],
356 'subtotal' => $item_data['line_subtotal_tax'],
357 ),
358 )
359 );
360
361 if ( 'fee' === $item->get_type() ) {
362 $item->set_amount( $item_data['line_total'] );
363 }
364
365 if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
366 $reserved_meta_keys = OrderItemMetaUtil::get_reserved_keys( $item );
367
368 foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
369 $meta_key = substr( wp_unslash( $meta_key ), 0, 255 );
370 $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
371
372 // Skip reserved keys, which cannot be added or edited as custom meta.
373 if ( in_array( $meta_key, $reserved_meta_keys, true ) ) {
374 continue;
375 }
376
377 if ( '' === $meta_key && '' === $meta_value ) {
378 if ( ! strstr( $meta_id, 'new-' ) ) {
379 $item->delete_meta_data_by_mid( $meta_id );
380 }
381 } elseif ( strstr( $meta_id, 'new-' ) ) {
382 $item->add_meta_data( $meta_key, $meta_value, false );
383 } else {
384 $item->update_meta_data( $meta_key, $meta_value, $meta_id );
385 }
386 }
387 }
388
389 // Allow other plugins to change item object before it is saved.
390 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
391 do_action( 'woocommerce_before_save_order_item', $item );
392 /* phpcs: enable */
393
394 $item->save();
395
396 if ( in_array( $order->get_status(), array( OrderStatus::PROCESSING, OrderStatus::COMPLETED, OrderStatus::ON_HOLD ), true ) ) {
397 $changed_stock = wc_maybe_adjust_line_item_product_stock( $item );
398 if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
399 $qty_change_order_notes[] = $item->get_name() . ' (' . $changed_stock['from'] . '&rarr;' . $changed_stock['to'] . ')';
400 }
401 }
402 }
403 }
404
405 // Shipping Rows.
406 if ( isset( $items['shipping_method_id'] ) ) {
407 $data_keys = array(
408 'shipping_method' => null,
409 'shipping_method_title' => null,
410 'shipping_cost' => 0,
411 'shipping_taxes' => array(),
412 );
413
414 foreach ( $items['shipping_method_id'] as $item_id ) {
415 $item = WC_Order_Factory::get_order_item( absint( $item_id ) );
416
417 if ( ! $item ) {
418 continue;
419 }
420
421 $item_data = array();
422
423 foreach ( $data_keys as $key => $default ) {
424 $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
425 }
426
427 $item->set_props(
428 array(
429 'method_id' => $item_data['shipping_method'],
430 'method_title' => $item_data['shipping_method_title'],
431 'total' => $item_data['shipping_cost'],
432 'taxes' => array(
433 'total' => $item_data['shipping_taxes'],
434 ),
435 )
436 );
437
438 if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
439 $reserved_meta_keys = OrderItemMetaUtil::get_reserved_keys( $item );
440
441 foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
442 $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
443
444 // Skip reserved keys, which cannot be added or edited as custom meta.
445 if ( in_array( $meta_key, $reserved_meta_keys, true ) ) {
446 continue;
447 }
448
449 if ( '' === $meta_key && '' === $meta_value ) {
450 if ( ! strstr( $meta_id, 'new-' ) ) {
451 $item->delete_meta_data_by_mid( $meta_id );
452 }
453 } elseif ( strstr( $meta_id, 'new-' ) ) {
454 $item->add_meta_data( $meta_key, $meta_value, false );
455 } else {
456 $item->update_meta_data( $meta_key, $meta_value, $meta_id );
457 }
458 }
459 }
460
461 $item->save();
462 }
463 }
464
465 $order = wc_get_order( $order_id );
466
467 if ( ! empty( $qty_change_order_notes ) ) {
468 /* translators: %s item name. */
469 $order->add_order_note( sprintf( __( 'Adjusted stock: %s', 'woocommerce' ), implode( ', ', $qty_change_order_notes ) ), false, true, array( 'note_group' => OrderNoteGroup::PRODUCT_STOCK ) );
470 }
471
472 $order->update_taxes();
473 $order->calculate_totals( false );
474
475 // Inform other plugins that the items have been saved.
476 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
477 do_action( 'woocommerce_saved_order_items', $order_id, $items );
478 /* phpcs: enable */
479 }
480
481 /**
482 * Get HTML for some action buttons. Used in list tables.
483 *
484 * @since 3.3.0
485 * @param array $actions Actions to output.
486 * @return string
487 */
488 function wc_render_action_buttons( $actions ) {
489 $actions_html = '';
490
491 foreach ( $actions as $action ) {
492 if ( isset( $action['group'] ) ) {
493 $actions_html .= '<div class="wc-action-button-group"><label>' . $action['group'] . '</label> <span class="wc-action-button-group__items">' . wc_render_action_buttons( $action['actions'] ) . '</span></div>';
494 } elseif ( isset( $action['action'], $action['url'], $action['name'] ) ) {
495 $actions_html .= sprintf( '<a class="button wc-action-button wc-action-button-%1$s %1$s" href="%2$s" aria-label="%3$s" title="%3$s">%4$s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( isset( $action['title'] ) ? $action['title'] : $action['name'] ), esc_html( $action['name'] ) );
496 }
497 }
498
499 return $actions_html;
500 }
501
502 /**
503 * Shows a notice if variations are missing prices.
504 *
505 * @since 3.6.0
506 * @param WC_Product $product_object Product object.
507 */
508 function wc_render_invalid_variation_notice( $product_object ) {
509 global $wpdb;
510
511 // Give ability for extensions to hide this notice.
512 /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
513 if ( ! apply_filters( 'woocommerce_show_invalid_variations_notice', true, $product_object ) ) {
514 return;
515 }
516 /* phpcs: enable */
517
518 $variation_ids = $product_object ? $product_object->get_children() : array();
519
520 if ( empty( $variation_ids ) ) {
521 return;
522 }
523
524 $variation_count = count( $variation_ids );
525
526 // Check if a variation exists without pricing data.
527 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
528 $valid_variation_count = $wpdb->get_var(
529 "
530 SELECT count(post_id) FROM {$wpdb->postmeta}
531 WHERE post_id in (" . implode( ',', array_map( 'absint', $variation_ids ) ) . ")
532 AND ( meta_key='_subscription_sign_up_fee' OR meta_key='_price' )
533 AND meta_value >= 0
534 AND meta_value != ''
535 "
536 );
537 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
538
539 $invalid_variation_count = $variation_count - $valid_variation_count;
540
541 if ( 0 < $invalid_variation_count ) {
542 ?>
543 <div id="message" class="inline notice notice-warning woocommerce-message woocommerce-notice-invalid-variation">
544 <p>
545 <?php
546 echo wp_kses_post(
547 sprintf(
548 /* Translators: %d variation count. */
549 _n( '%d variation does not have a price.', '%d variations do not have prices.', $invalid_variation_count, 'woocommerce' ),
550 $invalid_variation_count
551 ) . '&nbsp;' .
552 __( 'Variations (and their attributes) that do not have prices will not be shown in your store.', 'woocommerce' )
553 );
554 ?>
555 </p>
556 <div class="woocommerce-add-variation-price-container">
557 <button type="button" class="button add_price_for_variations"><?php esc_html_e( 'Add price', 'woocommerce' ); ?></button>
558 </div>
559 </div>
560 <?php
561 }
562 }
563
564 /**
565 * Get current admin page URL.
566 *
567 * Returns an empty string if it cannot generate a URL.
568 *
569 * @internal
570 * @since 4.4.0
571 * @return string
572 */
573 function wc_get_current_admin_url() {
574 $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
575 $uri = preg_replace( '|^.*/wp-admin/|i', '', $uri );
576
577 if ( ! $uri ) {
578 return '';
579 }
580
581 return remove_query_arg( array( '_wpnonce', '_wc_notice_nonce', 'wc_db_update', 'wc_db_update_nonce', 'wc-hide-notice' ), admin_url( $uri ) );
582 }
583
584 /**
585 * Get default product type options.
586 *
587 * @internal
588 * @since 7.9.0
589 * @return array
590 */
591 function wc_get_default_product_type_options() {
592 return array(
593 'virtual' => array(
594 'id' => '_virtual',
595 'wrapper_class' => 'show_if_simple',
596 'label' => __( 'Virtual', 'woocommerce' ),
597 'description' => __( 'Virtual products are intangible and are not shipped.', 'woocommerce' ),
598 'default' => 'no',
599 ),
600 'downloadable' => array(
601 'id' => '_downloadable',
602 'wrapper_class' => 'show_if_simple',
603 'label' => __( 'Downloadable', 'woocommerce' ),
604 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ),
605 'default' => 'no',
606 ),
607 );
608 }
609