Skip Navigation

[Resolved] Transfer values from one multi-select checkbox to another

This support ticket is created 2 years, 3 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
- 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)

Author
Posts
#2261323

I am creating an onboarding flow for users where
1. Users add information in a temporary post type called temporary user as guests. I am using multiple forms to create an onboarding process.
2. Once this post is ready, user submits a user registration form when I am passing the values from the CPT to the user.

Some of the fields in the CPT and the user are checkboxes and I am having trouble moving to the user post.

I have gone through this function to insert checkboxes - https://toolset.com/forums/topic/update-checkboxes-field-with-php/

It is helpful but I still don't know how to extract checkboxes that are already checked in the custom field. This prevents me from passing the $option value to the function and updating the user fields.

I can see that data is stored in the CPT custom field like this - hidden link

How can I do that?

#2262427

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

Can you please share screenshots of how these involved checkboxes type custom fields are set up and the function that you're using for passing the values from the CPT to the user?

I'll be in a better position to guide you with the next steps, accordingly.

regards,
Waqar

#2262441

Here is the field setup - hidden link

This is the code, i have been using and it does not work.

 
<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_action('cred_save_data', 'populate_temp_data_to_new_subscriber',10,2);
function populate_temp_data_to_new_subscriber($post_id, $form_data)
{
    if ($form_data['id']==[form-id]) // user registration form
    { 
      $temp_post_id = $_GET['post-id'];
      //get all relevant data from temporary user custom post 
      $temp_user_role = get_post_meta($temp_post_id,'wpcf-target-role-temp', true);
      $temp_user_seniority = get_post_meta($temp_post_id,'wpcf-target-seniority-level-temp', true);
      $temp_user_industries = get_post_meta($temp_post_id,'wpcf-target-industry-temp', false);
      $temp_user_target_industries_other = get_post_meta($temp_post_id,'wpcf-target-industry-other-temp', true);
      $temp_user_job_search_status = get_post_meta($temp_post_id,'wpcf-job-search-status-temp', true);
      if( $temp_user_job_search_status != 3) {
      $temp_user_target_employment_types = get_post_meta($temp_post_id,'wpcf-target-employment-type-temp', false);
      $temp_user_target_company_sizes = get_post_meta($temp_post_id,'wpcf-target-company-size-temp', false);
      $temp_user_country_of_employment = get_post_meta($temp_post_id,'wpcf-countries', true);
      $temp_user_job_locations = get_post_meta($temp_post_id,'wpcf-target-job-location-temp', false);
      } //check if user wants a job
      
      //update user meta with the information from above
 update_user_meta($post_id, 'wpcf-target-role', $temp_user_role);
 update_user_meta($post_id, 'wpcf-target-seniority-level', $temp_user_seniority);
 update_user_meta($post_id, 'wpcf-job-search-status', $temp_user_job_search_status);
 update_user_meta($post_id, 'wpcf-target-country-of-employment', $temp_user_country_of_employment);
  //loop is required for all multi data fields
      
      //insert industry data
   foreach( $temp_user_industries as $key => $value) {
        if ($value == 'Other') {
    update_user_meta($post_id, 'wpcf-target-industry-other', $temp_user_target_industries_other);
        }
        else {
           $meta = array();
          $meta[$key] = $value;
//          add_user_meta($post_id, 'wpcf-target-industry', $meta);
        }
      }
      //update the indsutry checkboxes only if there is data beyond 'Other'. Thisis done to avoid running add_user_meta multiple times in the For loop.
      if( $meta != null) {
        update_user_meta($post_id, 'wpcf-target-industry', $meta);
      }
 //add rest of multiple fields for recruiting goals only if user entered data     
  if( $temp_user_job_search_status != 3) {
    //insert employment type 
    if ( $temp_user_target_employment_types != null) {
      foreach( $temp_user_target_employment_types as $temp_user_target_employment_type) {
   add_user_meta($post_id, 'wpcf-target-employment-types', $temp_user_target_employment_type);
      }
      }
    //insert company sizes
    if ( $temp_user_target_company_sizes != null) {
      foreach( $temp_user_target_company_sizes as $temp_user_target_company_size) {
   add_user_meta($post_id, 'wpcf-target-company-size', $temp_user_target_company_size);
      }
      } 
     //insert job locations  
    if ( $temp_user_job_locations != null) {
      foreach( $temp_user_job_locations as $temp_user_job_location) {
   add_user_meta($post_id, 'wpcf-target-job-location', $temp_user_job_location);
      }
      }   
  }// end of if for job search != 3
      
  }  
          
    }
}
?>

