Last active
March 4, 2024 19:36
-
-
Save kloon/5299119 to your computer and use it in GitHub Desktop.
WooCommerce total order weight column on orders page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.