Skip to content

Instantly share code, notes, and snippets.

@NiklasHogefjord
Created July 29, 2013 13:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiklasHogefjord/6104328 to your computer and use it in GitHub Desktop.
Save NiklasHogefjord/6104328 to your computer and use it in GitHub Desktop.
WooCommerce - Display cart total weight on the cart page
<?php
/**
* WooCommerce
* --------------
*
* Display cart total weight on the cart page
*
*/
add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
function myprefix_cart_extra_info() {
global $woocommerce;
echo '<div class="cart-extra-info">';
echo '<p class="total-weight">' . __('Total Weight:', 'woocommerce');
echo ' ' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit');
echo '</p>';
echo '</div>';
}
@owlonline
Copy link

Awesome. this works very well - thanks! I would like to also show the weight of each item in the cart as well. Ideally just below the item name. What would be the code for that?

Copy link

ghost commented Jan 21, 2014

Newbie alert: Where do I insert this?

@razvan21
Copy link

razvan21 commented Jan 9, 2015

I think you should add the code in the function.php of your theme, miltersen.

@Costas58
Copy link

HI.. can anybody show me the path to where the code needs to be inserted? Thanks

@badaionly
Copy link

this works very well, but i want to place before total
Total
Subtotal : $100
Shipping : $100
Total Weight : 1000g <- how to place in here
Total : $200

Thnks before..

@javiercb
Copy link

javiercb commented Sep 10, 2016

in "plugins->woocommerce->templates->cart->cart-totals.php"
search for "" and add this code before

<tr>
<th>Total Weight</th>
<td><?php echo WC()->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit'); ?></td>
</tr>

@creativerios
Copy link

Just wanted to say that even though I'm a visual designer with very little coding experience, this worked out fabulously. Especially the code by @javiercb. Many thanks.

@IanMelia
Copy link

Hi, owlonline commented on 9 Sep 2013 wrote I would like to also show the weight of each item in the cart as well. Ideally just below the item name. What would be the code for that? - I am also looking to do this, do we have an answer

@gautammurarka
Copy link

gautammurarka commented Sep 24, 2017

Don't make any changes in the Plugin files. Just add the below code in Function.php file:

/** * Add Cart Weight to Cart and Checkout */ function wcw_cart() { global $woocommerce;
if ( WC()->cart->needs_shipping() ) : ?>
    <tr class="shipping">
        <th><?php _e( 'Weight', 'woocommerce-cart-weight' ); ?></th>

        <td><span class="label"><?php echo $woocommerce->cart->cart_contents_weight . ' ' . get_option( 'woocommerce_weight_unit' ); ?></span></td>
    </tr>
<?php endif;

}
add_action( 'woocommerce_cart_totals_after_order_total', 'wcw_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'wcw_cart' );

/**

  • Add Cart Weight to Mini Cart
    */
    function wcw_mini_cart() {
    if ( WC()->cart->needs_shipping() ) : ?>

    cart->cart_contents_weight . ' ' . get_option( 'woocommerce_weight_unit' ); ?>

}
add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'wcw_mini_cart' );

@alexd0001
Copy link

Hi thanks for this! - Do you know how can i set the weight with a "comma" "," instead a "dot" "."? - alex

@protorob
Copy link

Super nice snippet, thanks.
Any suggestion on how to maker it update with the ajax cart?
It only update the value when the page is reffreshed.
Thanks in advance.

@nbvcw
Copy link

nbvcw commented Jan 21, 2020

Hello @protorob , were you able to find a solution for your query ? I'm having the same problem and were wondering if you could share your findings with me, if you don't mind. Thanks, -nbvcw

@ospiotr
Copy link

ospiotr commented Jan 30, 2020

Hi @nbvcw - you can use this plugin instead: https://wordpress.org/plugins/woo-cart-weight/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment