The objective is to get/query/loop through all orders which have order_status "Processing", and if they have no items in backorder, update an advanced custom field 'internal_status' to value 'Ready to Pack'
Right now for a method of testing (to keep it simple and see if it's working) I am just trying to update the custom field whenever an order is passed to status "Completed" (no condition for "items in backorder" yet)
Based on Auto completed status for all existing processing orders in WooCommerce answer code, here is my code attempt:
function auto_update_orders_internal_status(){
// Get all current "processing" customer orders
$processing_orders = wc_get_orders( $args = array(
'numberposts' => -1,
'post_status' => 'wc-processing',
) );
if(!empty($processing_orders))
foreach($processing_orders as $order)
add_action('acf/save_post', 'update_internal_status_ready_to_pack');
}
add_action( 'woocommerce_order_status_completed', 'auto_update_orders_internal_status' );
function update_internal_status_ready_to_pack ( $order_id ) {
$internalstatus = 'Ready to Pack';
update_field( 'internal_status', $internalstatus, $order_id );
}
One thing I am aware I don't fully grasp here is the method of querying / getting all orders on "Processing status" and updating their corresponding fields.