I just asked this question regarding hooking into an order after it's updated. Unfortunately, though I've realised that the meta is saved before, so I need to hook into the order BEFORE the order is saved so that I have both the original meta and the newly saved meta available in an array. Does anybody know what hook I could use to achieve this? Here's what I'm trying so far.
I've written the below code to hook into the order save function. I then go into one of my WooCommerce orders and change the custom coverstart meta to a different date. The code below should then dump the previous meta value that was assigned to coverstart, as well as the new value that has currently been saved.
//save_post_shop_order
add_action( 'save_post_shop_order', 'process_offline_order' );
function process_offline_order( $post_id, $post, $update ) {
// Orders in backend only
if ( ! is_admin() ) {
return;
}
// Get an instance of the WC_Order object (in a plugin)
$order = new WC_Order( $post_id );
$trigger_status = get_post_meta( $post_id, '_hook_is_triggered', true );
if ( $update ) {
// Not a new order
if ( 'Create new order' != $trigger_status ) {
$metaArray = $_POST['meta'];
foreach ( $metaArray as $meta => $key ) {
$metaArr[ $key["key"] ] = $key["value"];
}
// Data
$data = new WC_Order( $order );
$meta = get_post_meta( $order->ID );
// These should be different when altering an order and changing it
// ... but it's showing the same value for both
var_dump( $meta['coverstart'][0] );
var_dump( $metaArr['coverstart'] );
die();
}
}
}
coverstartvalue will always be assigned to every order because it's a custom required meta field in the checkout that has to be filled in (I used WooCommerce Checkout Field Editor Pro plugin to add this to the checkout). When we are editing an already-existing order, I need to get the previously set value before the save function overwrites it. - Liam McArthurupdate_post_metato assign meta keys and values to the order, that's all. I have emailed you the link via your contact form. - Liam McArthur