Skip Navigation

[Resolved] Using CPT entries in CRED registration form – select 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.

This topic contains 24 replies, has 2 voices.

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

Assigned support staff: Luo Yang.

Author
Posts
#580175

hi! I read all existing topics on this subject already, but it's not working for me..

What I'm trying to do:
i made a CPT 'billing addresses' and created some entries. There's one taxonomy filter 'regions'.
I made a CRED registration form and now i'm trying to show those entries in a select field. On top, those entries should be pre-filtered by that region, according to the field 'region' picked by user when registering. This can be done with conditional groups imo. (i already succesfully made conditional group depending on region, for a post field. But for CPT it's different)
So, i created a view for that CPT and added cred generic field in the form, to show those entries. I tried via a view shortcode (no result at all) and by JSON version of that view (than i can see a select field showing up, but with 'no results' only)

Sure I'm doing something or even more things wrong, but i can't figure it out.

The ultimate goal of all this: in the cred registration form, the user chooses the billing address from that select list, and that input will be saved and used by woocommerce as the billing address (users can't add their own billing address and must pick from a select list of available billing addresses filtered by region)

IMPORTANT: please just don't change theme, it'll break the site beyond repair (but that's not of influence on this topic).

Thanks a lot!

#580475

Luo Yang
Supporter

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

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

Dear Filip,

Views plugin is not designed as a data provider for other plugins, the shortcodes [wpv-layout-start] and [wpv-layout-end] will output a extra HTML div tag, so you can not use view's result as option of CRED generic field directly.

But you can try the solution of another thread:
https://toolset.com/forums/topic/upgrade-to-views-1-11-broke-a-cred-cred_generic_field-select-populated-by-a-view/#post-348415

For your reference.

#580545

Dear Luo,
i tried your solution, but the results remains the same.
I'm a bit confused that you wrote that view plugin is not designed as a data provider for other plugins, since i read different other topics were it's successfully done?
Please have a deeper look if you can, thanks for your help!
Kind regards

#580555

Luo Yang
Supporter

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

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

I tried the credentials you provided above:
https://toolset.com/forums/topic/using-cpt-entries-in-cred-registration-form-select-field/#post-580175

It is not valid, and I get this error message:
ERROR: Invalid email address.

Please check it, make sure the credentials you provided is valid, also point out the problem CRED form URL and page URL, I can setup a demo for you.

#580578

hi Luo,
sorry you're right, this site requires email instead of usual login name. Please use info8@ ... (the url) and the provided password.

Links to those pages:
cred: ../wp-admin/post.php?post=800&action=edit
frontend reg form: ../jde-user-registration/
view: ../wp-admin/admin.php?page=views-editor&view_id=871

Thanks!!

#580946

Luo Yang
Supporter

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

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

Thanks for the details, I have done below modification in your website:
1) edit the view
hidden link
in section "Loop Output Editor", modify the codes insider views loop, as below:

            [wpv-item index=1]{"value":"[wpv-post-id]","label":"[types field='billing-field-types-address'][/types] | [types field='billing-field-types-city'][/types]"}
            [wpv-item index=other],{"value":"[wpv-post-id]","label":"[types field='billing-field-types-address'][/types] | [types field='billing-field-types-city'][/types]"}

2) edit the CRED form:
hidden link

Modify the "t2" fields as below:

[cred_generic_field field="t2" type="select" class="" urlparam=""]
{
"required":1,
"validate_format":1,
"persist":1,
"default":["--- Select ---"],
"options":[[wpv-view name="Billing Address View"]]
}
[/cred_generic_field]

I did not remove your original shortcodes for field "t2", only disable it.

Please test it in front-end:
hidden link
Check if it is fixed or not

#581228

Hi Luo,

beautiful work, thanks a lot!
Now i will need to put that view output into the cred form (as you did already) so that it will show results filtered by taxonomy 'region', according to the user's cred input of the previous field (which is 'user region').
I know this is something to be done via conditional groups.

To my knowledge I need to make a different view with query filter set to a specific region, and then make create the conditional group in cred for that output. And do it all over for each other region.

Or is it possible to work with just one view and one cred input? Like: in the view loop output have something like 'if cred region field is set to X, than view results will be according X? (i guess not, but rather ask first since it'll save lot of time and will be mush easier for customer later to add new results)

Kind regards

#581328

in addition to previous reply:
I found that a field isn't saving the input in the cred form (that field that you fixed)
It's doesn't give any error when registering, but next on the cred edit user form in frontend ( url/jde-shop/my-account/my-brands/ ) the field remains 'not set' even when trying to save directly there...

#581350

Luo Yang
Supporter

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

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

Q1) Or is it possible to work with just one view and one cred input?
You are right, you will need to:
make a different view with query filter set to a specific region, and then make create the conditional group in cred for that output. And do it all over for each other region.

found that a field isn't saving the input in the cred form (that field that you fixed)
Yes, it is expected result, you can use CRED action hook cred_save_data to save the generic field into database, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
and similar thread:
https://toolset.com/forums/topic/save-cred-generic-field-value-to-database/#post-342091

#581454

hi Luo,
i had an attempt at that cred_save_data hook, added it to functions.php but isn't working. I left out:
// if a specific form
if ($form_data['id']==12)
{
since i think that's not relevant here (as we have more forms: one for registration and one for editing it)

My programmer left this week for two weeks holidays, so i'm afraid i have to kindly ask you to have a look what i did wrong?
(i followed the doc rather than the other topic's format)

Thanks again and have a nice day!

#581457

and added: will i need to add such action in functions.php for each and every region in that case?? (since the field name would be different for each conditional group afaik)

#581485

Luo Yang
Supporter

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

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

Yes, I can setup a demo for you, but I need more details:
When users submit the CRED form, they will submit the value of field "t2", where and how do you save the custom field value "t2"?
I need to test and debug it in your website, thanks

#581633

hi, the value of t2 was to be reflected in the 'edit user cred form' on page /jde-user-registration/ That's all for frontend. From there, i will take that value to pre-populate the 'billing_country' + billing_city and _postcode fields in woocommerce billing address.
Next remains that question "will i need to add such action in functions.php for each and every region in that case?? (since the field name would be different for each conditional group afaik)" --> as probably it's not just about t2 but also upcoming t3, t4, ... (one for each region, which will be called in the reg form via conditional groups, according to user input in reg form field 'user region'.
Is that enough info?
Thanks a lot Luo!

#581672

added to "where and how do you save the custom field value "t2"? "
--> ideally also in wordpress 'users' info, like 'user fields', but i guess that's not possible as it's not really set up as a user field?

#581794

Luo Yang
Supporter

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

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

I assume you are going to update the current user, who submits CRED form.
You can try below PHP codes:

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 ($form_data['id']==123){
		$t2 = $_POST['t2'];
		$user_id = get_current_user_id();
		update_user_meta($user_id, 'user-field-slug', $t2);
	}
}

Please replace 123 with yoru CRED form ID, replace "user-field-slug" with the custom user field slug.