Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce total order weight column on orders page
<?php
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
$columns['total_weight'] = __( 'Weight', 'woocommerce' );
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
global $post, $woocommerce, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
$the_order = new WC_Order( $post->ID );
if ( $column == 'total_weight' ) {
$weight = 0;
if ( sizeof( $the_order->get_items() ) > 0 ) {
foreach( $the_order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $item->get_product();
if ( ! $_product->is_virtual() ) {
$weight += $_product->get_weight() * $item['qty'];
}
}
}
}
if ( $weight > 0 ) {
print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
} else {
print 'N/A';
}
}
}
?>
@trevormesnik
Copy link

trevormesnik commented Aug 2, 2013

Which file does this go in to display total weights?

@flistefliste
Copy link

flistefliste commented Jul 1, 2014

If the code doesn't work first time, just add a later priority to the main filter to avoid that WC clears columns while loading :

add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column',20 );

@mikloshenrich
Copy link

mikloshenrich commented Jul 5, 2014

Awesome. It works like a charm! Thank you.

@Webmetz
Copy link

Webmetz commented Mar 19, 2015

I am trying to pull the order weight using a UPS Worldship Export Plugin (myzoogu.com)

How can I modify the following code to pull weight as well? I was able to get the order column, but it would be great to some how store it in meta data.

$query = $wpdb->prepare( "SELECT orden.ID AS order_number,
                    orden.post_modified AS last_date,
                    orden.post_excerpt AS deliverynotes,
                    orden.post_modified AS formatteddate,
                    user.ID AS user_id,
                    user.user_email AS user_email,
                    user.display_name AS user_name,
                    user.user_login AS user_login
                    FROM {$wpdb->posts} AS orden
                    LEFT JOIN {$wpdb->postmeta} AS meta
                    ON (meta.post_id = orden.ID)
                    LEFT JOIN {$wpdb->users} AS user
                    ON (user.ID=meta.meta_value )
                    WHERE
                    orden.post_type='shop_order'
                    AND orden.post_status not in ('trash',  'auto-draft')
                    AND meta.meta_key='_customer_user'
                    AND orden.ID IN ({$orderIdsArrStr})" , '');

                $results = $wpdb->get_results($query);

                if ( !empty( $results ) ) {
                    $OrderXML = '<?xml version="1.0"?>
                <OpenShipments xmlns="x-schema:OpenShipments.xdr">';

                    foreach($results as $result) {

                        $OrderId=$result->order_number;
                        $user_email=$result->user_email;
                        $user_id=$result->user_id;
                       $NewOrderID=get_post_meta( $OrderId, "_order_number", true );

@Netzie
Copy link

Netzie commented Nov 4, 2018

Any chance this could be updated?
Maybe even combined with this: https://stackoverflow.com/questions/45701473/add-the-order-total-weight-to-woocommerce-new-order-email-notification
And to make it complete - For the customers too?: like this: https://da.wordpress.org/plugins/woo-order-weight/
One plugin to rule them all :-)

@MarkPraschan
Copy link

MarkPraschan commented Jan 10, 2019

Lines 10-14 saved me! Thanks @kloon!

@famousmrthomas
Copy link

famousmrthomas commented May 2, 2019

You sir are awesome! :)

@wealthyone
Copy link

wealthyone commented May 6, 2019

is there anyway this can be added to the Order page? ( the page with all the details on it)
Seems it is only on the Orders list)

@CreedHub
Copy link

CreedHub commented Nov 1, 2020

I got this in error log today:
"PHP Deprecated: WC_Abstract_Legacy_Order :: get_product_from_item
Use $item->get_product() instead."

fix:
replace L20
$_product = $the_order->get_product_from_item( $item );
by
$_product = $item->get_product();

However, now I get:
PHP Notice: id was called incorrectly. Order properties should not be accessed directly.

How to fix this?

@kloon
Copy link
Author

kloon commented Nov 2, 2020

I updated the gist to make it compatible with the latest version of WooCommerce.

@futyko
Copy link

futyko commented Feb 9, 2021

I have changed line 22 to avoid PHP Warning: A non-numeric value encountered

$weight += floatval($_product->get_weight()) * $item['quantity'];

@Barnabas2
Copy link

Barnabas2 commented Mar 19, 2021

Fantastic, worked flawlessly!

@thnzrmzwr
Copy link

thnzrmzwr commented Nov 27, 2021

Awesome Brother works like a charm!

@Patrick-Hor
Copy link

Patrick-Hor commented May 24, 2022

Hi, works fine thank you!
do you have any idea how to get an total order weight on each order in a customers my account? i searching desperatly 2 days to find a sollution for this, your code works for the admin, but want almost the same for customers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment