regarding advanced custom fields repeater and select drop downs. i currently have a website which revolves around cars.
In an options page in wordpress, i have a repeater block with one text field named 'manufacturer' and another called 'models' as a text area field.
the text area is populated on each line with a model, so on one row for example:
manufacturer = audi models = A1 A2 A3 A4 etc..
on the custom post type 'lease' for the cars i have made 2 select drop downs for manufacturer and models.
I can auto-populate the manufacturer as per the example #2 code here:
http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
that works fine no problem!
then with the models drop down select, ONLY when the manufacturer select drop down changes to then auto populate it with the models from the repeater on the options page showing only the models based on the manufacturer...
Im aware this requires ajax to do so..
I have a sample code that i was testing the other week, i could get it to do what i wanted but when you go into the single car it would be blank, then when you select the options and then save/update the refreshed screen the models select drop down would be blank again, although with testing it shows that it has sent the data to the database and has saved.
so how do i fix the issue when i save the car post that it doesn't go blank?
here is the code i was testing
PHP
function acf_admin_enqueue( $hook ) {
$type = get_post_type(); // Check current post type
$types = array( 'lease' ); // Allowed post types
if( !in_array( $type, $types ) )
return; // Only applies to post types in array
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/library/dist/js/acf_select.js' );
wp_localize_script( 'populate-area', 'pa_vars', array(
'pa_nonce' => wp_create_nonce( 'pa_nonce' ), // Create nonce which we later will use to verify AJAX request
)
);
}
add_action( 'admin_enqueue_scripts', 'acf_admin_enqueue' );
// Return models by manufacturer
function model_by_manufacturer( $selected_manufacturer ) {
// Verify nonce
if( !isset( $_POST['pa_nonce'] ) || !wp_verify_nonce( $_POST['pa_nonce'], 'pa_nonce' ) )
die('Permission denied');
// Get manufacturer var
$selected_manufacturer = $_POST['manufacturer'];
// Get field from options page
$manufacturer_and_models = get_field('car_m_and_m', 'options');
// Simplify array to look like: manufacturer => models
foreach ($manufacturer_and_models as $key => $value) {
$manufacturer[$value['manufacturer']] = $value['models'];
}
// Returns model by manufacturer selected if selected manufacturer exists in array
if (array_key_exists( $selected_manufacturer, $manufacturer)) {
// Convert model to array
$arr_data = explode( ', ', $manufacturer[$selected_manufacturer] );
return wp_send_json($arr_data);
} else {
$arr_data = array();
return wp_send_json($arr_data);
}
die();
}
add_action('wp_ajax_pa_add_areas', 'model_by_manufacturer');
add_action('wp_ajax_nopriv_pa_add_areas', 'model_by_manufacturer');
Javascript:
jQuery(document).ready(function($) {
/* Add default 'Select one'
$( '#acf-field_5548d019b55cb' ).prepend( $('<option></option>').val('0').html('Select Manufacturer').attr({ selected: 'selected', disabled: 'disabled'}) );
*/
/**
* Get manufacturer option on select menu change
*
*/
$( '#acf-field_5548d019b55cb' ).change(function () {
var selected_manufacturer = ''; // Selected value
// Get selected value
$( '#acf-field_5548d019b55cb option:selected' ).each(function() {
selected_manufacturer += $( this ).text();
});
$( '#acf-field_5548da7058203' ).attr( 'disabled', 'disabled' );
// If default is not selected get models for selected manufacturer
if( selected_manufacturer !== 'Select Manufacturer' ) {
// Send AJAX request
data = {
action: 'pa_add_areas',
pa_nonce: pa_vars.pa_nonce,
manufacturer: selected_manufacturer,
};
// Get response and populate models select field
$.post( ajaxurl, data, function(response) {
if( response ){
/* Disable 'Select model' field until country is selected
$( '#acf-field_5548da7058203' ).html( $('<option></option>').val('0').html('Select Model').attr({ selected: 'selected', disabled: 'disabled'}) );
*/
// Add models to select field options
$.each(response, function(val, text) {
$( '#acf-field_5548da7058203' ).append( $('<option></option>').val(text).html(text) );
});
// Enable 'Select Model' field
$( '#acf-field_5548da7058203' ).removeAttr( 'disabled' );
}
});
}
}).change();
});
I assume it is something to do with the Javascript?
any advise or help would be appreciated,
thank you in advance.
R