Skip Navigation

[Resolved] post_meta not updated when the first repeatable post inserted

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Minesh 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2576473
Screenshot 2023-03-20 at 8.04.04 AM.png

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
#2576505

I fixed the issue by putting the calculation process in function success_for_form_with_id_7393()

I have no idea why I cannot get the total saved booking record within function save_data_for_form_with_id_7393()

Is the repeatable post be completely saved when function save_data_for_form_with_id_7393() called?

#2576585

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes - as you can see with the related doc for the hook "cred_save_data":
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

The hook will be called:

Description
This hook allows doing a custom action when post data is saved to database.

So yes hook should be fired but maybe you will have to use hire priority or you can also use the cred_submit_complete action as well:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete