Show the Total Weight in the Cart and Checkout
Adds a "Total weight" row to the cart and checkout totals, useful for stores that charge shipping by weight.
Opens WPIDE on
Code
$wpide_cart_weight_row = function () {
if ( ! WC()->cart ) {
return;
}
$weight = (float) WC()->cart->get_cart_contents_weight();
if ( $weight <= 0 ) {
return;
}
printf(
'<tr class="wpide-cart-weight"><th>%s</th><td data-title="%s">%s</td></tr>',
esc_html( 'Total weight' ),
esc_attr( 'Total weight' ),
esc_html( wc_format_weight( $weight ) )
);
};
add_action( 'woocommerce_cart_totals_before_order_total', $wpide_cart_weight_row );
add_action( 'woocommerce_review_order_before_order_total', $wpide_cart_weight_row );
Notes
Uses the weight unit set in WooCommerce > Settings > Products.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.