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