Skip Navigation

[Resolved] Conditional display based on taxonomy does not work on translated sites

This support ticket is created 4 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by maximilianD 4 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1400505
Bildschirmfoto 2019-12-05 um 10.04.14.png
Bildschirmfoto 2019-12-05 um 10.05.04.png

I'm using the following code to check if a product in the loop has a certain taxonomy (product cat) assigned:

{!{wpv-conditional if="( CONTAINS(#(product_cat),'schallwandler') )"}!}TEST[/wpv-conditional]
{!{wpv-conditional debug="true" if="( has_term('schallwandler', 'product_cat', null) eq '1' )"}!}TEST[/wpv-conditional]
[wpv-conditional if="('[wpv-post-taxonomy type='product_cat' format='slug' ]' eq 'schallwandler' )"]TEST[/wpv-conditional]

This works great on the main language but does not work on the transalted counterpart.

This is the debug info:

####################
wpv-conditional attributes
####################
Array
(
    [debug] => true
    [if] => ( CONTAINS(#(product_cat),schallwandler) )
)

####################
Debug information
####################
--------------------
Original expression: ( CONTAINS(#(product_cat),schallwandler) )
--------------------
After matching 2 numeric strings into real numbers: ( CONTAINS(ARRAY('microphones-haut-parleurs-fr-2',209,'Microphones haut-parleurs','transducteurs','201','Transducteurs'),schallwandler) )
	Matched '209' to 209
After matching 5 numeric strings into real numbers: ( CONTAINS(ARRAY('microphones-haut-parleurs-fr-2',209,'Microphones haut-parleurs','transducteurs',201,'Transducteurs'),schallwandler) )
	Matched '201' to 201
####################
wpv-conditional attributes
####################
Array
(
    [debug] => true
    [if] => ( has_term('schallwandler', 'product_cat', null) = 1 )
)

####################
Debug information
####################
--------------------
Original expression: ( has_term('schallwandler', 'product_cat', null) = 1 )

I would expect that it automatically checks for the translated string.
I don't want to add the translated strings in all my conditional logics.

#1400709

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I also would expect that the translations should be compared automatically, but found on my test site that they are not.

I'm raising an internal ticket about this, but in the meantime I considered how to get it to work.

You can register a custom shortcode to get the term that you want to check.

That seems a bit redundant, but WPML filters the call to the function get_term_by so that it returns the translated term.

So try registering this custom shortcode:

add_shortcode('translated', function ($atts = []) {

    // provide defaults
    $atts = shortcode_atts(
        array(
            'term' => null,
            'tax'  => null
        ),
        $atts
    );

    $term = get_term_by( 'slug', $atts['term'], $atts['tax'] );

    return $term->slug;
});

And here's an example of my using it where I specify the term is 'red' and belongs to the taxonomy 'colour':

<p>Conditionally: [wpv-conditional if="( CONTAINS(#(colour),'[translated term='red' tax='colour']') )"]It's red[/wpv-conditional]</p>
#1401367

Thank you Nigel. I will use your shortcode until this is fixed. Is there a link where I can track the status of the issue?

#1401375

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Let me re-open this for now, and when it's handled by second tier there should be an erratum created which you can watch.

I'll post another update when I see how this progresses.

#1402945

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

We discussed this between supporters and developers and decided there were too many ways that implementing this automatically could introduce problems, and so we won't be making any changes.

Please continue to use the custom shortcode that I provided.

An alternative would be to create different content templates for each language, and insert language-specific conditions in each template.

#1403565

Ok thank you Nigel. Your shortcode does the trick.