Ask SkyVerge

Time to start with our first Ask SkyVerge question of 2018 🙂 This question is from Houdayer:

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


Sure thing! We’re looking at the “Orders” menu in the my account page currently, which shows order number, date, status, total, and actions by default:

WooCommerce My Account > Orders default view

The woocommerce_my_account_my_orders_columns filter lets us add or adjust the columns included in this list, so we can add a new one for a shipping destination!

So our first step is to filter these columns, and add our new column in where we’d like. I’m going to loop through all existing columns and add them all to a new array so that I can put my new column where I’d like — after the status column:

/**
 * 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 sv_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['order-ship-to'] = __( 'Ship to', 'textdomain' );
        }
    }

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

Now, you’ll see the column added to this page:

WooCommerce My Account > Orders column added

Not done, yet! We need some data for this column that will appear in every row. The woocommerce_my_account_my_orders_column_{$column_id} action fires to let you populate the column you just added with content, and gives us access to the order for that row, so we’ll use this hook next.

The column ID will be the array key you’ve added in the previous snippet (ship-to in my case).

I’ll take the order that’s given to me, and get the shipping details to display in my column. Fortunately, WooCommerce 3.0+ has a handy method to output the formatted shipping display to make this simple:

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

    $formatted_shipping = $order->get_formatted_shipping_address();
    echo ! empty( $formatted_shipping ) ? $formatted_shipping : '–';
}
add_action( 'woocommerce_my_account_my_orders_column_order-ship-to', 'sv_wc_my_orders_ship_to_column' );

This will now ensure that, if the order has a shipping address, I’ve included it in this column (while orders without a shipping address, like those with virtual items, show nothing):

WooCommerce My Account > Orders column populated

That’s all you need 🙂 You can view the full snippet here or check it out below:

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

11 Comments

  1. Thank you so much for sharing this. I spent hours trying to figure this out. Exactly what I was looking for!

    Reply
  2. You are a life saver Beka. Exactly what I was looking for.

    Reply
  3. How to showing/display custom field on My Order like shipping to above ?

    Reply
  4. this plugin do what you want : Woo admin columns

    Reply
  5. Hello,
    Thank you for your post.
    How to add another column and populate it from “Admin Custom Order Fields”.
    One of the custom fields filled from within admin panel mus be shown in My Orders list of the Customer Panel for each order.
    Is it possible?

    Reply
  6. Hello, thank you for this post. That’s exactly what I was looking for. Please, is it possible to add another column and populate it with order items? Thanks

    Reply
  7. Hi!

    You dont have this function like an plugin? Will bee happy to pay for it!

    Reply
  8. Hi!

    Is there a equally simple way to add a column to the subscription table as well? Preferably with information from a custom checkout field populated at checkout?

    Reply
  9. Hi Beka,
    Thanks a lot for this magical snippet !

    I need something a little different, add a column which displays all items ordered.
    Could you help ?

    Sebastien Albert

    Reply

Leave a Reply

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