Add Custom Columns WooCommerce
January 31, 2025 · AI Conversations
We would also like to option to present the customers 'sex' and 'account/user ID' on the dashboard.
Again, ideally these should all be individual 'screen options' on the order dashboard so we can decide whether we want to enable and disable DOB, sex and/or account/user ID on the order dashboard as we chose (if this is simple enough).
in woocommerce programmatically
### **Steps:**
1. **Add Custom Columns** for DOB, Sex, and User ID.
2. **Retrieve Customer Meta Data** from the user profile.
3. **Make the Columns Optional** via **Screen Options**.
—
### **Code Implementation:**
Add this code to your **theme’s `functions.php` file** or in a custom plugin.
function custom_woocommerce_order_columns($columns) {
// Insert new columns after the 'order_status' column
$new_columns = [];
foreach ($columns as $key => $value) {
$new_columns[$key] = $value;
if ($key === 'order_status') {
$new_columns['customer_dob'] = __('DOB', 'woocommerce');
$new_columns['customer_sex'] = __('Sex', 'woocommerce');
$new_columns['customer_user_id'] = __('User ID', 'woocommerce');
}
}
return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'custom_woocommerce_order_columns', 20);
/**
* Populate custom columns
*/
function custom_woocommerce_order_column_content($column, $post_id) {
$order = wc_get_order($post_id);
$user_id = $order->get_user_id();
if ($user_id) {
$dob = get_user_meta($user_id, 'dob', true);
$sex = get_user_meta($user_id, 'sex', true);
if ($column === 'customer_dob') {
echo !empty($dob) ? esc_html($dob) : '—';
}
if ($column === 'customer_sex') {
echo !empty($sex) ? esc_html(ucfirst($sex)) : '—';
}
if ($column === 'customer_user_id') {
echo esc_html($user_id);
}
} else {
if (in_array($column, ['customer_dob', 'customer_sex', 'customer_user_id'])) {
echo '—';
}
}
}
add_action('manage_shop_order_posts_custom_column', 'custom_woocommerce_order_column_content', 20, 2);
/**
* Make the columns sortable
*/
function custom_woocommerce_order_sortable_columns($columns) {
$columns['customer_dob'] = 'customer_dob';
$columns['customer_sex'] = 'customer_sex';
$columns['customer_user_id'] = 'customer_user_id';
return $columns;
}
add_filter('manage_edit-shop_order_sortable_columns', 'custom_woocommerce_order_sortable_columns');
/**
* Add screen options to enable/disable columns
*/
function custom_woocommerce_order_screen_options() {
$screen = get_current_screen();
if ($screen->id !== 'edit-shop_order') {
return;
}
$options = [
'customer_dob' => 'DOB',
'customer_sex' => 'Sex',
'customer_user_id' => 'User ID'
];
foreach ($options as $key => $label) {
add_screen_option(
'customer_option_' . $key,
[
'label' => __($label, 'woocommerce'),
'default' => true, // Enabled by default
'option' => 'customer_option_' . $key
]
);
}
}
add_action('current_screen', 'custom_woocommerce_order_screen_options');
/**
* Hide columns based on screen options
*/
function custom_woocommerce_order_hidden_columns($hidden) {
$options = ['customer_dob', 'customer_sex', 'customer_user_id'];
foreach ($options as $option) {
$screen_option = get_user_option('customer_option_' . $option);
if ($screen_option === false || $screen_option === '0') {
$hidden[] = $option;
}
}
return $hidden;
}
add_filter('default_hidden_columns', 'custom_woocommerce_order_hidden_columns', 10, 2);
—
### **How This Works**
1. **Adds custom columns** for **DOB, Sex, and User ID** to the order list.
2. **Fetches metadata** from the customer's profile (`dob` and `sex` must be stored as user meta).
3. **Adds sorting functionality** for the new columns.
4. **Adds screen options** to enable/disable these columns as per your choice.
### **How to Enable/Disable Columns**
– Go to **WooCommerce → Orders**.
– Click **Screen Options (top-right corner)**.
– Check/uncheck **DOB, Sex, User ID** as needed.
—
### **Ensure Data Exists**
– The **DOB and Sex** must be stored in user meta.
– If your checkout form doesn’t capture these, you may need to **extend WooCommerce registration/checkout fields** to collect this information.
Let me know if you need help with that! 🚀