5
votes

I want to get the ID of a Woocommerce product attribute using the attribute name. e.g. pa_foobar

I know that product attributes are taxonomies, but get_taxonomy() doesn't return the taxonomy ID. I can't find a Woocommerce function that does this.

3
This has already been answered in this answer threadLoicTheAztec
@LoicTheAztec not true, you can't find best solution there (to use wc_attribute_taxonomy_id_by_name('pa_foobar')).Artur Czyżewski
@ArturCzyżewski Sorry, but the custom function get_attribute_id_from_name() is a bit lighter than the woocommerce official one, and answers this question too… So yes it's answered before... I could have close this thread as duplicated, which I have not done, to allow people to answer.LoicTheAztec

3 Answers

3
votes

You can use wc_attribute_taxonomy_id_by_name($taxonomy_name).

1
votes

Woocommerce stores attributes in the table wp_woocommerce_attribute_taxonomies. Querying the database directly is not recommended but I was able to get the attribute ID using this code:

global $wpdb;
$attribute_id = $wpdb->get_var("select attribute_id from {$wpdb->prefix}woocommerce_attribute_taxonomies where attribute_name='pa_foobar'");
0
votes

You can use that

woocommerce_get_product_terms

or

get_the_terms()

https://developer.wordpress.org/reference/functions/get_the_terms/

 global $product;
 $id = $product->get_id();