Dear Sir/Madam,
I have a custom post with repeatable field group which contain the timestamp, status ( booked, used and noshow ), in the form, the parent post id @booking-group_parent will be changed before the data save, below is my code to save the repeatable field group 'booking-group', I don't know why the to be saved repeatable post not to be calculated. The quota balance is calculated by $purchased_total - $used_total - $noshow_total , this bug can be found when I submit the form, I log the value of wpcf-coupon-quota-balance before and after the balance calculated, it looks the save_data_for_form_with_id_7393 doesn't save the data before this function called.
Below please find the code I made, I change the parent post id by using before_save_data_for_form_with_id_7393 and then update the balance in save_data_for_form_with_id_7393
function update_coupon($post_id) {
$args = array(
'posts_per_page' => -1,
'meta_key' => 'wpcf-book-status',
'meta_value' => 'used',
'meta_compare' => '='
);
$rfg_ids = toolset_get_related_posts( $post_id, 'booking-group', 'parent', 100, 0, $args, 'post_id', 'child');
$used_total = count($rfg_ids);
$args = array(
'posts_per_page' => -1,
'meta_key' => 'wpcf-book-status',
'meta_value' => 'noshow',
'meta_compare' => '='
);
$rfg_ids = toolset_get_related_posts( $post_id, 'booking-group', 'parent', 100, 0, $args, 'post_id', 'child');
$noshow_total = count($rfg_ids);
$purchased_total = get_post_meta($post_id, 'wpcf-coupon-purchased-quota', true);
update_post_meta($post_id, 'wpcf-coupon-quota-balance', $purchased_total - $noshow_total - $used_total);
update_post_meta($post_id, 'wpcf-coupon-last-update', strtotime("+8 hours"));
}
function save_data_for_form_with_id_7393($post_id, $form_data)
{
wp_update_post( array('ID' => $post_id, 'post_title' => date('Y-m-d H:i:s',strtotime("+8 hours"))) );
update_post_meta( $post_id, 'wpcf-book-timestamp', strtotime("+8 hours") );
$coupon_id = $_POST['coupon-member'];
$fp = fopen(WP_CONTENT_DIR . '/form_7393.log', 'a');
fwrite($fp, sprintf("\nbefore update, remind %s\n", get_post_meta($coupon_id, 'wpcf-coupon-quota-balance', true)));
update_coupon($coupon_id);
fwrite($fp, sprintf("after update, remind %s\n", get_post_meta($coupon_id, 'wpcf-coupon-quota-balance', true)));
fclose($fp);
}
add_action('cred_save_data_7393', 'save_data_for_form_with_id_7393',10,2);
function before_save_data_for_form_with_id_7393($form_data) {
$_POST['@booking-group_parent'] = POST['coupon-member'];
}
add_action('cred_before_save_data_7393', 'before_save_data_for_form_with_id_7393',10,1);
Here is the output I log from form_7393.log
before update, remind 10
after update, remind 10