Home › Forums › Front-end Issues › acf_form() – action when repeater row added or updated
Hey all,
So, I have a repeater field which my site users will be able to use on the frontend using acf_form()
to add rows to the repeater. This form appears on a WooCommerce order details page
Form works fine, rows get added etc and saved to the order. Cool.
Now, one of the fields in the repeater is an email address. Here’s what I’m hoping I can get to happen:
acf_form()
This action needs to be triggered EVERY TIME the form is changed and submitted on the frontend
Hopefully this is possible
Here’s kind of what I have from another support post I stumbled on from Google:
function delegate_form_update($value, $post_id, $field) {
// only do it to certain custom fields
if($field['name'] == 'ticket_alloc') { // ticket_alloc is the name of the repeater
// get the old (saved) value
$old_value = get_field('ticket_alloc', $post_id);
// get the new (posted) value
$new_value = $_POST['acf']['field_63ca95e88557a']; // this is the key of the repeater
// check if the old value is the same as the new value
if($old_value != $new_value) {
// Do something if they are different
} else {
// Do something if they are the same
}
}
// don't forget to return to be saved in the database
return $value;
}
add_filter('acf/update_value', 'delegate_form_update', 10, 3);
Can anyone help, or point me in the right direction? @hube2 @acf-support ?
Thanks
Mark
If you only want to know if a row is added
// use get post meta on the repeater field
// this will hold the number of rows - old value
$current_rows = intval(get_post_meta($post_id, 'repeater_field_name', true);
// count the rows submitted
$new_rows = count($_POST['acf']['field_XXXXXXXXX']); // field key of repeater
if ($old_rows != $new_rows) {
// number of rows has changed
}
@hube2 Ok that makes sense, thanks for that
Now, how do I check if any of the get_sub_fields()
have been changed/updated?
Eg. 4 rows have been previously saved. User edits one/many of those fields and saves again
Or was my initial code correct for checking that?
You are going to have to compare each row in the old value stored in the repeater against the new input row.
Unfortunately I do not know the indexes used for new rows on the input, when acf repeaters are submitted the input for a sub field is
$_POST['acf']['field_XXXXX'][$index]['field_YYYYY']
I am unsure of the $index_value and if they are always integers. I think, but I’m not sure, that new rows have a sting index.
something like this should work in any case
$something_changed = false;
$old_values = array();
if (have_rows('repeater', $post_id) {
while (have_rows('repeater', $post_id) {
the_row();
$old_values[] = get_sub_field('sub_field');
}
}
$new_values = array();
if (!empty($_POST['acf']['field_XXXXX'])) {
// loop over input rows
foreach ($_POST['acf']['field_XXXXX'] as $row) {
// get sub field value from row
$new_values[] = $row['field_YYYYY'];
}
}
if (count($old_values) != count($new_values) {
$something_changed = true;
} else {
for ($i=0; $i<count($old_value)l $i++) {
if ($new_values[$i] != $old_values[$i]) {
$something_changed = true;
}
}
}
if ($something_changed) {
// do something
}
@hube2 Ahhhhhhh ok, so compare each row individually rather than compare the parent repeater
I’ll give it bash and come back to you (if that’s ok) with a yay or nay
Appreciate the help
@hube2 Hey, I think I’m missing something, because this isn’t sending the email:
function delegate_form_update($value, $post_id, $field) {
$order = new WC_Order($post_id);
// global email settings
$to = get_option('admin_email');
$headers = array('Content-Type: text/html; charset=UTF-8');
// this will hold the number of rows - old value
$old_rows = intval(get_post_meta($order, 'ticket_alloc', true));
// count the rows submitted
$new_rows = count($_POST['acf']['field_63ca95e88557a']); // field key of repeater
if($old_rows != $new_rows) {
// number of rows has changed
$subject = 'Number of rows has changed';
$body = 'This is the mail body';
wp_mail($to, $subject, $body, $headers);
}
return $value;
}
add_filter('acf/update_field/key=field_63ca95e88557a', 'delegate_form_update', 10, 1);
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.