Navigation überspringen

[Gelöst] How to use a custom User field in a Conditional for a different CPT?

This support ticket is created vor 1 Jahr, 3 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

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

Zuletzt aktualisiert von Minesh vor 1 Jahr, 3 Monaten.

Assistiert von: Minesh.

Author
Artikel
#2643215

I have a custom User field "user-department" and a custom field "asset-department" added to the WooCommerce Product CPT. When listing Products using a Content Template, I want to use a Conditional to only display certain Product content when the product's 'asset-department' is the same as the current user's 'user-department'. I can't seem to figure out how to configure this - can you help?

#2643503

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please try to use the conditional statement as given under:
- For classic content template you can add it directly to template editor
- For block based content template you should try to add "Fields and Text" block or "Shortcodes" block and add the conditional shortcode within it as follows


[wpv-conditional if="(  '[types usermeta='user-department' user_current='true' output='raw'][/types]' eq '[types field='asset-department' output='raw'][/types]' )"]
   
Your content goes here
   
[/wpv-conditional]

More info:
- https://toolset.com/documentation/legacy-features/views-plugin/#conditional-output-in-views
- https://toolset.com/documentation/customizing-sites-using-php/functions/

#2643509

Hello and thank you for your help, Minesh. However, I'm not sure how to proceed with the solution you provide above. I am using a block-based template, and the content of the Conditional is a "Fields and Text" block which contains the following:

[toolset-edit-post-link content_template_slug='edit-asset-admin' target='self']Edit This Asset[/toolset-edit-post-link]

. This works in another Content Template that doesn't require a Conditional.
In the Content Template that needs the Conditional, I tried replacing the "Fields and Text" block with a "Shortcode" block and putting everything in it:

[wpv-conditional if="(  '[types usermeta='user-department' user_current='true' output='raw'][/types]' eq '[types field='asset-department' output='raw'][/types]' )"][toolset-edit-post-link content_template_slug='edit-asset-admin' target='self']Edit This Asset[/toolset-edit-post-link][/wpv-conditional]

but that doesn't work at all.
I'm happy to give you access so you can look at this situation directly. This is a development project and it's fully backed up.

#2643513

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Sure. Can you please share problem URL and admin access details and what content/fields you want to display when condition is satisfied.

Also share frontend user access details (if required) for testing where for that user you setup the user field "user-department".

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2643577

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

The condition is not working because as you can see with both the custom field groups where you configure your custom field:

Asset Department field:
- versteckter Link

User Department field:
- versteckter Link

If you notice the value for the options setup with both the fields are different.

For field User Department the option "Art Dep" value setup as "Art Dep" and with the field "Asset Department" the option "Art Dep" value setup as "art-dep" so it does not match.

To overcome the option value mismatch I've added the following code to "Custom Code" section offered by Toolset with the code snippet namely "toolset-custom-code":

add_shortcode( 'get_user_field_user_dept', 'func_get_user_field_user_dept' );
function func_get_user_field_user_dept() {
    global $current_user;
    
    $value = get_user_meta($current_user->ID,'wpcf-user-department',true);
    $value = str_replace(" ","-",strtolower($value));
    return $value;
}

And adjusted the conditional as given under:

[wpv-conditional if="(  '[get_user_field_user_dept]' eq '[types field='asset-department' output='raw'][/types]' )"]
[toolset-edit-post-link content_template_slug='edit-asset-admin' target='self']Edit This Asset[/toolset-edit-post-link]
[/wpv-conditional]

I've also register the shortcode name "get_user_field_user_dept" at:
=> Toolset => Settings => Front-end Content => Third-party shortcode arguments

Can you please confirm now it works as expected.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/