skip to Main Content

I have researched this all day but cant seem to get a straight answer how can I get the set product attributes along with the configured terms for each?

This is what I have now

//get the  terms
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();

if ( $attribute_taxonomies ) {
    foreach ($attribute_taxonomies as $tax) {
        
        //dont know what to add here
    }
}

var_dump($taxonomy_terms);

2

Answers


  1. Here below you will get a list of all product attributes and their respective term names:

    echo '<ul>';
    // Loop through WooCommerce registered product attributes
    foreach( wc_get_attribute_taxonomies() as $values ) {
        // Get the array of term names for each product attribute
        $term_names = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name, 'fields' => 'names' ) );
        echo '<li><strong>' . $values->attribute_label . '</strong>: ' . implode(', ', $term_names);
    }
    echo '</ul>';
    

    If you prefer to get an array of the WP_terms objects, you will use:

    // Get the array of the WP_Terms Object for the each product attribute
    $terms = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name );
    

    That will allow use a foreach loop to get what you want from each term…

    Login or Signup to reply.
  2. First check the wc_get_attribute_taxonomies() function with var_dump().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search