Support

Account

Home Forums Backend Issues (wp-admin) update_field is not working in action hook Reply To: update_field is not working in action hook

  • I think I’m having this problem as well. It’s really wierd.

    I’m building a custom WooCommerce-action that does a bunch of stuff for the given order. I initiate the action, by adding this to the functions.php-file:

    
    function custom_add_order_action( $actions ){
      global $theorder;
      $actions['custom_thing'] = 'My own custom action';
      return $actions;
    }
    add_action( 'woocommerce_order_actions', 'custom_add_order_action' );
    
    add_action( 'woocommerce_order_action_custom_thing', 'custom_action_content' );
    function custom_action_content( $order ){
      $my_class = new WcActionCustomActionClass( $order );
    }
    

    … and inside WcActionCustomActionClass a bunch of things happen.

    One of which should be to update the ACF-fields for the WooCommerce order. Let’s say that the order_id is 12345.

    Regardless of what I do, then I can’t get it to update _any_ of the fields, when I run update_field() inside my class, _unless_ if I write die()' afterwards(?!).

    So if I do this:

    
    update_field( 'test', 'some value', 12345 );
    

    and run the action, then the value of the test-field hasn’t changed.

    But if do this:

    
    update_field( 'test', 'some value', 12345 );
    die();
    

    … and then check it in another browser-window, then the value _does_ change. Really wierd!

    ————–

    ### Solution attempts

    **1. New field**

    I tried creating a new field and trying to update that. Exactly the same bug as described above.

    **2. Updating the field elsewhere**

    I tried updating the field in the init-hook, ensuring that my code actually works:

    
    function test_init(){
      update_field( 'test', 'another test value', 12345 );
    }
    add_action( 'init', 'test_init' );
    

    This updates the field (as it should).

    **3. Updating the field before my class is initialized**

    I tried changing my code, so it looks like this instead:

    
    ...
    function custom_action_content( $order ){
      update_field( 'test', 'another another test value', 12345 );
      $my_class = new WcActionCustomActionClass( $order );
    }
    

    **4. Using the field_key**

    I tried using the field key, like this:

    
    ...
    function custom_action_content( $order ){
      update_field( 'field_603f667437ba9', 'another another another test value', 12345 );
      $my_class = new WcActionCustomActionClass( $order );
    }
    

    … Same thing. No change.

    And again. If I do this:

    
    ...
    function custom_action_content( $order ){
      update_field( 'field_603f667437ba9', 'another another another test value', 12345 );
      die();
      $my_class = new WcActionCustomActionClass( $order );
    }
    

    … and open the order in a new window, then the value updates(?!). Omg…

    **5. get_the_object**

    Inside my class, I tried getting the field object, ensuring that it was accessible. So I did this:

    
    $field = get_field_object( 'field_603f667437ba9' );
    vardump( $field );
    die();
    

    and it returns an array of the field:

    
    array (
      "ID" => 48617
      "key" => "field_603f667437ba9"
      "label" => "Test"
      "name" => "test"
      "prefix" => "acf"
      "type" => "text"
    ...
    

    So it _is_ accessible! Hmm…

    **6. Moving the function up to fire earlier**

    I figured, that maybe something ‘got in the way’ of the ACF-function firing, so by moving it up to be the first thing that runs during that action, then maybe that could make a change.

    … But it was the same result: No change, unless I made it die() just after.

    **7. Moving the function, making it fire later**

    So that means doing this:

    
    ...
    function custom_action_content( $order ){
      $my_class = new WcActionCustomActionClass( $order );
      update_field( 'field_603f667437ba9', 'another another another test value', 12345 );
    }
    

    … Same thing. Doesn’t change, unless I write die(); after.

    **8. Clear cache and restart Apache/Nginx**

    Didn’t change anything.

    **9. Try using update_post_meta() instead**

    Same result.

    This doesn’t change anything:

    
    update_post_meta( 12345, 'test', 'Another another another... value' );
    

    This make the value change:

    
    update_post_meta( 12345, 'test', 'Another another another... value' );
    die();