I'm using the save_post_{$post->post_type} hook, which fires once a post (order) has been saved.
The intention is to save product meta based on certain order statuses. This is the code I wrote/used for this:
add_action ( 'save_post_shop_order', function (int $postId, \WP_Post $post, bool $update): void {
$order = new WC_Order( $postId );
$order_status = $order->get_status();
$status_arrays = array( 'processing', 'on-hold', 'to-order', 'needed' );
if ( in_array($order_status, $status_arrays) ) {
return;
}
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
$final_product->update_meta_data( '_test', '_test' );
$final_product->save();
}
},
10,
3
);
However, I can't find the new metadata in my database. Any advice? can anyone help me how to achieve this?