Skip Navigation

[Resolved] cred_before_save_data not firing

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm using the 'cred_before_save_data' hook to update several values for related posts prior to the update taking place. The hook does not appear to be firing as those values are not updating when the form is submitted.

Solution:

You can debug custom PHP codes manually, like this:
https://toolset.com/forums/topic/cred_before_save_data-not-firing/#post-1136646

Relevant Documentation:

This support ticket is created 6 years, 1 month 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by MattI4840 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1136265

I am trying to:

I've created a post form to edit a cpt, and I'm using the 'cred_before_save_data' hook to update several values for related posts prior to the update taking place. The hook does not appear to be firing as those values are not updating when the form is submitted.

Link to a page where the issue can be seen:

hidden link

Click on any of the listed containers and update them, then check the below inventory screen for the output.

hidden link

I expected to see:

The purpose of the edit screen is to allow a user to edit a container's totes or bags values. Those values are used to calculate the tonage, and value of the container and aggregate them per warehouse and product type as seen in the inventory page. To that end, the ylstracking_edit_container function should remove the original values for totes, bags, tons, and value from the warehouse and product aggregates.

Instead, I got:

The function does not appear to fire at all, instead of subtracting the totes, bags, tons, and value of the pre-updated container, the new values are added on top of the aggregates that were already there.

I understand that i may not be explaining this clearly, so I'll try to outline an example. There is currently a container listed on the edit-confirmed-container page named 'tclu4876263'. This container currently has 15 totes and 15 bags, and if I edit the container to have 20 totes and 20 bags the net result should be an inventory increase of 5 totes and 5 bags. However, since the ylstracking_edit_container function is not working, the actual result is an addition of 20 totes and 20 bags, which is 15 more of each than it should be.

Here is the function I wrote that is not firing. Note I set the priority to 9 so that the aggregation function which I mentioned above fires after this one.

function ylstracking_edit_container($form_data) {

	if ($form_data['id']==385) {

		if ($_POST['wpcf-product-type']==1) {

			$post_id = $form_data['container_id'];

			$warehouse = toolset_get_related_post( $post_id, 'warehouse-transaction' );

			//this section grabs the original container values for totes, subtracts it from the warehouse and product posts
			$container_totes = get_post_meta( $post_id, 'wpcf-container-totes', true);
			$warehouse_totes = get_post_meta($warehouse, 'wpcf-warehouse-dt80-totes', true);

			$new_warehouse_totes = $warehouse_totes - $container_totes;
			update_post_meta( $warehouse, 'wpcf-warehouse-dt80-totes', $new_warehouse_totes );

			$inventory_totes = get_post_meta( 75, 'wpcf-inventory-totes', true);
			$new_inventory_totes = $inventory_totes - $container_totes;
			update_post_meta( 75, 'wpcf-inventory-totes', $new_inventory_totes );
			
			//bags
			$container_bags = get_post_meta( $post_id, 'wpcf-container-bags', true);
			$warehouse_bags = get_post_meta($warehouse, 'wpcf-warehouse-dt80-bags', true);

			$new_warehouse_bags = $warehouse_bags - $container_bags;			
			update_post_meta( $warehouse, 'wpcf-warehouse-dt80-bags', $new_warehouse_bags );

			$inventory_bags = get_post_meta( 75, 'wpcf-inventory-bags', true);
			$new_inventory_bags = $inventory_bags - $container_bags;
			update_post_meta( 75, 'wpcf-inventory-bags', $new_inventory_bags );

			//tons
			$container_tons = get_post_meta( $post_id, 'wpcf-container-tons', true);
			$warehouse_tons = get_post_meta($warehouse, 'wpcf-warehouse-tons', true);

			$new_warehouse_tons = $warehouse_tons - $container_tons;
			update_post_meta( $warehouse, 'wpcf-warehouse-tons', round($new_warehouse_tons, 3) );

			$inventory_tons = get_post_meta( 75, 'wpcf-inventory-tons', true);
			$new_inventory_tons = $inventory_tons - $container_tons;
			update_post_meta( 75, 'wpcf-inventory-tons', round($new_inventory_tons, 3) );

			//values
			$container_value = get_post_meta( $post_id, 'wpcf-container-value', true);
			$warehouse_value = get_post_meta($warehouse, 'wpcf-warehouse-value', true);

			$new_warehouse_value = $warehouse_value - $container_value;
			update_post_meta( $warehouse, 'wpcf-warehouse-value', round($new_warehouse_value, 3) );


		}

	}


}
add_action('cred_before_save_data', 'ylstracking_edit_container', 9, 1);

Please note you have permission to review and make changes to whatever you need to test the issue.

#1136646

Hello,

First, the action hook "cred_before_save_data" is firing in your website, you can check these:
1) Edit the Toolset form "Edit Container", disable option "AJAX submission", and save
2) Add below codes into your theme file functions.php:

function my_function($form_data) {
 
    if ($form_data['id']==385) {
 
        echo 'cred_before_save_data action hook is triggered!';
		die();
    }
 
 
}
add_action('cred_before_save_data', 'my_function', 9, 1);

Test it in front-end:
hidden link

Submit the form, You should be able see the message: "cred_before_save_data action hook is triggered"

I suggest you debug your custom codes manually, and if you are going to use user's new input, I suggest you try another action hook "cred_save_data", this action will be triggered after the data is saved in database, then you can use get_post_meta() function to get the new inputs.

#1136672

thank you for your reply, I've tested what you sent over and it did work, the hook is firing as you described. I used the code you sent over to test part of my code, and I'm getting an unexpected result.

function my_function($form_data) {
  
    if ($form_data['id']==385) {
  		
  		$post_id = $form_data['container_id'];
  		$warehouse = toolset_get_related_post( $post_id, 'warehouse-transaction' );

        echo $warehouse;
        die();
    }
  
  
}
add_action('cred_before_save_data', 'my_function', 9, 1);

This echos out a 0, but I was expecting the slug for the related post. Is my syntax incorrect? Or is there another issue you can see here? Thanks!

#1137029

I believe I have the issue resolved, I was using the incorrect relationship slug. Thank you for your help Lou, and I appreciate the guidance on troubleshooting this!