Robbie Lewis
Robbie Lewis

Reputation: 2864

Woocommerce get product attribute ID using name

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.

Upvotes: 6

Views: 12686

Answers (3)

Artur Czyżewski
Artur Czyżewski

Reputation: 833

You can use wc_attribute_taxonomy_id_by_name($taxonomy_name).

Upvotes: 4

Robbie Lewis
Robbie Lewis

Reputation: 2864

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'");

Upvotes: 1

mooga
mooga

Reputation: 3317

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();

Upvotes: -2

Related Questions