
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:
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:
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):
That’s all you need 🙂 You can view the full snippet here or check it out below:
Need some help taking this code further? We recommend Codeable for small custom projects.
Thank you so much for sharing this. I spent hours trying to figure this out. Exactly what I was looking for!
You are a life saver Beka. Exactly what I was looking for.
How to showing/display custom field on My Order like shipping to above ?
this plugin do what you want : Woo admin columns
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?
Hey Gentian, good news is you don’t need custom code. 🙂 You can use the “Display in View Orders screen” property for your field. You can see more details here.
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
Hi!
You dont have this function like an plugin? Will bee happy to pay for it!
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?
Great question — I’m afraid I don’t see hooks for this in the existing templates, so I’d reach out to Prospress to see if there are hooks I’m missing that would make this possible.
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
do you have lists of hooks. or do you know the specific hooks for admin notes and refund amount
Very nice guide! Works!
i have used wp hotel booking plugin..In that plugin discount option is not working..so that i have manually added two fields like discount and grand total..changes in template (wp-cart page)now its showing but in payment gateway page it was showing the total only not my grand total..so wt to do ? for getting grand total in payment gateway and also orederd bookings? can anyone help me? Is there any option to change in ajax?
hey i want to get all order no based on user id this is my code
SELECT user_id FROM
wp_usermeta
WHEREmeta_value
=’ONLINE'” where i get user_idid but after that
customer_orders = get_posts( array(
‘numberposts’ => -1,
‘meta_key’ => ‘_customer_user’,
‘meta_value’ => $ user_id,
‘post_type’ => wc_get_order_types(),
‘post_status’ => array_keys( wc_get_order_statuses() ), //’post_status’ => array(‘wc-completed’, ‘wc-processing’),
) );
but this give me blank array
or you know there is any wp_query to get all order no based on post type=shop_order pls replay
Thanks for sharing this helpful post. Is there a way to show new custom column before order number?
Sure — instead of inserting it into the middle of the
$new_columns
array, you could start this array off with your new column name, then loop through the existing ones to add them after it.Hey,
this is a very nice code snippet. But i need to add the product name instead if shipping adress. I got no plan about php. Can you please provide this?
greetings
[…] Add additional columns to the Orders menu on the My Account page for details such as shipping addresses or the date items were shipped. […]
Is there any way to make these columns sortable?
Possible, though pretty involved, and not something I have any examples handy for I’m afraid.