Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active April 25, 2022 07:00
Show Gist options
  • Save trey8611/ea80860980a63ebd366eac2db1e0c8a0 to your computer and use it in GitHub Desktop.
Save trey8611/ea80860980a63ebd366eac2db1e0c8a0 to your computer and use it in GitHub Desktop.
WooCommerce Orders - Get total order weight in WP All Export
<?php
function my_get_total_order_weight( $order_id ) {
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
$total_qty = 0;
$total_weight = 0;
foreach ( $order_items as $item_id => $product_item ) {
$product = $product_item->get_product();
if ( ! $product ) continue;
$product_weight = $product->get_weight();
$quantity = $product_item->get_quantity();
$total_qty += $quantity;
$total_weight += floatval( $product_weight * $quantity );
}
return $total_weight;
}
@passatgt
Copy link

passatgt commented Sep 3, 2021

Check if the product exists, because it's possible the product was already deleted, but its still in an old order.

@trey8611
Copy link
Author

trey8611 commented Oct 5, 2021

Check if the product exists, because it's possible the product was already deleted, but its still in an old order.

Good call, I added a check.

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