Display product conditions or attributes for a specific product category (woocommerce)

I studied the entire network and forums dedicated to my question, but I can’t provide the correct results. Basically, I am trying to display conditions or product attributes only for a specific product category.

Here is the code I was working on.

<fieldset class="liquor-types-control filter-controls" > <?php $terms = get_terms( 'wine', /*<--Product Category */ 'pa_liquor-types' /* <--Product Attribute */); foreach ( $terms as $term ) : ?> <label> <input type="checkbox" value="<?php echo "." . $term->slug ?>" /> <?php echo $term->name ?> </label> <?php endforeach; ?> </fieldset> ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy. ) ) [error_data] => Array ( ) ) 
+5
source share
4 answers

var_dump shows that you are using taxonomies in WordPress. While I have no experience directly with Wordpress, the documents in Wordpress say:

Prior to 4.5.0, the first get_terms () parameter was a taxonomy or list of taxonomies:

Starting with 4.5.0, taxonomies must go through the "taxonomy" argument in the $ args array:

From the function reference:

 $term = get_term( $term_id, $taxonomy ); 

Gives you the name slug: for example: term-slug-example

 $slug = $term->slug; 

Gives you the name of the term: for example. Term name example

 $name = $term->name; 

First make sure that you are using the correct version - you are using the syntax from previous 4.5.0

Secondly, the error suggests that the pa_liquor-types taxonomy is invalid. You need to check where this is defined.

Check your syntax create_initial_taxonomies() and submit it if necessary.

+4
source

Use; after $ term-> slug and $ term-> name.

  <label> <input type="checkbox" value="<?php echo $term->slug; ?>" /> <?php echo $term->name; ?> </label> 
+3
source

try something like this:

 <?php $terms = get_terms( 'wine', 'pa_liquor-types'); foreach($terms as $term) { ?> <input type="checkbox" value="<?php echo "." . $term['slug'];?>"/> <?php echo $term['name'];?> <?php } ?> 
+3
source

Get the terms of the types of wine and liquors separately, combine them into a single $ terms array and swipe through this array to get all the conditions. Hope this helps, haven't checked the code yet.

 <fieldset class="liquor-types-control filter-controls" > <?php global $post; $terms_wine = get_the_terms(get_the_ID(),'wine'); $terms_liquor = get_the_terms(get_the_ID,'pa_liquor-types'); $terms = array(); foreach($terms_wine as $wine){ array_push($terms,$wind); } foreach($terms_liquor as $liquor){ array_push($terms,$liquor); } foreach ( $terms as $term ) : ?> <label> <input type="checkbox" value="<?php echo "." . $term->slug ?>" /> <?php echo $term->name ?> </label> <?php endforeach; ?> </fieldset> 
0
source

Source: https://habr.com/ru/post/1259701/


All Articles