* WooCommerce Admin Functions
* @package WooCommerce\Admin\Functions
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Utilities\OrderUtil;
if ( ! defined( 'ABSPATH' ) ) {
* Get all WooCommerce screen ids.
function wc_get_screen_ids() {
$wc_screen_id = 'woocommerce';
'toplevel_page_' . $wc_screen_id,
$wc_screen_id . '_page_wc-orders',
$wc_screen_id . '_page_wc-reports',
$wc_screen_id . '_page_wc-shipping',
$wc_screen_id . '_page_wc-settings',
$wc_screen_id . '_page_wc-status',
$wc_screen_id . '_page_wc-addons',
'toplevel_page_wc-reports',
'product_page_product_attributes',
'product_page_product_exporter',
'product_page_product_importer',
'product_page_product-reviews',
foreach ( wc_get_order_types() as $type ) {
$screen_ids[] = 'edit-' . $type;
$screen_ids[] = wc_get_page_screen_id( $type );
$attributes = wc_get_attribute_taxonomies();
foreach ( $attributes as $attribute ) {
$screen_ids[] = 'edit-' . wc_attribute_taxonomy_name( $attribute->attribute_name );
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
return apply_filters( 'woocommerce_screen_ids', $screen_ids );
* Get page ID for a specific WC resource.
* @param string $for Name of the resource.
* @return string Page ID. Empty string if resource not found.
function wc_get_page_screen_id( $for ) {
$for = str_replace( '-', '_', $for );
if ( in_array( $for, wc_get_order_types( 'admin-menu' ), true ) ) {
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$screen_id = 'woocommerce_page_wc-orders' . ( 'shop_order' === $for ? '' : '--' . $for );
* Create a page and store the ID in an option.
* @param mixed $slug Slug for the new page.
* @param string $option Option name to store the page's ID.
* @param string $page_title (default: '') Title for the new page.
* @param string $page_content (default: '') Content for the new page.
* @param int $post_parent (default: 0) Parent for the new page.
* @param string $post_status (default: publish) The post status of the new page.
function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0, $post_status = 'publish' ) {
$option_value = get_option( $option );
if ( $option_value > 0 ) {
$page_object = get_post( $option_value );
if ( $page_object && 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ), true ) ) {
// Valid page is already in place.
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode).
$shortcode = str_replace( array( '<!-- wp:shortcode -->', '<!-- /wp:shortcode -->' ), '', $page_content );
$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}%" ) );
// Search for an existing page with the specified page slug.
$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 ) );
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
if ( $valid_page_found ) {
update_option( $option, $valid_page_found );
return $valid_page_found;
// Search for a matching valid trashed page.
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode).
$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}%" ) );
// Search for an existing page with the specified page slug.
$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 ) );
if ( $trashed_page_found ) {
$page_id = $trashed_page_found;
'post_status' => $post_status,
wp_update_post( $page_data );
'post_status' => $post_status,
'post_title' => $page_title,
'post_content' => $page_content,
'post_parent' => $post_parent,
'comment_status' => 'closed',
$page_id = wp_insert_post( $page_data );
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
do_action( 'woocommerce_page_created', $page_id, $page_data );
update_option( $option, $page_id );
* Loops through the woocommerce options array and outputs each field.
* @param array $options Opens array to output.
function woocommerce_admin_fields( $options ) {
if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
WC_Admin_Settings::output_fields( $options );
* Update all settings which are passed.
* @param array $options Option fields to save.
* @param array $data Passed data.
function woocommerce_update_options( $options, $data = null ) {
if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
WC_Admin_Settings::save_fields( $options, $data );
* Get a setting from the settings API.
* @param mixed $option_name Option name to save.
* @param mixed $default Default value to save.
function woocommerce_settings_get_option( $option_name, $default = '' ) {
if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
return WC_Admin_Settings::get_option( $option_name, $default );
* Sees if line item stock has already reduced stock, and whether those values need adjusting e.g. after changing item qty.
* @param WC_Order_Item $item Item object.
* @param integer $item_quantity Optional quantity to check against. Read from object if not passed.
* @return boolean|array|WP_Error Array of changes or error object when stock is updated (@see wc_update_product_stock). False if nothing changes.
function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
if ( 'line_item' !== $item->get_type() ) {
* Prevent adjust line item product stock.
* @param bool $prevent If should prevent.
* @param WC_Order_Item $item Item object.
* @param int $item_quantity Optional quantity to check against.
if ( apply_filters( 'woocommerce_prevent_adjust_line_item_product_stock', false, $item, $item_quantity ) ) {
$product = $item->get_product();
if ( ! $product || ! $product->managing_stock() ) {
$item_quantity = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() );
$already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
$restock_refunded_items = wc_stock_amount( $item->get_meta( '_restock_refunded_items', true ) );
$diff = $item_quantity - $restock_refunded_items - $already_reduced_stock;
* 0 as $item_quantity usually indicates we're deleting the order item.
* Let's restore back the reduced count.
if ( 0 === $item_quantity ) {
$diff = $already_reduced_stock * -1;
$new_stock = wc_update_product_stock( $product, $diff * -1, 'increase' );
$new_stock = wc_update_product_stock( $product, $diff, 'decrease' );
if ( is_wp_error( $new_stock ) ) {
$item->update_meta_data( '_reduced_stock', $item_quantity - $restock_refunded_items );
if ( $item_quantity > 0 ) {
// If stock was reduced, then we need to mark this on parent order object as well so that cancel logic works properly.
$order_data_store = WC_Data_Store::load( 'order' );
if ( $item->get_order_id() && ! $order_data_store->get_stock_reduced( $item->get_order_id() ) ) {
$order_data_store->set_stock_reduced( $item->get_order_id(), true );
'from' => $new_stock + $diff,
* Save order items. Uses the CRUD.
* @param int $order_id Order ID.
* @param array $items Order items to save.
function wc_save_order_items( $order_id, $items ) {
// Allow other plugins to check change in order items before they are saved.
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
do_action( 'woocommerce_before_save_order_items', $order_id, $items );
$qty_change_order_notes = array();
$order = wc_get_order( $order_id );
if ( isset( $items['order_item_id'] ) ) {
'line_subtotal_tax' => array(),
'order_item_name' => null,
'order_item_qty' => null,
'order_item_tax_class' => null,
foreach ( $items['order_item_id'] as $item_id ) {
$item = WC_Order_Factory::get_order_item( absint( $item_id ) );
foreach ( $data_keys as $key => $default ) {
$item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_check_invalid_utf8( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
if ( '0' === $item_data['order_item_qty'] ) {
$changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 );
if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
$qty_change_order_notes[] = $item->get_name() . ' – ' . $changed_stock['from'] . '→' . $changed_stock['to'];
'name' => $item_data['order_item_name'],
'quantity' => $item_data['order_item_qty'],
'tax_class' => $item_data['order_item_tax_class'],
'total' => $item_data['line_total'],
'subtotal' => $item_data['line_subtotal'],
'total' => $item_data['line_tax'],
'subtotal' => $item_data['line_subtotal_tax'],
if ( 'fee' === $item->get_type() ) {
$item->set_amount( $item_data['line_total'] );
if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
$meta_key = substr( wp_unslash( $meta_key ), 0, 255 );
$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
if ( '' === $meta_key && '' === $meta_value ) {
if ( ! strstr( $meta_id, 'new-' ) ) {
$item->delete_meta_data_by_mid( $meta_id );
} elseif ( strstr( $meta_id, 'new-' ) ) {
$item->add_meta_data( $meta_key, $meta_value, false );
$item->update_meta_data( $meta_key, $meta_value, $meta_id );
// Allow other plugins to change item object before it is saved.
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
do_action( 'woocommerce_before_save_order_item', $item );
if ( in_array( $order->get_status(), array( OrderStatus::PROCESSING, OrderStatus::COMPLETED, OrderStatus::ON_HOLD ), true ) ) {
$changed_stock = wc_maybe_adjust_line_item_product_stock( $item );
if ( $changed_stock && ! is_wp_error( $changed_stock ) ) {
$qty_change_order_notes[] = $item->get_name() . ' (' . $changed_stock['from'] . '→' . $changed_stock['to'] . ')';
if ( isset( $items['shipping_method_id'] ) ) {
'shipping_method' => null,
'shipping_method_title' => null,
'shipping_taxes' => array(),
foreach ( $items['shipping_method_id'] as $item_id ) {
$item = WC_Order_Factory::get_order_item( absint( $item_id ) );
foreach ( $data_keys as $key => $default ) {
$item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
'method_id' => $item_data['shipping_method'],
'method_title' => $item_data['shipping_method_title'],
'total' => $item_data['shipping_cost'],
'total' => $item_data['shipping_taxes'],
if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
if ( '' === $meta_key && '' === $meta_value ) {
if ( ! strstr( $meta_id, 'new-' ) ) {
$item->delete_meta_data_by_mid( $meta_id );
} elseif ( strstr( $meta_id, 'new-' ) ) {
$item->add_meta_data( $meta_key, $meta_value, false );
$item->update_meta_data( $meta_key, $meta_value, $meta_id );
$order = wc_get_order( $order_id );
if ( ! empty( $qty_change_order_notes ) ) {
/* translators: %s item name. */
$order->add_order_note( sprintf( __( 'Adjusted stock: %s', 'woocommerce' ), implode( ', ', $qty_change_order_notes ) ), false, true );
// Only recalculate when a coupon is applied.
// This allows manual discounts to be preserved when order items are saved.
$order_coupons = $order->get_coupons();
if ( ! empty( $order_coupons ) ) {
$order->recalculate_coupons();
$order->calculate_totals( false );
// Inform other plugins that the items have been saved.
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
do_action( 'woocommerce_saved_order_items', $order_id, $items );
* Get HTML for some action buttons. Used in list tables.
* @param array $actions Actions to output.
function wc_render_action_buttons( $actions ) {
foreach ( $actions as $action ) {
if ( isset( $action['group'] ) ) {
$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>';
} elseif ( isset( $action['action'], $action['url'], $action['name'] ) ) {
$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'] ) );
* Shows a notice if variations are missing prices.
* @param WC_Product $product_object Product object.
function wc_render_invalid_variation_notice( $product_object ) {