Navigation überspringen

[Gelöst] Cred Conditions using attributes of Generic Select selections

This support ticket is created vor 5 Jahren, 5 Monaten. 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)

Dieses Thema enthält 10 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von alexG-4 vor 5 Jahren, 5 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1296865

In a form, a user selects a "Trigger Type" post from a Generic Select field.

I want to create a Conditional Group based on the Trigger Type selected.

I eventually figured how to write a condition that checks for a specific selection. I found that I have to check for the post ID like this:

( $(trigger-types-lookup) eq '2364' )

However, there will be a large number of possible Trigger Types, and what I really want to check for is not the ID of the Trigger Type post, but title of a post that is in the relationship "Asset of Trigger Type" with it. That will require far few conditional fields.

I can't find any Toolset features that will help me do this, so I'm expecting to write a shortcode. So, the condition in the Conditional Group would be something like:

( [my_shortcode ... ] eq 'Membership' )

So that the Group will be shown only if the selected "Trigger Type" has an "Asset of Trigger Type" relationship with the "Asset" post whose title is "Membership".

My problem is I don't know how my shortcode php can access the required information in the form.

1) Is there some Global variable that contains the current values of the fields in the form?

OR

2) Is there some way I can pass the ID of the selected "Trigger Type" using attributes?

e.g.
( [my_shortcode trigger_type_ID=???? ] eq 'Membership']

... where I want ??? to be, in effect, $(trigger-types-lookup)

Is this possible?

Thanks

Alex

#1297061

WordPress shortcodes are executed on the back-end using PHP before the page is rendered, but cred conditionals are executed on the front-end using JavaScript after the page is rendered. In other words, when you select some generic select value on the front-end, no shortcodes will be re-evaluated. Only once, when the page is rendered, will a shortcode be evaluated. So you'll have to use shortcodes in a way that provides all the necessary information to the front-end when the page is loaded. If there are a limited number of "Asset" posts, you may be able to effectively use the CONTAINS operator with a custom shortcode. https://toolset.com/documentation/user-guides/cred-conditional-display-engine/

For example, let's say you create a shortcode that returns a comma-separated list of Trigger Type IDs whose parent title is "Membership". That shortcode looks like this:

[my_shortcode trigger_type="Membership"]

It outputs something like this:

1,2,3

Your conditional group codes would look like this:

[cred_show_group if="( CONTAINS( ARRAY( [my-shortcode trigger_type='Membership']), $(trigger_types_lookup) ) )"  mode="fade-slide"]
...Membership stuff...
[/cred_show_group]

[cred_show_group if="( CONTAINS( ARRAY( [my_shortcode trigger_type='Awards']), $(trigger_types_lookup) ) )" mode="fade-slide"]
...Awards stuff...
[/cred_show_group]

[cred_show_group if="( CONTAINS( ARRAY( [my_shortcode trigger_type='Other']), $(trigger_types_lookup) ) )" mode="fade-slide"]
...Other stuff...
[/cred_show_group]
#1297151

That solves my problem perfectly!

Excellent explanation about why I can't do it the way I was proposing, and an alternative suggestion that is precisely what I need.

Thanks, Christian!

On a Sunday, as well!

Alex

#1297161

... well, it SHOULD work in theory!

I wrote the shortcode, and that works fine.

But I'm having a problem with the operations of CONTAINS in a CRED condition.

To isolate the problem, I ignored my shortcode and hard-coded some IDs in for testing purposes.

So - this condition does NOT work

( CONTAINS( ARRAY( 2384, 2382 ), $( auto-trigger-types-lookup ) ) )

But this one DOES

( $(auto-trigger-types-lookup) eq 2384 ) OR ( $(auto-trigger-types-lookup) eq 2382 )

As far as I can see, those two conditions should work identically.

You can see me testing this here

versteckter Link

Somewhat clutching at straws, I also tried these as alternatives, but with no success
( ARRAY( 2384, 2382 ) = $( auto-trigger-types-lookup ) )
and
( ARRAY( 2384, 2382 ) eq $( auto-trigger-types-lookup ) )

Any ideas?

Alex

#1298013

Okay thanks, I an see that you're using the drag and drop Form builder but I'm using expert mode to insert my condiional shortcodes. Let me run some tests to see if that could be a factor here, because it's working for me in my local tests.

#1298061
( CONTAINS( ARRAY( 2384, 2382 ), $( auto-trigger-types-lookup ) ) )

... 

( $(auto-trigger-types-lookup) eq 2384 ) OR ( $(auto-trigger-types-lookup) eq 2382 )

After a lot of messing around, I can see the CONTAINS ARRAY syntax example has spaces around the generic field slug. I think this is part of the problem...can you change it to this:

( CONTAINS( ARRAY( 2384, 2382 ), $(auto-trigger-types-lookup) ) )
#1298127

I think that's how I had it originally, Christian.

Anyway, I just tested it using exactly what you provided - still no difference.

Have you not been able to reproduce it at all?

I could see if can reproduce it on a simpler form if that would help.

Alex

#1298169

Christian

I've tested the "Contains" formulation on a bare-bones form - and it works!

I'll keep adding more items to the form to make it more like the one that fails, until I get the same result. That may make it possible to isolate the cause.

At least it shows the issue is with the form and nothing deeper to do with my setup.

I'll get back to you shortly.

Alex

#1298177

Solved!

The code that displays the (filtered view) of the Auto Trigger Types is

<!-- Memberships --><div class='form-group kam-generic-select-group'>
<label>[wpv-post-body view_template="snippet-forms-label-for-pick-a-trigger-type"]</label>
[cred_generic_field type='select' field='auto-trigger-types-lookup-membership' class='kam-generic-selector']
{
"required":1,
"options":[ [wpv-view name='asset-json-auto-trigger-type-grouping' wpvrelatedto=[kam_get_post_id slug="membership" type="asset-types"] ] ]
}
[/cred_generic_field]
</div>

Except that the 'field' attribute was auto-trigger-types-lookup, and not auto-trigger-types-lookup-membership, as shown above.

The problem was that I had several other filtered views of Auto Trigger Types, all using the same 'field' attribute of auto-trigger-types-lookup.

I've now changed all those attributes by appending the relevant Asset Type slug ('membership', 'product' etc.).

Of course, I also had to change the subsequent conditions to include that slug, e.g.

( CONTAINS( ARRAY( 2384, 2382 ), $(auto-trigger-types-lookup-membership) ) )

I don't know if you can explain why the issue didn't arise with the earlier formulation of the condition, or whether this is something Toolset need to be made aware of - but for my part, I'm happy to work with this.

If you need any more details from me, let me know.

Thanks for your help, Christian.

Alex

#1298615

Well it sounds like earlier you had a conditional based on the selected generic field value, but you actually had several generic fields with the same slug. So I'm not surprised the results were unexpected. I don't really see the need for more in-depth investigation here. Glad it's working for you now!

#1298641

My issue is resolved now. Thank you!