2
votes

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?

1

1 Answers

2
votes

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
}