WooCommerce: Save & Display Order Total Weight

For tracking purposes, or maybe because your shop manager needs to be aware of this, saving the total weight of each order and displaying it on the single order admin page is quite simple.

That’s right – WooCommerce does not save this value by default. You either need to save it yourself into the “order meta” or recalculate the weight based on the order items and their quantities. Here, we’ll cover option one (saving is better than calculating in regard to performance).

Enjoy ๐Ÿ™‚

Display the order weight @ order admin

PHP Snippet: Save Order Weight & Display It @ WooCommerce Order Admin

/**
 * @snippet       Save & Display Order Total Weight - WooCommerce Order
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_weight_order' );

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

add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_delivery_weight_display_admin_order_meta', 10, 1 );
 
function bbloomer_delivery_weight_display_admin_order_meta( $order ) {    
    echo '<p><strong>Order Weight:</strong> ' . get_post_meta( $order->get_id(), '_cart_weight', true ) . get_option( 'woocommerce_weight_unit' ) . '</p>';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Get Order Data (total, items, etc) From $order Object
    As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
  • WooCommerce: Allow Users to Edit Processing Orders
    How can WooCommerce customers edit an order they just placed and paid for? I swear I looked on search engine results and other places before coming to the conclusion I needed to code this myself. For example, a user might want to change the delivery date (if you provide this on the checkout page). Or […]
  • WooCommerce: How to Add a Custom Order Status
    All WooCommerce orders go to either “processing“, “completed“, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

26 thoughts on “WooCommerce: Save & Display Order Total Weight

  1. Dosen’t work with the new standard Woocommerce Gutenberg checkout blocks – no weight is saved to orders when using them

    Still works with the old checkout-system added through shortcodes.

    1. Welcome to the world of non-customizable Checkout Block!

  2. This code no longer works. It displays “Order Weight: lbs”, but no number. ๐Ÿ™

    1. Hey Daniel, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  3. D’oh!

    Would this might be the issue?
    I added the snippet to code snippets, and then I had it set to “Only run in administration area”.
    Maybe the “woocommerce_checkout_update_order_meta” runs on front-end?

    1. Could be, yes, give it a go!

      1. Hi! Is there a way to show this information on the Order tab of the Account Frontend?
        Thanks!

        1. Hi Claudio, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hello, I tested your code and it works fine. but it is possible to display the total sales by weight in the form of a report, in tonnes for example. ex: product1 = 1000 tonnes or by product category. ex: cat1 = 500 tonnes

    1. Hi Zambol thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Hello and many thanks for this snippet !
    I added this code and it works really good , but only in the orders coming from the front end.
    When i manually add an order from the admin area , the order weight is not being updated ๐Ÿ™
    Any ideas why?
    Many thanks

    1. No idea sorry, you probably need to slightly edit the code

  6. Hi Rodolfo,
    I use this code and work really well.
    But Now I m using the follow code to resolve a big delay of Webhooks in WooCommerce. when I use this code the Webhooks work really fast but Your code is ignored

    add_filter( ‘woocommerce_webhook_deliver_async’, ‘__return_false’ );

    1. Not sure Edoardo!

  7. hi Rodolfo,

    Thanks for this handy snippet of code. I will give it a shot to check if it works with new orders. MEanwhile i have a question and that is can we include the order weight in the new order emails which is sent to the admin. Please comment.

    1. Hello Abhinav, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. Hello

    I tried but the problem is that code does not show value (Order Weight: kg). How can I fix that? Maybe there is a problem with code on WordPress 5.3.2

    1. This will only work for NEW orders and also if ALL products in the order have weight – let me know

  9. Hello,

    Is there a way to show variation weight or variation description on individual order-item-line? So, not the total weight, just individual!!

    Hope you`ll help

    1. Hi Perusi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. Ohhh I forgot the php tags ๐Ÿ™‚ And I can’t edit — Sorry!

    1. Added them for you ๐Ÿ™‚ Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  11. Hi Rodolfo,

    Thanks for this…
    I already have 2 of them in my functions.php ๐Ÿ™‚ One to show on the “orders overview” page and one inside the order and print it on the order email – that is on my table when I am doing / packing and printing the Shipment label

    Any chance it can be combined into one – to rule them all?

    Here is the two snippets

    /**
    * Here is that custom function that will show on "Order page overview" the total weight pr. order
    * gist.github.com/kloon/5299119
    */
    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-&gt;id != $post-&gt;ID )
    		$the_order = new WC_Order( $post-&gt;ID );
    	if ( $column == 'total_weight' ) {
    		$weight = 0;
    		if ( sizeof( $the_order-&gt;get_items() ) &gt; 0 ) {
    			foreach( $the_order-&gt;get_items() as $item ) {
    				if ( $item['product_id'] &gt; 0 ) {
    					$_product = $the_order-&gt;get_product_from_item( $item );
    					if ( ! $_product-&gt;is_virtual() ) {
    						$weight += $_product-&gt;get_weight() * $item['qty'];
    					}
    				}
    			}
    		}
    		if ( $weight &gt; 0 )
    			print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
    		else print 'N/A';
    	}
    }
    
    1. And the second Snippet (If I add yours to the buttom of this … The site crashes? ๐Ÿ™

      /**
      * Here is that custom function hooked in woocommerce_email_after_order_table action hook, that will show on "New order" email notification the total weight
      * stackoverflow.com/questions/45701473/add-the-order-total-weight-to-woocommerce-new-order-email-notification
      */
      add_action('woocommerce_email_after_order_table','show_total_weight', 10, 4);
      function show_total_weight( $order, $sent_to_admin, $plain_text, $email ){
      
          if ( 'new_order' != $email-&gt;id ) return;
      
          $total_weight = 0;
      
          foreach( $order-&gt;get_items() as $item_id =&gt; $product_item ){
              $quantity = $product_item-&gt;get_quantity(); // get quantity
              $product = $product_item-&gt;get_product(); // get the WC_Product object
              $product_weight = $product-&gt;get_weight(); // get the product weight
              // Add the line item weight to the total weight calculation
              $total_weight += floatval( $product_weight * $quantity );
          }
      
          // Styles
          $style1 = 'style="width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; color: #737373; border: 1px solid #e4e4e4; border-top:0;"';
          $style2 = 'style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
          $style3 = 'style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
      
          // Output
          echo "" . __( 'Total Product Weight: ', 'woocommerce' ) . "" . $total_weight . " kg.";
      }
      
  12. Do you think this would be better transferred to the order details and to the print order or packing list.

    If for example, the shipping does not have access to the woocommerce backend.

    1. Hey Roberta, yes, you can display it wherever you like – just change hook ๐Ÿ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *