Skip Navigation

[Resolved] Split: Bug after upgrading to CRED version 1.9.4

This support ticket is created 6 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 27 replies, has 2 voices.

Last updated by Christian Cox 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#610527

I have this field [cred_field field='product_cat' display='select' single_select="true"] placed two times in a single cred edit form.

The right category is saved to the wp backend when creating the post, however on the frontend of the CRED edit form the wrong category is selected.

This doesn't happen with version 1.9.3.1. Again I am going back to this version.

Regards,
Nick

#610604

Hello. Thank you for creating this new ticket Christian.

#610773

Can you share the code for the entire CRED edit form here? I do not think two identical fields in the same form is a good idea, so I'm hoping we can do some reorganization to prevent the need for both.

#611547

Hello Christian. Thank you for helping me out with this:
Here's the code:

[credform class='cred-form cred-keep-original']
[cred_field field='form_messages' value='' class='alert alert-warning']
[cred_field field='actor-1' post='product' value='' placeholder='YouTuber 1 Name' urlparam='' class='sfc-campaign-new-input']
[cred_field field='product_cat' display='select' single_select="true"]
[cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']
[cred_field field='actor-2' post='product' value='' placeholder='YouTuber 2' urlparam='' class='sfc-campaign-new-input']
[cred_field field='product_cat' display='select' single_select="true"]
[cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']-
[cred_generic_field field='post_content_substitute' type='textarea' class='sfc-campaign-new-textarea' urlparam='' placeholder='Write a few words...']
                                            {
                                            "required":1,
                                            "validate_format":0,
                                            "default":"[wpv-post-excerpt format="noautop" id="[wpv-search-term param='post_id']"]",
                                            "persist":1
                                            }
                                            [/cred_generic_field]

                  
                  
                   [cred_generic_field field='post_status' type='select' class='sfc-campaign-new-select' urlparam='']
                                        {
                                        "required":0,
                                        "validate_format":0,
                                        "default":["[wpv-post-status]"],
                                        "options":[
                                        {"value":"draft","label":"Save for later"},
                                        {"value":"pending","label":"Submit for review"}
                                        ]
                                        }
                                        [/cred_generic_field]

                  [cred_field field='form_submit' value='Submit' urlparam='' class='sfc-campaign-new-button']

[cred_field field='campaign-status' post='product' value='' urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']</span>
[/credform]



There are two text fields named actor-1 and actor-2 where the user can type in the name of his favorite actors.
Both of these actors can have different categories, but they can also share the same category.

First section: Actor 1 choose a category:
[cred_field field='product_cat' display='select' single_select="true"]
[cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']

Second section: Actor 2 choose a category:
[cred_field field='product_cat' display='select' single_select="true"]
[cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']

The add new field is not even showing up on the frontend.
See screenshot and other ticket https://toolset.com/forums/topic/error-found-2-elements-with-non-unique-id/
hidden link
but I am trying to solve another issue in this thread that's when I try to edit the product/post and the categories previously selected and also stored in the database are not selected anymore.

Hope you can help me out with this.
I'd like to divide Actor 1 and Actor 2 in two sections and therefore need the category field 2 times.

#611560

Okay let's back up for a second, because I'm not quite sure what you want to accomplish. Where are the selected product_cat terms associated when the CRED form is submitted? As the CRED form is set up now, the selected product_cat terms are associated with the post created or edited by CRED. In other words, they are all stored together, not split up into two different sections. So if you're trying to associate the selected product_cat terms with the post created by CRED, you could accomplish this with just one set of product_cat term fields. The selected terms are all associated with one post anyway, and cannot be separated again once the form is submitted, i.e. there is no way to know which terms were associated with either Actor field.

However, if you're trying to associate product_cat terms with each favorite Actor in the two Actor input fields, I'm not quite clear how that would be accomplished from a technical perspective. Taxonomies are not associated with custom fields, they are associated with posts of some type. Is there an Actor Custom Post Type? If so, how are the Actor input fields in this CRED form connected to Actor posts?

#611562

Sorry Christian. My bad.

Yes the product_cat terms are associated with the post created or edited by CRED. In the end a product should be created with the form.

I know that I could accomplish this with just one set of product_cat term fields, but in my case both actors can have different categories, but they can also share the same category.

That's why I need the product_cat field two times.

When I say section I just mean that the form is divided into two sections on the frontend.

Section 1 is dedicated two actor 1 the users chooses and section 2 is dedicated two actor 2 the user chooses.
Hope that makes sense.

#611565

But in the end, a product can have just one or two categories, not more.

I'd like to use a single select drop-down menu for that two times instead of a multi-select menu just one time.

[cred_field field='product_cat' display='select' single_select="true"]

#611569

in my case both actors can have different categories, but they can also share the same category.
If Actors are custom field values in this form, you can't associate terms with them individually on the backend. You can only associate terms with the Product, and the selected terms aren't organized into separate groups in that product. They're all together - the Product's terms. Furthermore you can't include duplicate sets of taxonomy fields in a single CRED form, just like you cannot include the same custom field more than once. It's not supported by CRED.

The easiest solution I can think of is to use one set of product_cat taxonomy fields and ask your users to select categories for both Actors at the same time. It doesn't matter if both Actors have different categories, because they are all stored with the Product anyway in one group. Similarly it doesn't matter if both Actors have the same categories, because you can't associate a term with a Product more than once. If you look at the Product later, the terms can't be differentiated by Actor so you will never know which term was added for which Actor, which ones were duplicated, etc.

If your User experience doesn't really fit with selecting both Actors at the same time in one set of taxonomy fields, I can think of several more complex options:
1. Implement a custom generic field with options provided by a View, and add custom code to consolidate both sets of inputs and accomplish the exact same results in the backend. In the end, all the terms are still associated with one Product and cannot be separated later by Actor.

2. Create another post type called "Actor" where you can associate taxonomy terms with each Actor. Then single CRED form could be split into multiple CRED forms, one for each Actor, with separate taxonomy inputs for each form. This maintains the organization of terms by Actor. If you need to associate two Actors with each other, you can use a one-to-many relationship with a 3rd post type - parent of both Actor posts. But realistically this would probably be better as a many-to-many relationship, since two Users may like the same Actor.

3. Don't use a taxonomy, use two custom fields on the Product: one custom field for Actor 1 and another for Actor 2. You could select one or more values in these fields, which would allow you to maintain the organization by Actor. Then in Views you can filter by those fields instead of the taxonomy.

Thoughts?

#611571

Sorry I didn't see your previous reply before posting my response. Do you mean that there are only two possible categories, or that there are lots of categories but you can only associate a total of two categories with the product?

#611572

Thank you Christian for providing 3 different solutions according to my need.

You said: "If Actors are custom field values in this form, you can't associate terms with them individually on the backend. You can only associate terms with the Product, and the selected terms aren't organized into separate groups in that product."

Yes, I understand that. I would only associate terms with the Product.

I think I like solution #3 the best. However how's it possible to allow users to add a new category to the custom field like this field does it: [cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']

And there are lots of categories but a user can only associate two categories maximum to one product.

1 minimum and 2 categories maximum.

Another reason why I would use this single select field
[cred_field field='product_cat' display='select' single_select="true"]
two times in one form, because this way I could limit the selection to two categories only.

Also if this is not allowed why does it work?

It works pretty good actually except for the add new field but that's another issue for another day.

#611582

I think I like solution #3 the best. However how's it possible to allow users to add a new category to the custom field like this field does it:
There's not a simple, clean way manage available custom field values in CRED. You're better off with a single taxonomy input field, some form validation on the backend to enforce selection requirements, and possibly some custom JavaScript on the front-end to improve that experience even more.

And there are lots of categories but a user can only associate two categories maximum to one product. 1 minimum and 2 categories maximum.
I can show you how to enforce custom form validation like this on the backend with the cred_form_validate hook. You can display a form error upon submission if the User attempts to select too few or too many terms. More about that API: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Also if this is not allowed why does it work?
I think you're asking why it's possible to insert a field in a CRED form more than once, and yes, this experience could probably be improved some. CRED does not do much to prevent you from introducing errors in the CRED form builder, in general. If you copy a field and paste it again, that's not going to cause an error to show in the backend. You can insert any custom field more than once using the GUI, and this isn't supported either. If you break the field shortcode syntax we don't show any errors on the backend, and so on. If you'd like to create a separate ticket, we will take any suggestions as a feature request.

#611645

Ok I don't need this field necessarily cred_field field='product_cat_add_new' taxonomy='product_cat' type='add_new']
So let's just leave it out.

However, from a user experience in my case, it's just much better to have the select field [cred_field field='product_cat' display='select' single_select="true"] two times. And with the old version of CRED, this works just fine.

Your colleague Minege mentioned something like using a CRED generic field for the second dropdown.
I think you also mean this in solution #2. Do I really need to create an extra view or can I just hardcode the categories/options of the select field in there?

To be honest, I'd like to stick to the post categories, so I decided not create more custom fields.

#611862

I think you also mean this in solution #2. Do I really need to create an extra view or can I just hardcode the categories/options of the select field in there?
Correct, you don't need to create an extra View if you don't mind managing these options manually. Any time a new option becomes available, you must add it manually to the hard-coded options in the generic field(s).

#611876

Thanks Christian. Can you help me set up this generic field in the cred create and edit form?

Can I still keep one of the regular product_cat fields?

#611928

Okay sure, you can keep one of the product_cat fields. Go ahead and set up the terms in the Product Category taxonomy that you will be using as options in the generic field. You need to know the term name and term slug for each option. In the CRED form, click the "Add Generic Fields" button and insert a Select field. In the popup dialog, give the field a name like "alt_product_cat". Include an option for each Product category term. Use the term name as the Default label, and the term slug as the value.

After the generic field code is inserted in your CRED form builder, add the option "persist": 1 to the config object, like this:

[cred_generic_field field='alt_product_cat' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"persist":1,
"options":[
{"value":"term-slug-1","label":"Term Name 1"},
{"value":"term-slug-2","label":"Term Name 2"}
]
}
[/cred_generic_field]

Once you have the generic field visible on the front-end, compare the CRED product_cat field and the Generic alt_product_cat field. They should show the same option labels and each option should have the same value. Adjust as needed.

Next we will add custom code to capture those selections and use them to associate product_cat terms with the post when the form is submitted. Once we have that working, we can move on to form validation. Let me know when you have the generic field placed on the front-end of your site and we can go from there.