2

With the following code I populate a select dropdown item in Admin product page which I add, through a function (in my theme's functions.php). For instance, I managed to get the list of all my product attributes (taxanomy):

<?php               
    $attributes =  wc_get_attribute_taxonomies();
    if($attributes) {
        foreach ( $attributes as $attribute ) {
            echo '<option value="'. $attribute->attribute_id .'">' . $attribute->attribute_label . '</option>';
        }
    }               
?>  

Any idea how the get the term names (labels) and ids of all the terms under a specific product attribute (taxanomy), for example pa_test?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
evavienna
  • 1,059
  • 11
  • 29

1 Answers1

4

You can use function get_terms() to get all the terms of our product attribute taxonomies as follows (here for product attribute pa_test taxonomy):

$taxonomy = 'pa_test';
$terms    = get_terms( array('taxonomy' => $taxonomy, 'hide_empty' => false) );

// Loop through the terms for 'pa_test' taxonomy
foreach ( $terms as $term ) {
    $term_name = $term->name; // name
    $term_slug = $term->slug; // slug
    $term_id   = $term->term_id; // Id
    $term_link = get_term_link( $term ); // Link
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thx for your quick reply. Unfortunatley it only shows 9 of 41 terms... i don't know why. Never saw anything like that... – evavienna Feb 19 '21 at 22:13
  • 1
    @evavienna Updated, it should show all terms now. Anyway this is the way to get the terms of a custom taxonomy and each product attribute is a custom taxonomy. – LoicTheAztec Feb 19 '21 at 22:30
  • Ok, found the problem. I think we should turn show empty to false to show all the terms. Any idea how to fix (that a stupid question - of course you know) :) Thx for the help – evavienna Feb 19 '21 at 22:31
  • 1
    oops - 28 seconds to late :) – evavienna Feb 19 '21 at 22:31