You have your duplicate_line_items()
function written so that the items from the first parameter, $source_order
are added to the second parameter, $new_order
, but in your first block of code you’re passing the old order as the 2nd parameter:
duplicate_line_items($new_order, $old_order);
So all the items from $new_order
are being added to $old_order
, not the other way round.
Make sure you’re using the variables consistently:
duplicate_line_items( $old_order, $new_order );
Maybe name your variables in duplicate_line_items()
a bit more clearly too:
function duplicate_line_items( $source_order, $target_order ) {
foreach ( $source_order->get_items() as $item ) {
$target_order->add_item( $item );
}
$target_order->save();
}
So with those two changes you’re properly setting $old_order
as the $source_order
and $new_order
as the $target_order
.