1

I have a possibility to clone orders for each product. But I need to make a custom order ID with parent ID and add some number or letters for it. Depends, from quantity of cloning numbers in parent order.

Here is an example:

enter image description here

If I clone element from #1092 it should create a new order with #1092-1. The second cloning will create an #1092-2 etc.

Here is the small part of code:

function create_order( $original_order_id, ...
    try {
            $new_order = new WC_Order( ??? );
            $new_order->set_props( array( ...
2

Well.. You can add meta for original and cloned order. Just add +1 for it, each time when you will clone items.

function create_order( $original_order_id, ...
    try {
            $new_order = new WC_Order();
            $new_order->set_props( array( ...
    }

    $original_order = wc_get_order( $original_order_id );
    $order_item_num = $original_order->get_meta('_order_items_num');
    if($order_item_num >= 1) {
        $new_order->update_meta_data( '_order_item_num', $order_item_num+1 );
        $new_order->save();
        $original_order->update_meta_data( '_order_items_num', $order_item_num+1 );
        $original_order->save();
    } else {
        $new_order->update_meta_data( '_order_item_num', '1' );
        $new_order->save();
        $original_order->update_meta_data( '_order_items_num', '1' );
        $original_order->save();
    }

Now you can modify order number

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
function change_woocommerce_order_number( $order_id, $order ) {
    $parent_id = $order->parent_id;
    if($parent_id != 0) {
        $parent_order = wc_get_order( $parent_id );
        $order_sub_num = $order->get_meta('_order_item_num');
        return $parent_id . '-' . $order_sub_num;
    }
    return $order->id;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.