Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 990
Robbie Lewis Avatar asked Apr 29 '18 17:04

Robbie Lewis


People also ask

How do I find product ID for WooCommerce?

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.

How do I get the attribute of a product in WooCommerce?

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.

How do I find my WordPress product ID?

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.


2 Answers

You can use wc_attribute_taxonomy_id_by_name($taxonomy_name).

like image 180
Artur Czyżewski Avatar answered Oct 21 '22 06:10

Artur Czyżewski


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'");
like image 1
Robbie Lewis Avatar answered Oct 21 '22 05:10

Robbie Lewis