Support

Account

Home Forums Add-ons Repeater Field Dynamically populated checkbox field not saved on the post.

Helping

Dynamically populated checkbox field not saved on the post.

  • I have this function called “obtener_extras_taller” which is triggered by ajax whenever I select a option in another field, and populate the checkbox field dynamically with information bringed from an API. It works as intended. The problem is that when I update the post is no saving any of the selected choices from the checkbox field.

    function fn_obtener_extras_taller() {
    
    	$arrayName = array('body' => array('tipos' => $_POST['id_producto'], 'troquel' => 1, 'impresion' => 0) );
    	$url = 'https://calculadora.imprimebanana.com/api/taller/extras';
    	$extras = wp_remote_post( $url , $arrayName  );
    	$body = wp_remote_retrieve_body($extras);
    	$dataExtras = json_decode($body, true);
    
    	$retornar = '';
    
        foreach ($dataExtras as $key => $value) {
    		if( $value == 'error'|| $value == 'No existen extras para está configuración'){
    			$retornar = '<p>No hay opciones disponibles</p>';
    			break;
    		}
    		$retornar .= '<li><label><input type="checkbox" value="'.$value.'" >'.$value.'</input></label></li>';
    	}    
    
        echo $retornar;
    
        wp_die();
    }
    add_action('wp_ajax_obtener_extras_taller', 'fn_obtener_extras_taller');

    Here the ajax:

    $(document).on('change', '[data-key="field_5f8a0db7c4bc2"] .acf-input select', function(e) {
    
        var valor = this.value;
    
        var selectExtras = jQuery(this).parents('.acf-row').find('[data-key="field_5f8fa8bdd4c85"] .acf-input ul');
        selectExtras.html('<p>Cargando...</p>');
    
        jQuery.ajax({
            type: "post",
            url: ajaxurl,
            data: "action=obtener_extras_taller&id_producto=" + valor,
            success: function(result){
               selectExtras.empty();
               selectExtras.html(result);
            }
        });     
      });

    Also this is a sub_field in a repeater field. I’ve done this for other select fields, but the checkbox field is giving me troubles.

  • I’m not sure where your having this issue here.

    Is it when you load the post in the admin? When editing in the admin the value of the field must be one of the choice available for the field. This means that you need to have an acf/prepare_field filter that populates the choices of the checkbox with the choices based on the first field and this needs to be done in addition to the population done by AJAX.

    A second issue is that this is a sub field of a repeater. You cannot easily have different choices for each row of the repeater. What this means is that the acf/prepare_field filter needs to load every choice that is possible for choices of this field and then you must create and acf ready action for this field to update the choices of the field based on the choice in the first field.

    Breaking it down… When the edit page is loaded the saved value must match an existing choice available for the field.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.