Last active
April 25, 2022 07:00
-
-
Save trey8611/ea80860980a63ebd366eac2db1e0c8a0 to your computer and use it in GitHub Desktop.
WooCommerce Orders - Get total order weight in WP All Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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
Check if the product exists, because it's possible the product was already deleted, but its still in an old order.