Skip to content

Instantly share code, notes, and snippets.

@popforce
Forked from kloon/functions.php
Last active December 17, 2019 12:56
Show Gist options
  • Save popforce/e3efbe4e1b02165b28ac292596dbe600 to your computer and use it in GitHub Desktop.
Save popforce/e3efbe4e1b02165b28ac292596dbe600 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 = $the_order->get_product_from_item( $item );
if ( ! $_product->is_virtual() ) {
$weight += $_product->get_weight() * $item['qty'];
}
}
}
}
if ( $weight > 45 ){
print '<span style="color: orange;">'.$weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ).'</span>';
}elseif ($weight > 0){
print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
}else{
print 'N/A';
}
}
}
?>
@popforce
Copy link
Author

popforce commented Oct 9, 2019

Updated to prevent direct access notice, added color coding for orders above weight 45.

@lukasprelovsky
Copy link

Perfect job :) thx

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