Created
June 6, 2020 19:25
-
-
Save Musilda/0ee0cbb0be46a0b905dbc6608eb8495d to your computer and use it in GitHub Desktop.
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 | |
//Save cart weight | |
add_action( 'woocommerce_checkout_update_order_meta', 'musilda_save_cart_weight' ); | |
function musilda_save_cart_weight( $order_id ) { | |
$weight = WC()->cart->get_cart_contents_weight(); | |
update_post_meta( $order_id, 'order-weight', $weight ); | |
} | |
//Update all orders | |
$args = array( | |
'status' => array( 'processing', 'completed'), | |
'limit' => -1, | |
); | |
$orders = wc_get_orders( $args ); | |
if( isset( $orders ) ){ | |
foreach ( $orders as $order ){ | |
$weight = 0; | |
foreach ( $order->get_items() as $key => $item_data ) { | |
$product = $item_data->get_product(); | |
$weight += $product->get_weight(); | |
} | |
update_post_meta( $order->get_id(), 'order-weight', $weight ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment