I’m currently working on adding some ACF custom fields to Woocommerce products variations.
I use ACF with the product (adding a sidebar video, a GPS location, etc.)
I need (short of a proper “travel management” plugin for WooCommerce) to use ACF with the product_variation(s)
So I read http://www.advancedcustomfields.com/resources/acf_form/ and manage to get the form on each variation.
On the same page,
there is a ‘product’ post_id + an ACF form with field group for ‘product’
and several ‘product_variation’ post_id + an ACF form for ‘product_variation’ > there is multiple forms
The form seems to work ok, I can input values.
But there is two problems :
1 / All html input for the same field (like “departure_date”) have the same id (like [field_548c7a7d6f6d8] ), which mean all the values are not send to $_POST (only the last one for each field id)
2 / When saving, all the fields are saved on the main product post_id
Have you any advice on how to solve this ?
Right now, I’m looking at :
– ajax generate and save the product variation ACF form
– coding the back-end part by hand, only using ACF for front-end
i have done it before , but i don’t remember exactly how 😀
i think you should set ‘form’ attribute to ‘false’ and then at the end of the page add a custom submit button , like this
<?php acf_form(array( 'post_id' => $post_id, 'form' => false, 'field_groups' => array(397), 'return' => add_query_arg( array( 'post_id' => $post_id, 'updated' => 'true'), get_permalink() ), )); acf_form(array( 'post_id' => $post_id, 'form' => false, 'field_groups' => array(397), 'return' => add_query_arg( array( 'post_id' => $post_id, 'updated' => 'true'), get_permalink() ), )); ?> <input type="submit" value="send">
you should use your own
i hope this will help
Right now, I’m using :
acf_form(array( 'post_id' => $variation_id, 'form' => false, 'label_placement' => 'top', 'instruction_placement' => 'label', ));
‘return’ didn’t change the behaviour.
If you can dig up the solution you came with at the time, I will be very interested 🙂
I know ACF is working, since I can use /wp-admin/post.php?post=<product_variation_id>&action=edit&message=1 , edit the ACF fields, save them, it works ok. Opening the product edit form next, I find the product_variation fields updated. But can’t save them on this page.
Another alternative : creating the product_variations on the page but forcing the user to edit them on another page (a product_variation edit page)
I tried your code with a little bit changes and it’s working,
I think you have missed
'field_groups' => array(YOUR_FIELD_GROUP_ID), in your acf_form() , Try adding the id of your field group to the acf_form()
try this and use your own field group id instead of ’28’ in
'field_groups' => array(28)
field_groups : I suppose it’s the post_id from the acf field group (574)
tried it with no change : all the fields have the same id
and behaviour didn’t change.
<input type="text" id="acf-field-commentaire" class="text" name="fields[field_548c7eddacf04]" value="" placeholder=""> on every variation
Oh , you’re right , i tried it only for one variation , since we’re dealing with variable product then field ids should be different.
My guess is you should do it dynamically some how
I mean you should create dynamic custom fields which is not possible with ACF (normally).
What you’re trying to do requires some programming. my solution to this is :
add an html field to variations box like this :
<input type="text" name="myfield_<?php echo $variation_id; ?>">
in this way all fields combine with
$variation_id so they wont be the same any more
And to save the fields you can use
I hope this will help
I could work on core/api.php : acf_form()
// load fields $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
to modify the fields keys
There is a hook in acf.php : save_post() :
do_action('acf/update_value', $v, $post_id, $f );
I probably won’t be able to prevent the product_variation meta to be linked the product, but I could ensure the meta value to be linked to the product_variation
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!
© 2023 Advanced Custom Fields.