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.
A second option is to head over the Products page in your WordPress Admin. In this listing, you'll find the WooCommerce product ID when you hover over a product name. You can additionally search for your product using the product SKU name or product name and hover over the search results to get the Product ID.
Add global attributes to product Add the created attributes to your products. Go to: Products > Add Product (or edit an existing one). Select the Attributes tab in the Product Data. There you can choose any of the attributes that you've created in the dropdown menu.
The second way you can consider getting your WooCommerce product ID is to move in the direction of your page Products in the area of WordPress Admin, when you are here, you could see the list that contains product ID when hovering over one certain product name.
You can use wc_attribute_taxonomy_id_by_name($taxonomy_name)
.
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'");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With