Table of Contents
Weight in the order details
Cart Weight allows displaying the weight of the products in the cart, minicart and checkout. If you would like to display the weight also in the order details, it would be necessary to place the code in the functions.php file. You can find this file in the Appearance > Theme File Editor > Theme Files tab.
Code:
<?php
add_action( 'woocommerce_admin_order_totals_after_total', function ( $order_id ) {
$order_weight = 0.0;
try {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ) {
try {
$product = $item->get_product();
$order_weight += $product->get_weight() * $item->get_quantity();
} catch (Exception $e) {
// do nothing.
}
}
} catch (Exception $e) {
// do nothing.
}
if ( $order_weight === 0.0 ) {
return;
}
?>
<tr>
<td colspan="3"><hr/></td>
</tr>
<tr>
<td class="label"><?php esc_html_e( 'Weight', 'woocommerce' ); ?>:</td>
<td width="1%"></td>
<td class="total">
<span class="amount">
<?php echo wc_format_weight( $order_weight ); ?>
</span>
</td>
</tr>
<?php
} );
Note: If in the functions.php file there is already some code, it is necessary to omit <?php
Below, you can find the weight display in the order details: