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

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]

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 *