Skip to content

Instantly share code, notes, and snippets.

@Musilda
Created June 6, 2020 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Musilda/0ee0cbb0be46a0b905dbc6608eb8495d to your computer and use it in GitHub Desktop.
Save Musilda/0ee0cbb0be46a0b905dbc6608eb8495d to your computer and use it in GitHub Desktop.
<?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