Hello @alexmigf apologies I only saw this today.
I’m not sure if this is the best solution but what i did was let the suborder come in as a normal order and then restore the stocks. Please bear in mind that I am still learning php and no where near qualified lol. Might be a long read but see below if you wanna have a look. I hooked after (‘save_post_shop_order’) SLW is 10,3 Custom is 10,4.
//ADDED THIS CODE TO REVERSE BACK THE STOCK DEDUCTED IF A SUBORDER IS CREATED
add_action('save_post_shop_order','update_stock_locations_data_wc_order_save_custom',10,4);
function update_stock_locations_data_wc_order_save_custom($post_id,$post){
//GET AN INSTANCE OF WOOCOMMERCE $order OBJECT
$sub_order = wc_get_order( $post_id );
$parent_order = wp_get_post_parent_id( $sub_order->get_id());
//CHECK FOR PARENT
if ( ! $parent_order ) {
return;
}
//LOOP $sub_order ONLY IF $parent_order EXISTS
foreach ( $sub_order->get_items() as $sub_order_item_id => $sub_order_item ) {
if ( ! $sub_order_item->is_type( 'line_item' ) ) {
continue;
}
//WOOCOMMERCE PART
$sub_order_product_id = $sub_order_item->get_product_id();
$sub_order_qty = $sub_order_item->get_quantity();
$sub_order_current_stock = get_field('_stock',$sub_order_product_id);
//UPDATE
$sub_order_correct_stock = $sub_order_current_stock + $sub_order_qty;
update_post_meta($sub_order_product_id,'_stock',$sub_order_correct_stock);
if ($sub_order_correct_stock > 0) {
//UPDATE STATUS IN POSTMETA TABLE
update_post_meta($sub_order_product_id,'_stock_status','instock');
}
//SLW PART
$sub_order_slw_data = $sub_order_item->get_meta('_slw_data');
//LOOP THROUGH ARRAY DATA
foreach ($sub_order_slw_data as $sub_order_location_id => $sub_order_location ){
foreach ($sub_order_location as $key => $value){
$sub_order_quantity_subtracted = ($sub_order_location['quantity_subtracted']);
$sub_order_post_meta_key = '_stock_at_'.$sub_order_location_id;
$sub_order_current_stock_at_location = get_field($sub_order_post_meta_key,$sub_order_product_id);
$sub_order_updated_qty = $sub_order_current_stock_at_location + $sub_order_quantity_subtracted ;
//UPDATE
update_post_meta ($sub_order_product_id,$sub_order_post_meta_key,$sub_order_updated_qty);
}
}
}
}
If you have any suggestions or a better way i’d really appreciate it.