Skip Navigation

[Resolved] Conditional with product categories is no longer working

This thread is resolved. Here is a description of the problem and solution.

Problem: Some of my conditionals that previously worked to test Product Category assignment no longer work as expected.

Solution: Use the following syntax with the WordPress function has_term to test whether or not the current post has one or more of the desired terms assigned.

[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) eq '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]
...this post has the terms 'Discogs' and 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]
 
 
[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) ne '1' )"]
...this post has neither the term 'Discogs' nor the term 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]
 
 
[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]
...this post has the term 'Vinyl' but not the term 'Discogs' in the Product Category taxonomy.
[/wpv-conditional]
 
 
[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) eq '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) ne '1' )"]
...this post has the term 'Discogs' but not the term 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]
This support ticket is created 3 years, 7 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 4 replies, has 2 voices.

Last updated by dirkV-2 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2073761

Tell us what you are trying to do?
I made a template for woocommerce single product (content template) which showed product info and syntax based on conditional logic for product categories. It worked for a long time, but it stopped working suddenly.

Is there any documentation that you are following?
this is the code:
<!-- Discogs Imports Layout -->
[wpv-conditional if="( CONTAINS(#(product_cat),'Discogs') )" AND ( CONTAINS(#(product_cat),'Vinyl') )"]
<div class="ps-tabs-container no-discogs">
<div class="tabs">
<div class="tab-header">
<section class="active">About</section>
<section>Tracklist</section>
<section>Catalogue</section>
<section>Grading</section>
<section>Media</section>
</div>
<div class="tab-indicator"> </div>
<div class="tab-content">
<section class="active">[wpv-conditional if="( '[wpv-post-field name="wpcf-discogs-information"]' ne '' )"][wpv-post-field name="wpcf-discogs-information"][/wpv-conditional]<br>[discogs_about]</section>
<section>[discogs_tracklisting]<br>[ps_tracklisting]
</section>
<section>[display_format][discogs_reference]</section>
<section>[discogs_grading]</section>
<section>[ps_yt]<br>[wpv-conditional if="( '[wpv-post-field name="wpcf-spotify-link"]' ne '' )"][Spotify][/wpv-conditional]</section>
</div>
</div>
</div>
[/wpv-conditional]

<!-- Pre-orders Layout -->
[wpv-conditional if="( !CONTAINS(#(product_cat),'Discogs') )" AND ( CONTAINS(#(product_cat),'Vinyl') )"]
<div class="ps-tabs-container discogs">
<div class="tabs">
<div class="tab-header">
<section class="active">About</section>
<section>Tracklist</section>
<section>Catalogue</section>
<section>Media</section>
</div>
<div class="tab-indicator"> </div>
<div class="tab-content">
<section class="active">[wpv-post-body view_template="None"]</section>
<section>[discogs_tracklisting]<br>[ps_tracklisting]
</section>
<section>[display_format]Label:<span class="references">[ps_label][wpv-post-taxonomy type="pa_label"]</span><br>Catalogue number:<span class="references">[wpv-conditional if="( '[wpv-post-field name="wpcf-cat_numbers"]' ne '' )"][wpv-post-field separator=", " name="wpcf-cat_numbers"][/wpv-conditional][ps_catalogue]</span></section>
<section>[wpv-conditional if="( '[wpv-post-field name="wpcf-video-link"]' ne '' )"][embedyt][wpv-post-field name="wpcf-video-link"][/embedyt][/wpv-conditional]<br>[wpv-conditional if="( '[wpv-post-field name="wpcf-spotify_link"]' ne '' )"][Spotify][/wpv-conditional]</section>
</div>
</div>
</div>
[/wpv-conditional]

<!-- Peckhamsoul Designs Layout -->
[wpv-conditional if="( !CONTAINS(#(product_cat),'Vinyl') )" AND ( !CONTAINS(#(product_cat),'Discogs') )"]
<div class="ps-tabs-container no-vinyl">
<div class="tabs">
<div class="tab-header">
<section class="active">About</section>
</div>
<div class="tab-indicator"> </div>
<div class="tab-content">
<section class="active">[wpv-conditional if="( has_term('discogs', 'product_cat', null) ne '1' )"][wpv-post-body view_template="None"][/wpv-conditional]</section>
</div>
</div>
</div>
[/wpv-conditional]

Even if the product doesn't have the category 'Vinyl', it still showes the structure related to that conditional...

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2073951

Hello, I see two different syntaxes used in these conditionals testing taxonomy terms.

1. Testing with has_term:

[wpv-conditional if="( has_term('discogs', 'product_cat', null) ne '1' )"]
  [wpv-post-body view_template="None"]
[/wpv-conditional]

2. Testing with functions CONTAINS and #:

[wpv-conditional if="( !CONTAINS(#(product_cat),'Vinyl') )" AND ( !CONTAINS(#(product_cat),'Discogs') )"]
...
[/wpv-conditional]

I'm not familiar with the "#" function used here. It could be a syntax that was supported in a previous version of the plugin, but has been deprecated and is no longer supported, or I suppose it could be a custom function you created and registered? At any rate, the has_term function is definitely still supported and available, so I recommend using it exclusively. Replace the CONTAINS and # conditional clauses with has_term.
https://developer.wordpress.org/reference/functions/has_term/

Since it's already used here, I assume the has_term function is already registered in Toolset > Settings > Front-end Content > Functions inside conditional evaluations. If not, you must register the function has_term to use it effectively. Then you will rewrite the existing conditional clauses using CONTAINS and # to use has_term instead, as in these examples:

[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) eq '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]
...this post has the terms 'Discogs' and 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]


[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) ne '1' )"]
...this post has neither the term 'Discogs' nor the term 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]


[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]
...this post has the term 'Vinyl' but not the term 'Discogs' in the Product Category taxonomy.
[/wpv-conditional]


[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) eq '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) ne '1' )"]
...this post has the term 'Discogs' but not the term 'Vinyl' in the Product Category taxonomy.
[/wpv-conditional]

Let me know if this isn't working as expected, or if I've misunderstood the problem, and I can take a closer look.

#2075687

Hi Christian,

thank you for your time and nice explanation. Still there is a strange thing.
If the product neither has the term 'Discogs' nor 'Vinyl', the
[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' )" AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]

...this post has the term 'Vinyl' but not the term 'Discogs' in the Product Category taxonomy.

[/wpv-conditional]
is executed...
I don't know why? Any suggestions?
Thank you,
Kind regards, Dirk

#2076463

Sorry for the confusion, but it looks like I made a mistake in my first example and copied that into each of the other examples. There is an extra double-quote in each example before AND, causing this problem. See here:

[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' )" AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]

Try removing that double quote like so:

[wpv-conditional if="( has_term( 'Discogs', 'product_cat', null ) ne '1' ) AND ( has_term( 'Vinyl', 'product_cat', null ) eq '1' )"]

I will update my examples to remove that extra double quote. Please let me know if the problem is still not resolved and I can take another look.

#2076871

My issue is resolved now. Thank you!