Skip to content
Advertisement

Get product variation id by their product attributes slug values pairs in Woocommerce

How can I get product variation ID by taxonomy terms?

For example if I have two taxonomies and their terms:

And my product variations:

I would like to get variation ID of this terms combination medium > green.

Advertisement

Answer

The following function uses a very light SQL query to get the variation ID for from a variable product for defined product attributes “color” and “size” term slugs:

Code goes in function.php file of your active child theme (or active theme). Tested and works.


USAGE (2 cases examples):

  1. From a defined variable product ID (where 746 is the parent variable product ID):

    $variation_id = get_product_variation_id( ‘large’, ‘blue’, 746 );

  2. On the variable product single product page (No need to define the variable product ID):

    $variation_id = get_product_variation_id( ‘large’, ‘blue’ );

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement