Search code examples
phpwordpresswoocommercemetadataorders

Display items total weight in WooCommerce Admin Orders list


I currently have a script calculating and displaying the weight of each order in a separate column on the orders page of my woocommerce site. This works well, but means everything must be calculated again with each page load.

I found a script that saves the order weight, but my php knowledge is not good enough to combine both scripts. What would this look like? In other words, display the saved weight and not calculate each time?

the saving script:

add_action( 'woocommerce_checkout_update_order_meta', 'solhatt_save_weight_order' );
 
function solhatt_save_weight_order( $order_id ) {
    $weight = WC()->cart->get_cart_contents_weight();
    update_post_meta( $order_id, '_cart_weight', $weight );
}

the calculating script:

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';
        }
    }
}

My php knowledge is just not good enough, I tried combining both but got errors...


Solution

  • Replace all your code (including the first function) with the following code, that will use directly '_cart_weight' metadata in admin orders list, to display the total formatted weight:

    add_action( 'woocommerce_checkout_create_order', 'add_order_total_weight_metadata' );
    function add_order_total_weight_metadata( $order ) {
        $order->add_meta_data('_cart_weight', intval( WC()->cart->get_cart_contents_weight() ) );
    }
    
    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 ) {
        if ( $column == 'total_weight' ) {
            global $the_order;
    
            // Get total weight metadata value
            $total_weight = $the_order->get_meta('_cart_weight');
    
            if ( $total_weight > 0 ) {
                echo wc_format_weight( $total_weight );
            } else {
                _e('N/A', 'woocommerce');
            }
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.