Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active March 4, 2024 19:36
Show Gist options
  • Save kloon/5299119 to your computer and use it in GitHub Desktop.
Save kloon/5299119 to your computer and use it in GitHub Desktop.
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';
}
}
}
?>
@famousmrthomas
Copy link

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

Fantastic, worked flawlessly!

@thnzrmzwr
Copy link

Awesome Brother works like a charm!

@Patrick-Hor
Copy link

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

@NewVeryNew
Copy link

Hi! I need to show the total weight of the order in the Frontend @ the Order of My Account. I have found the way to add the columns and show some product data but not the weight. Help will be much appreciated!

@Barnabas2
Copy link

Is there a way to store this value in an Advanced Custom Field or as order meta field?

@ben72
Copy link

ben72 commented May 24, 2023

The code is now giving:
Warning: A non-numeric value encountered in /home/public_html/wp-content/plugins/theme-customisations/custom/functions.php on line xx
Where xx=$weight += $_product->get_weight() * $item['qty'];
I fixed it by using the is_numeric on $product_weight before using it.

@Netzie
Copy link

Netzie commented Jun 15, 2023

Hi Lads,
You are all awesome - Can one of you please post the entire script now with all the new stuff so it's error free and PHP 8.2 freindly

@bonnsbonn
Copy link

Would it be possible to get an updated version of this code? I would like to run it will php 8.1 and above if possible please.

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