3

As we know, to get terms of specific attribute added to a product we can use:

$attr_terms = $product->get_attribute( 'attr_slug' );

OR to get all terms of specific attribute regardless of a product we can use

$attr_terms = get_terms( 'pa_attr_slug' );

But how to get all attributes with their terms added to products of specific product category?

Something like:

$cat_attrs = ... ($cat->id);

foreach($cat_attrs as $cat_attr) {

    echo $cat_attr->name; // name of attribute

    foreach($cat_attr->terms as $term) {
        echo $term->name; // name of attribute term
    }
}
LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
stckvrw
  • 1,264
  • 10
  • 31

1 Answers1

5

To get an array of product attributes taxonomies/term names related to a product category, try the following:

// Here define the product category SLUG
$category_slug = 'posters';

$query_args = array(
    'status'    => 'publish',
    'limit'     => -1,
    'category'  => array( $category_slug ),
);

$data = array();
foreach( wc_get_products($query_args) as $product ){
    foreach( $product->get_attributes() as $taxonomy => $attribute ){
        $attribute_name = wc_attribute_label( $taxonomy ); // Attribute name
        // Or: $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
        foreach ( $attribute->get_terms() as $term ){
            $data[$taxonomy][$term->term_id] = $term->name;
            // Or with the product attribute label name instead:
            // $data[$attribute_name][$term->term_id] = $term->name;
        }
    }
}

// Raw output (testing)
echo '<pre>'; print_r($data); echo '</pre>';

You will get something like (an example extract):

Array
(
    [pa_color] => Array
        (
            [9]  => Blue
            [10] => Green
        )
    [pa_size] => Array
        (
            [15] => Small
            [16] => Medium
            [18] => Large
        )
)
LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • Yes, I meant attribute_label, not attribute_name. Name, label... I've mixed them up. The names with the prefix `pa_` are like as "slugs" of attributes and the labels are "titles". But what's difference between `get_taxonomy( $taxonomy )->labels->singular_name` and `wc_attribute_label( $taxonomy )` ? – stckvrw Sep 30 '18 at 16:37
  • @stckvrw They both give the same result on Woocommerce product attribute taxonomies… Looking at [the source code of `wc_attribute_label()`](https://docs.woocommerce.com/wc-apidocs/source-function-wc_attribute_label.html#123-150) the function is heavier as it handle more features for other cases… – LoicTheAztec Sep 30 '18 at 16:56
  • Ok. One more question: the terms of each attribute are displayed not according to their admin sort ordering (I use custom ordering). While if I try to display terms of specific attribute like `$my_terms = get_terms('pa_some_attr_name');foreach($my_terms as $my_term){echo $my_term->name;}` the terms are displayed with correct ordering. How to resolve the issue? – stckvrw Sep 30 '18 at 18:18
  • @stckvrw This should be a new question as it can't be answered in a comment. – LoicTheAztec Sep 30 '18 at 20:20