Skip Navigation

[Resolved] Get current product meta with cred_save_data is not working

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.

Our next available supporter will start replying to tickets in about 2.29 hours from now. Thank you for your understanding.

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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nashaat 1 year, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2581831

I am trying to get the current value of my fields 'wpcf-car-status' when saving or editing the form, so i can compare the current value with the new value set in the form. The issue that i am getting always the new value of 'wpcf-car-status' and not the current one

$current_status = get_post_meta( $post_id, 'wpcf-car-status', true);

This is my function

add_action('cred_save_data', 'idy_while_saving_the_form',10,2); // While saving data
function idy_while_saving_the_form($post_id, $form_data) {

    $forms = array( 8604, 8598, 8599, 8603, 8613, 8614, 8605, 8612, 8615, 8611, 8616, 35227, 35231 );
    if ( in_array( $form_data['id'], $forms ) ) {

        $product = wc_get_product( $post_id );
        $current_status = get_post_meta( $post_id, 'wpcf-car-status', true);
        $new_status = (isset($_POST['wpcf-car-status']) ? $_POST['wpcf-car-status'] : false );
        
        if ($current_status !== $new_status) { 
            $product->update_meta_data('idy_sold_date', current_time('timestamp'));
        }
        
    }

}

The output should be

$current_status = 'my_old_value_from_the_product';
$new_status = 'my_new_value_from_the_form';

Instead i am getting this which is wrong

$current_status = 'my_new_value_from_the_form';
$new_status = 'my_new_value_from_the_form';
#2582135

Hi,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and by the time the 'cred_save_data' hook executes, the custom field values have been updated, based on the form fields.

To overcome this, you can use the 'cred_before_save_data' hook instead:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

Example:


add_action('cred_before_save_data', 'idy_while_saving_the_form',10,1); // While saving data
function idy_while_saving_the_form($form_data) {
 
    $forms = array( 8604, 8598, 8599, 8603, 8613, 8614, 8605, 8612, 8615, 8611, 8616, 35227, 35231 );
    if ( in_array( $form_data['id'], $forms ) ) {

    	$post_id = $_POST['_cred_cred_prefix_post_id'];
 
        $product = wc_get_product( $post_id );
        $current_status = get_post_meta( $post_id, 'wpcf-car-status', true);
        $new_status = (isset($_POST['wpcf-car-status']) ? $_POST['wpcf-car-status'] : false );

        .....
         
    }
 
}

If you'd like to continue using the 'cred_save_data' hook, you can include a hidden generic field with the slug 'existing-status-value' in the form.

Next, using some custom code, you can copy the value from the 'car-status' field into this hidden generic field:


jQuery(document).ready(function( $ ) {
	
  	var existingStatus = $('input[name=wpcf-car-status]').val();
	$('input[name=existing-status-value]').val(existingStatus);
	
});

As a result, when the form will be submitted, you'll be able to obtain the existing field value, using this generic field:


$current_status = $_POST['existing-status-value'];

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2582207

Hi Thanks for your reply.

I have managed this as well with the cred_before_save_data like following:

add_action('cred_before_save_data', 'idy_cred_before_save_data',10,2); // While saving data
function idy_cred_before_save_data($form_data) {

    //error_log('DATA: ' . print_r($form_data,true));
  
    $forms = array( 8604, 8598, 8599, 8603, 8613, 8614, 8605, 8612, 8615, 8611, 8616, 35227, 35231 );
    if ( in_array( $form_data['id'], $forms ) ) {

        $date = (isset($_POST['wpcf-general-date-field	']) ? $_POST['wpcf-general-date-field	'] : current_time('timestamp') );
        $post_id = $form_data['container_id'];
        $current_status = get_post_meta( $post_id, 'wpcf-car-status', true);
        $new_status = (isset($_POST['wpcf-car-status']) ? $_POST['wpcf-car-status'] : false );
	  	  
        if ($current_status !== 'נרכש' && $new_status == 'נרכש') {  
            update_post_meta($post_id, 'wpcf-idy-sold-date', $date);
        } else if ($current_status == 'נרכש' && $new_status !== 'נרכש') {  
            update_post_meta($post_id, 'wpcf-idy-sold-date', NULL);
            update_post_meta($post_id, 'my_autor_select', NULL);
        }

        //$updated_status = get_post_meta( $post_id, 'wpcf-car-status', true);

    }
    
}

The only difference that i am getting the product id from the container_id

 $post_id = $form_data['container_id'];

Can you confirm that getting the post_id from the container_id is ok or i need to change it to

$post_id = $_POST['_cred_cred_prefix_post_id'];
#2582223

Thanks for writing back and glad that it is working now.

The '$form_data['container_id']' will return the ID of the post/page that is holding or showing the form. So, as long as you're showing the edit form on the same post/page that is being edited by the form, you'll be fine.

And for a case, where the edit form is on a different post/page that it is being used for editing, you can use the '$_POST['_cred_cred_prefix_post_id']'.

#2582225

My issue is resolved now. Thank you!

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