I am unable to use the function mentioned earlier because I don't know how to get checked boxes from the temporary user post.

#2263401

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

I'll be performing some tests on my website with a similar setup and will share my findings, as soon as this testing completes.

Thank you for your patience.

#2264403

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting.

During testing on my website, unfortunately, I couldn't make that custom function work to programmatically set/copy the checkboxes type field values.

Can you please share temporary admin login details along with the link to the page where this form can be seen? This will allow me to review the setup and share some different workaround, accordingly.

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2269075

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details and no changes will be made on this production website.

Just wanted to let you know that I'm still performing some tests on my website and will share my findings by the end of the day today.

Thank you for your patience.

#2271205

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting, as this troubleshooting took longer than I initially expected.

On my test website, I was able to make this work, using the following steps:

1. For my temp post type, I add a checkboxes type field with slug "post-checkboxes" and for the users, added checkboxes type custom field with slug "user-checkboxes".

It is important to set the post custom field in a way that option titles are shown when the field is selected, using the "Show one of these two values" setting.
( screenshot: hidden link )

And these titles should also match the option titles used in the user checkboxes type custom field, in which you'd like to copy these values.

2. The "ts_checkboxes" function from the other thread only works for the post custom fields and not for the user custom fields.

For this specific requirement, I added another custom function:


function custom_checkboxes_copy_post_to_user( $user_id, $field, $options ){
	if ( isset($user_id) && isset($field) && isset($options) ){
		$field_settings = types_get_field( $field, 'usermeta' );
		$field_options = $field_settings['data']['options'];

		$meta = array();

		for ($i=0; $i < sizeof($options); $i++) { 
			$key = array_search( $options[$i], array_column( $field_options, 'title' ) );
			$keys = array_keys( $field_options );
			$option_key = $keys[$key];

			$meta[$option_key] = array($field_options[$option_key]['set_value']);
		}

		if(!empty($meta)) {
			update_user_meta( $user_id, 'wpcf-'.$field, $meta ); 
		}
	}
}

This function accepts 3 parameters:

$user_id: ID of the user to which field value should be copied.
$field: Slug of the user checkboxes type custom field (without 'wpcf-' prefix).
$options: An array of option titles, which should be checked/selected.

3. In the last step, I used this new function in the function attached to the "cred_save_data" hook for user form, like this:


add_action('cred_save_data', 'populate_temp_data_to_new_subscriber',10,2);
function populate_temp_data_to_new_subscriber($post_id, $form_data)
{
	if ($form_data['id']==5) // user registration form
	{ 
		$temp_post_id = $_GET['post-id'];

		$post_field_options = types_render_field( "post-checkboxes", array( "separator" => ",", "item" => $temp_post_id ) );

		$options = explode(',', $post_field_options);

		custom_checkboxes_copy_post_to_user( $post_id, 'user-checkboxes', $options );
	}
}

This function gets the target temp post's ID from the ULR parameter "post-id", gets the titles of all the checked checkboxes options from this post, and then passes them on to the "custom_checkboxes_copy_post_to_user" function.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2272525

Waqar,

This is great. I am going to test this weekend and get back to you.

Thank you.

#2273715

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Glad I could help and please take your time.

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