0
votes

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();

        }

    }

}
1
Yes, this is only for edited orders that are changed in the backend. What I'm trying to do is check if the metadata values have changed and if so, do something with them. - Liam McArthur
That's correct. A coverstart value 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 McArthur
It uses update_post_meta to assign meta keys and values to the order, that's all. I have emailed you the link via your contact form. - Liam McArthur
I have answered with a good turn around… Now it's your turn to work :) - LoicTheAztec

1 Answers

2
votes

I have found a turn around that could work for you. I save a 2nd time the coverstart value in an additional and different post meta data key. So this way I have my initial coverstart value… You could even create an history of changes within an array stored in this additional custom meta field.

The code:

// Save an additional coverstart value in in the order post meta dat
add_action( 'woocommerce_checkout_create_order', 'initial_coverstart_custom_field_save', 20, 1 );
function initial_coverstart_custom_field_save( $order ) {
    if( ! isset($_POST['coverstart']) ) return;

    if( ! empty($_POST['coverstart']) ){
        $order->update_meta_data( '_coverstart0', sanitize_text_field( $_POST['coverstart'] ) );
    }
}

// Backend: Updating Order data
add_action( 'save_post_shop_order', 'process_offline_order' );
function process_offline_order( $post_id, $post, $update ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id; // Exit if it's an autosave

    // Initial coverstart value
    $initial_coverstart = get_post_meta( $post_id, '_coverstart0', true );

    // Updated coverstart value
    $updated_coverstart = get_post_meta( $post_id, 'coverstart', true );

    // RAW OUTPUT
    var_dump( $initial_coverstart );
    var_dump( $updated_coverstart );
    die();
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.