Add Custom Data as a New Column in the WooCommerce My Orders list in My Account page

enquerer > Blog > Uncategorized > Add Custom Data as a New Column in the WooCommerce My Orders list in My Account page

Is there a way to add shipping information in the table at MyAccount > Orders?

Yes, & of course. First check `Orders` menu on my account page, which shows order number, date, status, total, and actions by default.

Our first step is to filter these columns and add our new column in the location we’d like.

/**
     * Adds a new column to the "My Orders" table in the account.
     *
     * @param string[] $columns the columns in the orders table
     * @return string[] updated columns
     */
    function th_wc_add_my_account_orders_column( $columns ) {

        $new_columns = array();

        foreach ( $columns as $key => $name ) {

            $new_columns[ $key ] = $name;

            // add ship-to after order status column
            if ( 'order-status' === $key ) {
                $new_columns['new-data'] = __( 'New Data', 'textdomain' );
            }
        }

        return $new_columns;
    }
    add_filter( 'woocommerce_my_account_my_orders_columns', 'th_wc_add_my_account_orders_column' );

Now we need to add some data for this column that will appear in every row. We can use `woocommerce_my_account_my_orders_column_{$column_id}` action hook to populate content in every row. The column ID will be the array key we have added in the previous snippet. In my case, my column ID is `newdata` as per the above code snippet.


    /**
     * Adds data to the custom "new-data" column in "My Account > Orders".
     *
     * @param \WC_Order $order the order object for the row
     */
    function th_wc_my_orders_new_data_column( $order ) {

        $new_data = get_post_meta( $order->get_id(), 'new_data', true ); // Get custom order meta
        echo ! empty( $new_data ) ? $new_data : '–';
        
    }
    add_action( 'woocommerce_my_account_my_orders_column_new-data', 'th_wc_my_orders_new_data_column' );

Done! The complete code snippet is given below. Add this snippet in your child theme’s functions.php file or in your custom plugin file and do the modification as per your data & requirements.

/**
     * Adds a new column to the "My Orders" table in the account.
     *
     * @param string[] $columns the columns in the orders table
     * @return string[] updated columns
     */
    function th_wc_add_my_account_orders_column( $columns ) {

        $new_columns = array();

        foreach ( $columns as $key => $name ) {

            $new_columns[ $key ] = $name;

            // add ship-to after order status column
            if ( 'order-status' === $key ) {
                $new_columns['new-data'] = __( 'New Data', 'textdomain' );
            }
        }

        return $new_columns;
    }
    add_filter( 'woocommerce_my_account_my_orders_columns', 'th_wc_add_my_account_orders_column' );
    
    /**
     * Adds data to the custom "new-data" column in "My Account > Orders".
     *
     * @param \WC_Order $order the order object for the row
     */
    function th_wc_my_orders_new_data_column( $order ) {

        $new_data = get_post_meta( $order->get_id(), 'new_data', true ); // Get custom order meta
        echo ! empty( $new_data ) ? $new_data : '–';
        
    }
    add_action( 'woocommerce_my_account_my_orders_column_new-data', 'th_wc_my_orders_new_data_column' );  

Leave a Comment