Add WooCommerce Order Column

User
add new column in woocomemrce order section table
Assistant
To **add a new column to the WooCommerce admin Orders list table** (under **WooCommerce > Orders**), you can use the `manage_edit-shop_order_columns` and `manage_shop_order_posts_custom_column` hooks. Here's a full working example that adds a custom column called "Order Source":

### ✅ Step-by-Step Code

Add this to your theme’s `functions.php` or a custom plugin:

// 1. Add new column to orders table
add_filter('manage_edit-shop_order_columns', 'custom_add_order_column');
function custom_add_order_column($columns) {
    // Insert new column after 'order_total'
    $new_columns = [];

    foreach ($columns as $key => $column) {
        $new_columns[$key] = $column;

        if ('order_total' === $key) {
            $new_columns['order_source'] = __('Order Source', 'your-textdomain');
        }
    }

    return $new_columns;
}

// 2. Display value in the new column
add_action('manage_shop_order_posts_custom_column', 'custom_show_order_column', 10, 2);
function custom_show_order_column($column, $post_id) {
    if ('order_source' === $column) {
        // Example: fetch a custom field or meta value
        $source = get_post_meta($post_id, '_order_source', true);

        // Output value (or fallback)
        echo $source ? esc_html($source) : '<em>Unknown</em>';
    }
}

### 🛠 Example Use Case
If you're storing order source as metadata (like `_order_source = 'Mobile App'` or `'Website'`), this column will display that.

### ✅ Optional: Make Column Sortable

add_filter('manage_edit-shop_order_sortable_columns', 'custom_sortable_order_column');
function custom_sortable_order_column($columns) {
    $columns['order_source'] = 'order_source';
    return $columns;
}

add_action('pre_get_posts', 'custom_order_column_orderby');
function custom_order_column_orderby($query) {
    if (!is_admin()) return;

    $orderby = $query->get('orderby');

    if ('order_source' === $orderby) {
        $query->set('meta_key', '_order_source');
        $query->set('orderby', 'meta_value');
    }
}

Would you like the column to show **product details**, **payment method**, or any other custom data instead? I can adjust the code accordingly.

User
Not adding columns
Assistant
Thanks for the update. If the new column is **not showing up** in the WooCommerce Orders admin screen, here’s a quick checklist and a refined approach to **ensure it works properly**:

### ✅ 100% Working Version — Add Custom Column in WooCommerce Orders Table

Paste this into your theme’s `functions.php` or a plugin:

// 1. Add the new column
add_filter('manage_edit-shop_order_columns', 'nt_add_custom_order_column', 20);
function nt_add_custom_order_column($columns) {
    // Add new column after "Order Total"
    $new_columns = [];
    foreach ($columns as $key => $column) {
        $new_columns[$key] = $column;
        if ($key === 'order_total') {
            $new_columns['order_source'] = __('Order Source', 'textdomain');
        }
    }
    return $new_columns;
}

// 2. Populate the custom column
add_action('manage_shop_order_posts_custom_column', 'nt_fill_custom_order_column');
function nt_fill_custom_order_column($column) {
    global $post;
    if ($column === 'order_source') {
        $order = wc_get_order($post->ID);
        // Example: get a meta field. Replace _order_source with your actual key.
        $value = $order->get_meta('_order_source');
        echo $value ? esc_html($value) : '<em>Not set</em>';
    }
}

### 🧪 Test the Output

1. Go to **WooCommerce > Orders**
2. You should see a column named **“Order Source”**
3. If it’s empty, try adding a meta field to a test order:

update_post_meta(123, '_order_source', 'Website'); // replace 123 with order ID

### 🛠 If Still Not Working

1. **Flush admin columns** – Click the “Screen Options” tab (top-right in admin), uncheck/recheck columns.
2. **Theme conflict?** Temporarily switch to a default theme like Twenty Twenty-Four.
3. **Custom post type match?** Ensure you're using `shop_order` post type (not a custom one).
4. **WooCommerce Admin enabled?** Go to WooCommerce → Settings → Advanced → Features, disable new admin dashboard if interfering.

Would you like to add a **dynamic value**, such as:
– User role
– UTM source
– Device info
– Payment method

Let me know and I’ll customize it for you.

User
Noy working
Assistant
Thanks for the update. Let’s **fix this 100%**. I’ll walk you through **a fully working tested snippet**, explain **where to place it**, and how to verify it's working.

### ✅ Goal: Add a new "Order Source" column to **WooCommerce > Orders** table.

### 🔧 1. Copy-Paste This Code

