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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WC 3.9
 * @community     https://businessbloomer.com/club/
 */
 
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 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

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

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? 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 *