ACF Clone Field – Set Default Value

Based on your pastebin array, this code below should loop through all your values in NJ, check if any of the values are empty, and if values have subfields or if value type is a post object.

For example on NJ [welcome_bonus], is an array with sub fields. This code will loop through each subfield in [NJ][welcome_bonus], check if field is blank, if it is blank, it will then get the original field value and set it as that.

You don’t need Global 2 group, unless you are using this for something else.

See comments in code…

// acf action that runs when you save post
add_action( 'acf/save_post', 'nj_empty_clone_value_check', 20 );

/**
 * @param $post_id int
 * @return void
 */
function nj_empty_clone_value_check($post_id) {

    // global post
    global $post;

    // check we are on the correct post type (casino_review)
    if($post->post_type <> 'casino_review') return;

    // get all the fields values from our current post
    $fields = get_fields($post_id);

    // changes check
    $changes = false;

    // for each of the cloned fields in NJ group
    foreach ($fields['nj'] as $key => $value) {
        
        // if current NJ cloned field is a post object
        if(is_object($value)) {
            
            // temp cast
            $temp = (array)$value;
            
            // check if post object is not empty (blank)
            if(!empty($temp)) {

                // update the NJ cloned field value with the original field value
                $fields['nj'][$key] = $fields[$key];

                // update changes check to true
                $changes = true;

            }
        
        }

        // else if current NJ cloned field is an array (has subfields)
        else if(is_array($value)) {

            // for each of the cloned array subfields in NJ group
            foreach ($value as $sub_key => $sub_value) {

                // if current NJ cloned subfield value is not blank
                if (!$sub_value) {

                    // check the original subfield value is not blank
                    if (!empty($fields[$key][$sub_key])) {

                        // update the NJ group subfield value with the original field value
                        $fields['nj'][$key][$sub_key] = $fields[$key][$sub_key];

                        // update changes check to true
                        $changes = true;

                    }

                }

            }

        } 

        // if current NJ cloned field value is not blank
        else if(!$value) {

            // check the original field value is not blank
            if(!empty($fields[$key])) {

                // update the NJ field value with the original field value
                $fields['nj'][$key] = $fields[$key];

                // update changes check to true
                $changes = true;

            }

        }

    }

    // if any changes have been made to NJ group
    if($changes) {

        // update NJ group field with updated NJ array data
        update_field('nj', $fields['nj'], $post_id);

    }

}