Skip Navigation

[Resolved] Can not update parent field when submitting a child form

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by nabils 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1486477

Hi,
I am trying to:

I have parent post rental with field balance and child post payment with field payment amount. When new payment form is submitted I want to subtract the payment amount from the balance and update balance filed in rental post.

I am using this code but it is not working.

add_action('cred_save_data', 'balance_calculation',10,2);

function balance_calculation($post_id, $form_data)

{
 if ( $form_data['id']==1332 ){
	$payment_for = get_post_meta($post_id, "wpcf-payment_for", true);
	$payment_amount = get_post_meta($post_id, "wpcf-payment_amount", true);
	$rental_id = toolset_get_related_post( $post_id, "rental-payment" );
	$balance = get_post_meta($rental_id, "wpcf-rental_balance", true);
	$balance = $balance - $payment_amount;
	update_post_meta($rental_id, "wpcf-rental_balance", $balance);
}
}
#1486695

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I suggest you add error_log messages to your code that output the variables at each step and it should enable you to determine why it is not working.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

Update your code snippet to print the variables to the log, e.g.

error_log("payment_for: " . $payment_for);

Although I don't think it applies here, in cases where the variable may be an object or array, it's useful to output using print_r for readability, e.g.

error_log('object: ' . print_r($object, true));
#1489449

Thank you.

Now I can see that it is not getting the payment parent so

  $rental_id = toolset_get_related_post( $post_id, "rental-payment" ); 

is not working.
What can be the problem?

#1489793

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

The rental-payment relationship is a one-to-many relationship?

$post_id is the ID of a child post, and you are trying to retrieve the id of the parent rental post?

If this were a many-to-many relationship it cannot work with the toolset_get_related_post function, which only works when there is only one possible result (the singular parent). If there were more than one possible result (because you were aiming to retrieve a child post ID or because this was a many-to-many relationship) you would need to use toolset_get_related_posts instead (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts).

Can you clarify the above?

#1489829

The rental-payment relationship is one to many relationship (Infinite Payments can be assigned to one Rental)

#1489895

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

In which case toolset_get_related_post is the correct function to use.

It only takes two arguments, the ID of the child post and the slug of the relationship.

Assuming these are correct, that leads to a third possibility: there is no parent post.

1. Does the form to publish a child post include a selector for the parent post?
2. Is it set when submitting the form?
3. After submitting the form if you edit the child post in the backend, can you see that the child post is actually connected to a parent post?

#1489987

Yes the child form includes a selector for the parent post and after submitting the form I can see that the payment is connected to the correct rental.

#1490015

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hmm. Perhaps Forms internally uses the same hook to connect the posts, and when your code runs the connection has not yet been made.

You might try raising the priority of the hook, so change the 10 priority to, say, 999...

add_action( 'cred_save_data', 'balance_calculation', 999, 2 );

Or you could switch to the cred_submit_complete hook (same arguments: https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete).

Can you try that..?

#1490737

Many thanks.
When I used

add_action( 'cred_submit_complete', 'balance_calculation', 999, 2 );

it works fine.

#1490739

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.