Home › Forums › Backend Issues (wp-admin) › Generating multiple forms on the same page (and saving them)
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 $post_id
and "field_groups"
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)
Full code for the plugin is :
http://pastebin.com/frdtEn1n
Screenshot of the product_variation tab :
http://i.imgur.com/r38fM0e.png (the 4 fields in bold are ACF)
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()
<?php
function woocommerce_variation_options_acf_form($loop, $variation_data, $variation) {
add_action('admin_head','acf_form_head',20);
$variation_id = $variation->ID;
?>
<tr><td colspan="2">
<style>
table.data_table td #poststuff { padding: 0; min-width: 1em;}
table.data_table td .postbox { min-width: 1em;}
table.data_table td div.acf_postbox div.field_type-true_false p.label { display: inline; }
table.data_table td div.acf_postbox div.field_type-true_false ul {display: inline-block;}
#variable_product_options .woocommerce_variation table td input[type='checkbox'] { min-width: 1em!important;}
</style>
<?php
acf_form(array(
'post_id' => $variation_id,
'form' => false,
'label_placement' => 'top',
'instruction_placement' => 'label',
'field_groups' => array(28),
'return' => add_query_arg( array( 'post_id' => $post_id, 'updated' => 'true'), get_permalink() ),
));
// var_dump( $variation_id );
?>
<script type="text/javascript">
(function($) {
// setup fields
acf.do_action('append', $('#popup-id'));
})(jQuery);
</script>
</td></tr>
<?php
}
add_action('admin_head','acf_form_head',20);
add_action('woocommerce_product_after_variable_attributes','woocommerce_variation_options_acf_form',99);
/**
In WooCommerce : includes/admin/meta-boxes/views/html-variation-admin.php
*/
do_action( 'woocommerce_product_after_variable_attributes', $loop, $variation_data, $variation ); ?>
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 save_post
action
http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
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
The topic ‘Generating multiple forms on the same page (and saving them)’ is closed to new replies.
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.