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.
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'");
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();
wc_attribute_taxonomy_id_by_name('pa_foobar')
). – Artur Czyżewskiget_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