WooCommerce: Display Total Weight @ Cart & Checkout

Following up from the other day’s snippet (deny checkout based on cart weight), you might want to show what is the current Total Weight on the cart and checkout page in case this is useful to your customers.

Of course, in order for this snippet to work, all your products must have a weight, otherwise the total will always be equal to 0. So here you go – enjoy!

Show total product weight at WooCommerce Cart & Checkout

PHP Snippet: Show Total Weight @ WooCommerce Cart & Checkout Pages

/**
 * @snippet       Display Weight @ Cart & Checkout - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WC 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_before_checkout_form', 'bbloomer_print_cart_weight' );
add_action( 'woocommerce_before_cart', 'bbloomer_print_cart_weight' );
 
function bbloomer_print_cart_weight() {
   $notice = 'Your cart weight is: ' . WC()->cart->get_cart_contents_weight() . get_option( 'woocommerce_weight_unit' );
   if ( is_cart() ) {
      wc_print_notice( $notice, 'notice' );
   } else {
      wc_add_notice( $notice, 'notice' );
   }
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

25 thoughts on “WooCommerce: Display Total Weight @ Cart & Checkout

  1. Great snippet. Only issue I have is that when the basket is shown, it shows the weight total on the right side summary as I need it. However, when I change a quantity, it appears as a notice on the top. Any way to keep the notice in the same place for this?

    add_action( 'woocommerce_cart_totals_before_shipping', 'bbloomer_print_cart_weight' );
    function bbloomer_print_cart_weight() {
       $notice = 'Total weight is: ' . WC()->cart->get_cart_contents_weight() . get_option( 'woocommerce_weight_unit' );
       if ( is_cart() ) {
          wc_print_notice( $notice, 'notice' );
       } else {
          wc_add_notice( $notice, 'notice' );
       }
    }

    .

    1. Yes, instead of wc_print_notice you’d echo a DIV instead. Also remove the “else” bit, you’re already in the cart

  2. Thank you for the awesome snippet. Though it is working perfectly for me, there are times when it shows the fixed weight for the cart as 577.6kg. This has happened thrice in last 5 days. Any suggestions?
    I have disable the snippet temporarily.

    1. Uhm, no idea. My snippet uses a default WooCommerce function so nothing wrong in there. Could be a cache problem, or one of your products that has a weird weight?

  3. I’m using this snippet but it shows the message twice, in the checkout page. What can the reason be?

    1. Even if you switch theme temporarily?

      1. I’m in a loophole because my staging site doesn’t have the problem, so it’s all right not to check with another theme. But on my production site if I install a new theme everything breaks.
        I have left a post on WordPress forum: https://wordpress.org/support/topic/message-button-shown-twice-at-checkout/

      2. I have found the culprit in a rich-snippet plugin. As soon as I deactivate that, your code works by showing the weight only once.
        Now the problem is that I need that plugin, and it’s strange that it clashes with your short and simple code. Could you suggest something to resolve it?

        1. Great to know your issue is solved. I’d suggest looking for a plugin alternative, if it breaks my simple snippet I’m sure it’s going to break even more WooCommerce functionalities. Either find a plugin alternative or get a developer to code the custom plugin for you. Hope this helps

  4. Great snippet, so simple! Thanks a bunch for sharing 🙂

  5. Hi Rodolfo,

    It still works great in WooCommerce 5.1, but only on cart and checkout pages. Is there a way to print the same notice in the mini cart too?

    1. Hi Alex, 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!

  6. Shame this doesn’t seem to work with the new WooCommerce blocks

    1. I don’t use blocks so not sure, sorry

  7. Hi Rodolfo,

    Thanks for sharing this. Very useful. 2 questions please.

    1. Is there are a way to stop auto scrolling to weight notice?

    2. Can I deny checkout based on weight for all countries except UK?

    Many thanks,

    Gary

    1. Hi Gary, 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. Can you please share code to add total cart volume in cart page too? (HxWxL=Volume)?
    I need something like: Total volume: xx m3.

    1. Hi there, 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!

  9. Use in PHP 7.2+ script is slowly or my theme have problem.

    1. I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

  10. Great snippet! Works with latest WC and PHP 7.1.
    Do you know if it´s possible to show the weight of each item in cart?

    1. Hey Alex – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution 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

  11. Dear Rodolfo,

    Could you help me how is it possible to add the total weight for the new order admin email? I have been trying to find out for 3 hours now, and can’t…

    Thanks a lot,

    Regina

    1. Regina, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.