Order Dashboard Customization

User
We would like the customers date of birth to be present on the order dashboard. This way we will not need to load the order to view the date of birth.
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).
Thanks,
Assistant
To achieve this in WooCommerce, you’ll need to customize the **Orders Dashboard** by adding extra columns for **Date of Birth (DOB), Sex, and User ID**. Additionally, making these columns **optional via "Screen Options"** requires registering them properly with WordPress.

### **Steps to Implement:**

#### **1. Store and Retrieve the Customer's DOB & Sex**
If you’re already capturing **DOB** and **Sex** during checkout or via user profile fields, ensure they are saved in `usermeta` or `order meta`.
For example, if they are stored as:
– `billing_dob`
– `billing_sex`

#### **2. Add Columns to WooCommerce Orders Dashboard**
Add new columns using the `manage_edit-shop_order_columns` filter:

function custom_woocommerce_order_columns( $columns ) {
    $columns['customer_dob']  = __( 'Date of Birth', 'your-textdomain' );
    $columns['customer_sex']  = __( 'Sex', 'your-textdomain' );
    $columns['customer_id']   = __( 'User ID', 'your-textdomain' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_woocommerce_order_columns' );

#### **3. Populate the Custom Columns**
Fetch data from the order and display it using the `manage_shop_order_posts_custom_column` action:

function custom_woocommerce_order_column_content( $column, $post_id ) {
    $order = wc_get_order( $post_id );

    if ( 'customer_dob' === $column ) {
        $dob = get_post_meta( $post_id, 'billing_dob', true );
        echo $dob ? esc_html( $dob ) : '-';
    }

    if ( 'customer_sex' === $column ) {
        $sex = get_post_meta( $post_id, 'billing_sex', true );
        echo $sex ? esc_html( ucfirst( $sex ) ) : '-';
    }

    if ( 'customer_id' === $column ) {
        $customer_id = $order->get_user_id();
        echo $customer_id ? esc_html( $customer_id ) : '-';
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_woocommerce_order_column_content', 10, 2 );

#### **4. Make Columns Optional via "Screen Options"**
To allow toggling these columns in the **Screen Options** panel:

function custom_woocommerce_order_columns_screen_options( $columns ) {
    $columns['customer_dob'] = __( 'Date of Birth', 'your-textdomain' );
    $columns['customer_sex'] = __( 'Sex', 'your-textdomain' );
    $columns['customer_id']  = __( 'User ID', 'your-textdomain' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_woocommerce_order_columns_screen_options' );

### **Final Outcome**
– **Date of Birth, Sex, and User ID will appear in the Orders Dashboard.**
– **You can enable/disable them from "Screen Options".**
– **Values are fetched from order meta (`billing_dob` and `billing_sex`) and user ID.**

Would you like to format the **DOB** (e.g., `DD/MM/YYYY` instead of `YYYY-MM-DD`)? Let me know if you need further refinements!

User
How to add columns in screen options
Assistant
To add **optional columns** in the WooCommerce **Orders Dashboard** and allow enabling/disabling them via **Screen Options**, follow these steps:

### **1. Register Custom Columns in the Orders List**
Add new columns for **Date of Birth (DOB), Sex, and User ID**:

function custom_woocommerce_order_columns( $columns ) {
    $columns['customer_dob']  = __( 'Date of Birth', 'your-textdomain' );
    $columns['customer_sex']  = __( 'Sex', 'your-textdomain' );
    $columns['customer_id']   = __( 'User ID', 'your-textdomain' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_woocommerce_order_columns' );

### **2. Populate Custom Column Data**
Retrieve the stored **DOB**, **Sex**, and **User ID** for each order:

function custom_woocommerce_order_column_content( $column, $post_id ) {
    $order = wc_get_order( $post_id );

    if ( 'customer_dob' === $column ) {
        $dob = get_post_meta( $post_id, 'billing_dob', true );
        echo $dob ? esc_html( date( 'd/m/Y', strtotime( $dob ) ) ) : '-';
    }

    if ( 'customer_sex' === $column ) {
        $sex = get_post_meta( $post_id, 'billing_sex', true );
        echo $sex ? esc_html( ucfirst( $sex ) ) : '-';
    }

    if ( 'customer_id' === $column ) {
        $customer_id = $order->get_user_id();
        echo $customer_id ? esc_html( $customer_id ) : '-';
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_woocommerce_order_column_content', 10, 2 );

### **3. Make Columns Optional in "Screen Options"**
Use the `set_screen_option` filter to add toggles in **Screen Options**:

function custom_woocommerce_order_screen_options() {
    add_filter( 'default_hidden_columns', function( $hidden ) {
        $hidden[] = 'customer_dob';
        $hidden[] = 'customer_sex';
        $hidden[] = 'customer_id';
        return $hidden;
    } );
}
add_action( 'admin_head', 'custom_woocommerce_order_screen_options' );

This ensures the new columns are **hidden by default**, but users can **enable them manually** in **Screen Options** (top-right of the orders page).

### **4. Preserve User Preferences for Columns**
If you want to save user preferences for showing/hiding columns:

function custom_woocommerce_order_columns_screen_options( $columns ) {
    $columns['customer_dob'] = __( 'Date of Birth', 'your-textdomain' );
    $columns['customer_sex'] = __( 'Sex', 'your-textdomain' );
    $columns['customer_id']  = __( 'User ID', 'your-textdomain' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_woocommerce_order_columns_screen_options' );

### **Final Outcome**
✅ **New columns** for **DOB, Sex, and User ID** in WooCommerce Orders Dashboard.
✅ Users can **enable/disable** these columns via **Screen Options** (top-right menu).
✅ **User preferences are saved** to keep their preferred settings.

Would you like me to further refine this or add styling to match WooCommerce's look?

Leave a Comment

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