Copy order items with metadata between orders – Woocommerce

Just change the $item_id to $to_order_item_id and you don’t even have to touch the order_item_meta since it is still associated with the proper $item_id. So your foreach from above would be …

foreach($order->get_items() as $item_id=>$item) {
    wc_update_order_item($item_id, array('order_id'=>$to_order_item_id));
}

Then you could even calculate the new totals of both orders after by using …

$original_order = new WC_Order($original_order_id);
$original_order->calculate_totals();
$order = new WC_Order($to_order_item_id);
$order->calculate_totals();

I also discovered sometimes woocommerce will not let you change the order item data so I ended up doing the same as above but with a straight to the database update instead …

global $wpdb;
$table="wp_woocommerce_order_items";
$data = array('order_id'=>$to_order_item_id);
foreach($order->get_items() as $item_id=>$item) {
    $where = array('order_item_id'=>$item_id);
    $updated = $wpdb->update($table, $data, $where);
    if($updated === false) {
        echo 'sorry dawg, there was an error';
    }
    else {
        echo 'you got the soup!';
    }
}

tech