• Resolved ravensays

    (@ravensays)


    We are able to use the ‘action’ calls on the admin form tab actions. The calls to function.php appear to be working. The problem is that the ‘update’ to a field (field_5da06fb959057) is not showing up on the current form we are creating. The field is being updated somewhere in the background of ACF and then the next time the form is used it now has and will save that field with the prior forms ‘action’ update. If we toggle the ‘load’ fields on the form for that field (field_5da06fb959057) we see the previous forms calculated value.

    If we go into the DB and call up the post-meta data the posts for this field are always one post behind. We tried doing the same action (with a different action name) before and after the ‘create post’ action but the results are the same. What are we missing (doing wrong)?

    So we have actions in this order: set_final_amt, post, aft_set_final_amt
    And here is our code:
    //////////////////////////////////////////////////////////////
    add_action(‘acfe/form/submit/action/set_final_amt’, ‘set_final_amt’, 10, 2);
    function set_final_amt($form, $post_id){

    //Get amt fields(‘realm_select’ , ‘asgard_gift_amount’);
    $realm_amt = get_field(‘realm_select’);
    $asgard_amt = get_field(‘asgard_gift_amount’);
    $final_amt = get_field(‘field_5da06fb959057’);

    // Do something in set_final_amt
    if ($realm_amt[‘value’] == 0 )
    {
    $gift_amt = $asgard_amt * 100;
    }
    else
    {
    $gift_amt = $realm_amt[‘value’];
    }

    $fieldkey = “field_5da06fb959057”;
    $value = $gift_amt;
    update_field ($fieldkey, $value);

    // if ( function_exists( ‘update_field’ ) ){
    // update_field( $fieldkey, $gift_amount, $post_id );
    // if ( isset( $_POST[‘acf’][$fieldkey] ) )
    // echo ‘yoha’;
    // unset($_POST[‘acf’][$fieldkey] );
    // echo ‘haha’;
    // }

    echo ‘ra’, ‘ ‘, $realm_amt[‘value’], ‘ ‘, ‘ga’, ‘ ‘, $gift_amt, ‘ ‘, ‘aa’, ‘ ‘, $asgard_amt, ‘ ‘, ‘va’, ‘ ‘, $value, ‘ ‘, ‘ca’, ‘ ‘, $final_amt;

    $post_final_amt = get_field(‘field_5da06fb959057’);
    echo ‘pfa’, ‘ ‘, $post_final_amt;
    }
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    add_action(‘acfe/form/submit/action/aft_set_final_amt’, ‘aft_set_final_amt’, 10, 2);
    function aft_set_final_amt($form, $post_id){

    //Get amt fields(‘realm_select’ , ‘asgard_gift_amount’);
    $realm_amt = get_field(‘realm_select’);
    $asgard_amt = get_field(‘asgard_gift_amount’);
    $final_amt = get_field(‘field_5da06fb959057’);

    // Do something in set_final_amt
    if ($realm_amt[‘value’] == 0 )
    {
    $gift_amt = $asgard_amt * 100;
    }
    else
    {
    $gift_amt = $realm_amt[‘value’];
    }

    $fieldkey = “field_5da06fb959057”;
    $value = $gift_amt;
    update_field ($fieldkey, $value);

    // if ( function_exists( ‘update_field’ ) ){
    // update_field( $fieldkey, $gift_amount, $post_id );
    // if ( isset( $_POST[‘acf’][$fieldkey] ) )
    // echo ‘yoha’;
    // unset($_POST[‘acf’][$fieldkey] );
    // echo ‘haha’;
    // }

    echo ‘ra’, ‘ ‘, $realm_amt[‘value’], ‘ ‘, ‘ga’, ‘ ‘, $gift_amt, ‘ ‘, ‘aa’, ‘ ‘, $asgard_amt, ‘ ‘, ‘va’, ‘ ‘, $value, ‘ ‘, ‘ca’, ‘ ‘, $final_amt;

    $post_final_amt = get_field(‘field_5da06fb959057’);
    echo ‘pfa’, ‘ ‘, $post_final_amt;
    }
    //////////////////////////////////////////////////////////////

    Do we need to set the priorities differently. We just used 10 and believe that your process is setting and running them by how they are ordered on the actions tab. That is why we tried both before and after ‘create post’ action.

    Thank you in advance? It would be create if someone, I can’t because I do not understand it yet, made a simple flowchart diagram for ACF or ACF-ext that shows where fields/data are coming and going to during processing. Obviously our calculated field is being updated somewhere but not in our new post yet all all the other fields are pulled from current form and being used/set in the current post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    After reading your post, I think there is a little misunderstang of what get_field() is doing in your action. Here is an example:

    
    add_action('acfe/form/submit/action/set_final_amt', 'set_final_amt', 10, 2);
    function set_final_amt($form, $current_post_id){
    
        /*
         * Get current form input value
         */
        
        // Input realm_select
        $realm_amt = get_field('realm_select');
        
        // Input asgard_gift_amount
        $asgard_amt = get_field('asgard_gift_amount');
        
        
        /*
         * Get classic field from post ID: 456
         */
         
        // Get the field realm_select
        $realm_amt = get_field('realm_select', 456);
        
        // Get the field asgard_gift_amount
        $asgard_amt = get_field('asgard_gift_amount', 456);
        
        // ...
        
        // Update field for post ID: 456
        update_field('my_field', 'my_value', 456);
        
    }
    

    As you can see, in the form action, if you use get_field() without the $post_id parameter, it will simply get the current form field input value. If you want to get the field from a specific $post_id, you need to set the second parameter.

    Same goes for update_field() inside the action, you should provide the $post_id parameter in order to target a specific post, otherwise, the update field won’t work there. Remember: in this hook, you’re inside your form submit action, you should provide context to your functions.

    If you’re creating a post/updating a post, I suggest you to use the hook: acf/save_post (https://www.advancedcustomfields.com/resources/acf-save_post/). This hook is used in front-end and in the back-end when you create/update a post.

    This way you only have to write one code, and it will be used for both sides. Here is an example of use:

    
    // Priority: 20 (after fields are saved)
    add_action('acf/save_post', 'my_acf_save_post', 20); 
    function my_acf_save_post($post_id){
        
        // Target my_post_type save only
        
        if(get_post_type($post_id) !== 'my_post_type')
            return;
            
        // Get the field realm_select
        $realm_amt = get_field('realm_select', $post_id);
        
        // Get the field asgard_gift_amount
        $asgard_amt = get_field('asgard_gift_amount', $post_id);
        
        // ...
        
        // Update field for the current post save
        update_field('my_field', 'my_value', $post_id);
        
        // Target only when the save come from a front-end form
        if(acfe_form_is_front()){
        
            // Do something specific
        
        }
        
    }
    

    I hope I helped you to understand it better! ??

    Regards.

    PS: If you want to share some code, please use https://pastebin.com as it’s more readable.

    Thread Starter ravensays

    (@ravensays)

    My apologies for not using the pastebin . I did not understand how to paste the code properly until now. Thank you.

    Thank you for sharing and trying to help us learn. Part of the issue, at least for me, is not sure of what the color coding means on ACF documentation. I read and reread using the save post before using your plugin and now again after what you just wrote for us. That is why I mentioned flow chart (some kind of pictures) because to those that know how it works it is simple. To us ‘simple’ people that do not understand it is still all hidden behind the clouds lol.

    For instance your explanation to me still doesn’t clear up why the next new post I go to create has the calculated field info from the prior post I created (but only that field has the old data). Where was it hiding and if it was hid somewhere why couldn’t the current new post see it. It seems the ‘post_id’ is the juggler of our data that is laughing at me.

    Also forgive me I was seeing your ‘submit’ action code as being the ‘acf save post’ just worded differently for use in your admin actions tabs.

    I will try to work through this some more and hopefully the light bulb will go on and stay on or not explode :O)

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello Ravensays,

    To be honest it’s hard to understand what you’re trying to achieve without the complete context. However, the latest ACF Extended 0.8.2 update tweaked some ACF Extended Forms hooks. You now have complete code examples for each action in a new tab called “Advanced” (in the action).

    It looks like you want to do something on post update with ACF Extended Forms, so you should use the “Post Action” and take a look at the Advanced tab. In fact, actions are not tied to each other, because one action can add a post, and an another one add a user. So if you want to do special operations with a specific post on update, you should stay with that action (and not add a custom action).

    Let me know if you found a way to achieve your goal, maybe we can try to setup a simple example form hook for your case ??

    Regards.

    Thread Starter ravensays

    (@ravensays)

    Thank you! We will look at it. Will also 5* and a small $ from us personally (sep from foundation) for your work on plugin and help here. Safe Journey.

    Thread Starter ravensays

    (@ravensays)

    I FINALLY (YEAH !!) was able to get my calculated field to grab values from form, do a calculation based on those values, and most importantly then update another field on the form which is then saved to newly created post in DB. I did it by not using the ‘action’ submission but instead using a ‘filter’ update field ‘value’ by field_key. Not finished yet but that was a big deal breaker that seems to now be working OK.

    Also I understand much better how this all is working by looking at where values are going and coming from. I have begun making a diagram of same to share with others as believe it is the equivalent of ACF/ACFE Variables for Dummies pictorial explanation. Will share it when done for correction or enhancement as needed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Upates on Next Form Call’ is closed to new replies.