woocommerce get attribute terms
Asked Answered
M

5

22

In Woocommerce you can add global product attributes and terms. So for instance:

Size (attribute)
small  (term)
medium (term)
large  (term)

This is product independent. You can then select from the pre defined attributes on a product.

I need to get all of the terms in an attribute with php. So select the attribute required, eg size, and then return an array including [small,medium,large].

Seems simple enough but I can't find any help on doing this.

Makkah answered 1/5, 2013 at 17:15 Comment(0)
R
38

Slightly confusing, especially when looking through the WooCommerce Docs since there is absolutely no mention of getting a list of the terms/attributes.

The Attributes are saved as a custom taxonomy, and the terms are taxonomy terms. That means you can use native Wordpress functions: Wordpress get_terms() Function Reference

By clicking on an attribute in WooCommerce, you can look in the URL and you can see they are all prepended with 'pa_'

This is likely what you need:

$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}
Rotary answered 7/6, 2013 at 18:45 Comment(1)
How can I count it ?Clinton
G
20

I wanted to be able to get all the different attributes from the backend that were set, and get them in an array for me to work with, I took some code from the class-wc-admin-attributes.php file and modified it for my needs:

<?php

$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();

if ($attribute_taxonomies) :
    foreach ($attribute_taxonomies as $tax) :
        if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
            $taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0');
        endif;
    endforeach;
endif;

var_dump($taxonomy_terms);

exit;

This will loop through all the attribute taxonomies, retrieve the terms for each, leaving you with an array of term objects to work with for each taxonomy.

Gabbey answered 28/4, 2014 at 17:2 Comment(1)
This gave me a good start but I found a slightly better example for anyone looking for a more advanced and recent example, check this out: #53705622Druse
T
7

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

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

For example, if the taxonomy slug is 'date', so the taxonomy will be 'pa_date'. You can also mouse over the attribute name and see the taxonomy name at the bottom of the browser.

enter image description here

I hope it helps!

Townsend answered 12/10, 2018 at 11:59 Comment(0)
B
5

I use this:

echo '<h1>variations</h1>';
mario( $product->get_available_variations());
echo '<h1>Atributos</h1>';
mario($product->get_attributes());
echo '<h1>Poste Terms</h1>';
mario(wp_get_post_terms( $post->ID, 'pa_color'));


function mario($texto){
    echo '<pre>';var_dump($texto);echo '</pre>';
};

Really with: "wp_get_post_terms( $post->ID, 'pa_color')" i search only one term, but the idea is to loop for the key ['name'] that return that function.

Balakirev answered 13/11, 2013 at 2:19 Comment(0)
P
1

Get all attributes with attribute terms in woocommerce.

$attributes = wc_get_attribute_taxonomies();
foreach ($attributes as $attribute) {
    $attribute->attribute_terms = get_terms(array(
        'taxonomy' => 'pa_'.$attribute->attribute_name,
        'hide_empty' => false,
    ));
}
Philips answered 24/2, 2021 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.