I am syncing orders between my WooCommerce store and a 3rd party tool, and I am re-making the sync program using the REST API. I want to get a batch of new orders, and after inserting them into the db I want to set a status or flag so that the next time I ask for a batch of orders, I don't get the same ones.
That details how to get orders, and it works. I don't want to use the STATUS field because shop managers can change it. I don't want to exclude a list of order IDs because it will become too long over time. Looking for a field that I can use to limit the orders returned to the ones I haven't seen yet, and be able to update that field after it has been successfully synced. Seem possible?
A field that already exists or a way to create one?
I want to know if there is a field to mark an order as being previously synced so that I can then request only the new orders that have yet to be synced. Seems like a common thing, and hoping someone has done this and can advise.
Updated
To flag an order, the only way is to add some custom metadata.
First, when an order is placed by a customer or created manually via the admin, we add our custom metadata to flag it (with a "no" value):
add_action( 'woocommerce_new_order', 'add_custom_order_metadata', 20, 2 );
function add_custom_order_metadata( $order_id, $order ) {
$order->add_meta_data('_order_synced', 'no', true);
$order->save();
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Below, I use PHP programming language to access WooCommerce REST API.
Then we query orders to sync that have as metadata "no" value for "_order_synced" key using a GET request:
$data = [
"meta_data": [
{
"key": "_order_synced",
"value": "no"
}
]
];
print_r($woocommerce->get('orders/batch', $data));
And finally, we will batch update that synced orders using a POST request, changing the metadata value:
$data = [
'update' => [
"meta_data": [
{
"key": "_order_synced",
"value": "yes"
}
]
]
];
print_r($woocommerce->post('orders/batch', $data));