Add Custom Metadata to WooCommerce Orders in Admin Panel

How to add custom metadata to woocommerce orders; Add custom fields to woocommerce order admin panel; Woocommerce display order meta in admin; Customize woocommerce order details page; Add custom column to woocommerce orders list; Woocommerce admin order custom field tutorial; Show custom order metadata in woocommerce; Woocommerce order admin custom meta box; Add order metadata to woocommerce admin; Woocommerce custom fields in order summary;

Explanation

To add custom metadata to WooCommerce orders in the admin panel, you'll be enhancing the order details page and the orders list with a new custom field.

Adding Custom Fields to Order Details:

  • A new section titled "Custom Order Data" is added to the order details page.
  • This section includes a text input field where you can enter custom information for each order.

Saving Custom Fields:

  • When you save the order, the custom field data is stored in the database.
  • The data is sanitized to ensure it's safe and clean before saving.

Adding a Custom Column to Orders List:

  • A new column named "Custom Field" is added to the orders list in the admin panel.
  • This column displays the custom field data for each order, making it easy to view at a glance.

With these enhancements, you can easily manage additional order information directly from the WooCommerce admin panel, streamlining your workflow and keeping everything organized.

Code

<?php

// Add custom fields to WooCommerce order admin panel
function wp_dudecom_add_custom_order_meta_fields($order) {
    echo '<div class="order_data_column">';
    echo '<h4>' . __('Custom Order Data', 'woocommerce') . '</h4>';
    woocommerce_wp_text_input(array(
        'id' => '_custom_order_field',
        'label' => __('Custom Field', 'woocommerce'),
        'wrapper_class' => 'form-field-wide',
        'value' => get_post_meta($order->get_id(), '_custom_order_field', true),
    ));
    echo '</div>';
}
add_action('woocommerce_admin_order_data_after_order_details', 'wp_dudecom_add_custom_order_meta_fields');

// Save custom fields from WooCommerce order admin panel
function wp_dudecom_save_custom_order_meta_fields($post_id) {
    $order = wc_get_order($post_id);
    if (isset($_POST['_custom_order_field'])) {
        update_post_meta($order->get_id(), '_custom_order_field', sanitize_text_field($_POST['_custom_order_field']));
    }
}
add_action('woocommerce_process_shop_order_meta', 'wp_dudecom_save_custom_order_meta_fields', 10, 1);

// Add custom column to WooCommerce orders list
function wp_dudecom_add_custom_orders_list_column($columns) {
    $new_columns = (is_array($columns)) ? $columns : array();
    $new_columns['custom_order_field'] = __('Custom Field', 'woocommerce');
    return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'wp_dudecom_add_custom_orders_list_column', 20);

// Display custom field data in the new column
function wp_dudecom_display_custom_orders_list_column($column) {
    global $post;
    if ('custom_order_field' === $column) {
        $custom_field_value = get_post_meta($post->ID, '_custom_order_field', true);
        echo esc_html($custom_field_value);
    }
}
add_action('manage_shop_order_posts_custom_column', 'wp_dudecom_display_custom_orders_list_column', 10, 1);

?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Open your WordPress admin panel and navigate to Appearance > Theme Editor or use an FTP client to access your theme files.
  2. Locate and open the functions.php file of your active theme.
  3. Copy the provided code snippet and paste it at the end of the functions.php file.
  4. Save the changes to the functions.php file.
  5. Go to the WooCommerce orders section in your WordPress admin panel to see the new "Custom Order Data" section in the order details and the "Custom Field" column in the orders list.
  6. Test by adding and saving custom data to ensure it appears correctly in the orders list.

If you encounter any issues or need further assistance with this implementation or more advanced functionality, consider reaching out to wp-dude.com for expert help.