Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

woocommerce get attribute terms

Writer Andrew Henderson

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.

5 Answers

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>";
}
1

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.

1

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!

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.

0

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, ));
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy