Skip Navigation

[Resolved] Simplify code for saving entry from cred into user field

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 5.32 hours from now. Thank you for your understanding.

This topic contains 6 replies, has 3 voices.

Last updated by filipV-3 3 years, 5 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#582676

Hi Luo,

i opened this for a question regarding your last code/issue proposal:

Q2) this is no longer usable since we're no longer working with just 1 "t2" ID, but with an ID per regio (as seen in cred forms)
1) It needs new custom fields setup with Types plugin,
step 1) create a custom single line user field "billing-addresses"
You will need to create other custom user fields for new generic fields
2) Then modify the PHP codes to add new fields, for example:
$t3 = $_POST['t3'];
$user_id = $post_id;
update_user_meta($user_id, 'wpcf-t3', $t3);
Again, it is only a exapmle, you will need to customize it, please create new thread for the new questions, that will help other users to find the answers,

--> so if i understand correctly, in 'admin- users' fields there will be a whole list of fields, as many as there are regions, and just one will be filled with an entry, according to the region the user saved?
Is there a way to clean up the visual mess in 'admin - users' in that case, so that there's only 1 field for this matter, and not 20 empty rows + 1 filled?

Thank you

#582881

Hi Filip,

I'm Mohammed, the Toolset support team leader.

I see that Luo was handling an issue of your website previously.
Luo is on vacation today and will get back to work normally the next Monday.

I will add this ticket to his queue so that he can work on it once he gets back again.

Thanks.

#583162

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Dear Filip,

I just got this thread.
Could you elaborate the questions?
which on is the 'admin- users' fields you mentioned above?
What is the "regions" you mentioned above? how do you setup it?
How do you want to clean up the visual mess in 'admin - users' ?

I need to test it if it is possible within Toolset plugins. thanks

#583325

Hi Luo,
you can test it within toolset plugins, it's the same site as what we were working on together whole of last week.

I will tackle the most important of questions here, since i get an error and site isn't reachable when i added your codes in functions.php more than once.
Earlier we had (good working):
$t2 = $_POST['t2'];
$user_id = $post_id;
update_user_meta($user_id, 'wpcf-t2', $t2);
(ATTENTION: 'wpcf-t2' was NOT what you wrote earlier, you adviced: update_user_meta($user_id, 'wpcf-billing-addresses', $t2); <-- this works)

Now i added:
$t3 = $_POST['t3'];
$user_id = $post_id;
update_user_meta($user_id, 'wpcf-billing-addressesXXX', $t3);

and it gives an error.

We could see and edit the field in /account-details/ but can edit that field only when it belongs to 't2'. That's why i started now adding the other 't3', 't4', ... Note that i changed names of 't2' etc to relevant new names for easier identification.

What i did step by step to add the regions:
1) make new country in user fields and in products taxonomy
2) make new view for this region (duplicate and edit)
3) make new entry in user field group 'billing addresses'
4) edit cred user forms with new entries
5) add new entries to functions.php

I set functions.php back how it was so the site's functional. This is what i had there, not working:
add_action('cred_save_data', 'update_user_field_func',10,2);
function update_user_field_func($post_id, $form_data)
{
// if a specific form
if (in_array($form_data['id'], array(800, 838))){
$BABelux = $_POST['BABelux'];
$user_id = $post_id;
update_user_meta($user_id, 'wpcf-billing-addresses-belux', $BABelux);
}
}

add_action('cred_save_data', 'update_user_field_func',10,2);
function update_user_field_func($post_id, $form_data)
{
// if a specific form
if (in_array($form_data['id'], array(800, 838))){
$BANL = $_POST['BANL'];
$user_id = $post_id;
update_user_meta($user_id, 'wpcf-billing-addresses-nl', $BANL);
}
}

#583757

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, I am checking it in your website, will feedback if there is anything found

#583763

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

There are two PHP function with same name "update_user_field_func", it will trigger PHP error as you mentioned above, in your case I suggest you merge them into one PHP function, for example, modify the PHP codes like this:

add_action('cred_save_data', 'update_user_field_func',10,2);
function update_user_field_func($post_id, $form_data)
{
	// if a specific form
	if (in_array($form_data['id'], array(800, 838))){
		$user_id = $post_id;
		// --- BABelux ---
		$BABelux = $_POST['BABelux'];
		update_user_meta($user_id, 'wpcf-billing-addresses-belux', $BABelux);
		// --- BANL---
		$BANL = $_POST['BANL'];
		update_user_meta($user_id, 'wpcf-billing-addresses-nl', $BANL);
		// here update more fields ...
	}
}
#584432

Thanks a lot Luo for your help, it works flawlessly now 🙂
Kind regards