Home › Forums › Backend Issues (wp-admin) › Update Select data field on change Relationship field
Good morning
I’m looking for a little help here for a particular request…for which I can’t find a solution.
I’m looking to update a Select field when I change the value of a Relationship field.
My Relationship Field includes products from my WooCommerce and I only authorize a selection. (I didn’t do a loop)
My script for the select field works. In fact I would like to be able to launch the PHP update of this field from my JS
.on('DOMSubtreeModified', function()
But I do not know how…
Here is my complete script (JS and PHP) in my function.php file
function my_acf_input_admin_footer() {
?>
acf.add_action('ready', function( $el ){ // $el will be equivalent to $('body') // var $field = jQuery('.acf-field-65d324bb8637b');
jQuery('.acf-field-65d324bb8637b ul.values-list').on('DOMSubtreeModified', function(){ console.log('changed'); });
});
<?php } add_action('acf/input/admin_footer', 'my_acf_input_admin_footer'); // Fonction champ couleur function acf_load_product_variations( $field ) { // Reset du champ Select $field['choices'] = array(); // Récupération des infos "PRODUIT" $produit = get_field( 'produit'); // Si champ "PRODUIT" - UPDATE du
if($produit):
$product_id = $produit["0"]->ID;
$product = wc_get_product( $product_id );
$variation_ids = $product->get_children();
if ( $variation_ids ) :
foreach ( $variation_ids as &$variation ) :
$product = new WC_Product_Variation($variation);
$attributes = $product->get_attributes();
$data_array = array();
if( $attributes ){
foreach ( $attributes as $meta_key => $meta ) {
$display_key = wc_attribute_label( $meta_key, $product );
$display_value = $product->get_attribute($meta_key);
#$data_array[] = $display_key .' : '. $display_value;
$label = $display_value;
}
}
#echo implode( '
', $data_array ) . '
';
// Les variables
$value = $variation;
// $label = $produitName;
// Ajout au Select
$field['choices'][$value] = $label;
endforeach;
endif;
endif;
// Return the field
return $field;
}
add_filter('acf/load_field/name=color_product', 'acf_load_product_variations');
If anyone has an idea or has already encountered this problem, I’m interested!
Thanks for your help 🙂
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.