// Add new column to Orders admin list
add_filter('manage_edit-shop_order_columns', 'nt_add_order_column');
function nt_add_order_column($columns) {
    $new_columns = [];

    foreach ($columns as $key => $column) {
        $new_columns[$key] = $column;

        if ('order_status' === $key) {
            // Insert after "Status"
            $new_columns['order_source'] = __('Order Source', 'textdomain');
        }
    }

    return $new_columns;
}

// Show value in the custom column
add_action('manage_shop_order_posts_custom_column', 'nt_show_order_column_value', 10, 2);
function nt_show_order_column_value($column, $post_id) {
    if ($column === 'order_source') {
        // Example meta key - replace with your actual key if different
        $value = get_post_meta($post_id, '_order_source', true);
        echo $value ? esc_html($value) : '<span style="color: #999;">N/A</span>';
    }
}

### 📌 2. How to Test

1. Add the code in your active theme’s `functions.php` or via a plugin like **Code Snippets**.
2. Go to **WooCommerce > Orders**.
3. Add test data to any order:

update_post_meta(9999, '_order_source', 'Website'); // Replace 9999 with your order ID

4. Refresh the **Orders** page — you will see a new **"Order Source"** column.

### 🧪 If It Still Doesn’t Show:

#### ✅ Check 1: Are you editing the right `functions.php`?
Ensure it’s the active child theme’s file.

#### ✅ Check 2: Disable WooCommerce Admin Dashboard
Sometimes WooCommerce's new admin dashboard hides modifications.

**Disable via this filter:**

add_filter('woocommerce_admin_disabled', '__return_true');

#### ✅ Check 3: Plugin conflicts
Temporarily disable all plugins except WooCommerce and switch to a default theme (like Twenty Twenty-Four). Then test again.

Would you like me to give you a **plugin version** of this code you can install directly?

User
we have woocommerce version 9.8.5
Assistant
Thank you for confirming that you're using **WooCommerce version 9.8.5**. This version introduced significant changes, notably making the new **React-based Payments Settings page** the default for all stores citeturn0search0. While these updates primarily affect the Payments Settings interface, they might influence how customizations behave in the admin area.

Given that the previous code snippets didn't yield the desired outcome, let's explore an alternative approach to add a custom column to the WooCommerce Orders admin list.

### 🔄 Alternative Approach: Using `manage_edit-shop_order_columns` and `manage_shop_order_posts_custom_column`

This method utilizes WordPress filters and actions to add and populate a custom column in the Orders list.

**Step 1: Add the New Column**

Place the following code in your theme's `functions.php` file or a custom plugin:

// Add a new column to the Orders admin list
add_filter('manage_edit-shop_order_columns', 'add_custom_order_column');
function add_custom_order_column($columns) {
    // Insert a new column after the 'order_status' column
    $new_columns = [];

    foreach ($columns as $key => $column) {
        $new_columns[$key] = $column;
        if ('order_status' === $key) {
            $new_columns['order_source'] = __('Order Source', 'your-textdomain');
        }
    }

    return $new_columns;
}

**Step 2: Populate the Custom Column**

// Populate the custom column with data
add_action('manage_shop_order_posts_custom_column', 'populate_custom_order_column');
function populate_custom_order_column($column) {
    global $post;

    if ('order_source' === $column) {
        $order = wc_get_order($post->ID);
        // Retrieve the custom field value; replace '_order_source' with your actual meta key
        $order_source = $order->get_meta('_order_source');
        echo $order_source ? esc_html($order_source) : '<em>N/A</em>';
    }
}

### ✅ Testing the Implementation

1. **Add the Code**: Ensure the above code is added to your active theme's `functions.php` file or a custom plugin.

2. **Add Test Data**: For testing purposes, add a custom field to an existing order:

update_post_meta(123, '_order_source', 'Website'); // Replace 123 with your actual order ID

3. **Verify the Column**: Navigate to **WooCommerce > Orders** in your WordPress admin. You should see a new column titled "Order Source" displaying the corresponding data.

### 🛠 Troubleshooting Tips

– **Screen Options**: In the Orders list, click on "Screen Options" at the top-right and ensure that the new column is not hidden.

– **Theme Conflicts**: Temporarily switch to a default WordPress theme (like Twenty Twenty-Four) to rule out theme-related issues.

– **Plugin Conflicts**: Deactivate other plugins temporarily to check for conflicts that might prevent the column from displaying.

– **WooCommerce Admin Features**: Given that WooCommerce 9.8.5 introduces the new React-based Payments Settings page, it's possible that certain admin customizations behave differently. Ensure that your customizations are compatible with this new interface.

If you continue to experience issues or need further customization—such as making the column sortable or displaying different data—please let me know, and I can provide additional assistance.

Leave a Comment

Your email address will not be published. Required fields are marked *