WooCommerce: Get Currently Selected Variation ID

We’ve seen a lot of PHP so far on Business Bloomer – WooCommerce after all is a bunch of PHP files! However, sometimes PHP is just not enough, mostly when you need to work with variable products and the “currently selected variation”.

In fact, WooCommerce uses jQuery (a JavaScript Library) to handle variations on the frontend and show conditional content (variation price, description, add to cart) based on the dropdown selection. So, to detect the current variation ID we must use JQuery as well. And here’s how!

WooCommerce: Detect Selected Variation ID

PHP/jQuery Snippet: Get Currently Selected Variation ID @ WooCommerce Single Product Page

I won’t go into too much detail, but in order to “get” the variation ID I thought of a nice workaround. When you select a variation, WooCommerce assigns the variation ID to a hidden input on the frontend:

<input type="hidden" name="variation_id" class="variation_id" value="293">

Therefore, there is no need to “redo” the work – let’s take advantage of this existing script and simply “read” the value of the input when the input changes ๐Ÿ™‚

In my snippet, an alert (see screenshot above) will popup when one of the variation dropdown options is selected – but of course you can do much more with this, such as loading conditional content, executing PHP, running events, and so on.

 * @snippet       Get Currently Selected Variation ID @ WooCommerce Single Product Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=76782
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.4.3

add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_display_dropdown_variation_add_cart' );

function bbloomer_display_dropdown_variation_add_cart() {
	global $product;
	if ( $product->is_type('variable') ) {
		jQuery(document).ready(function($) {
			$('input.variation_id').change( function(){
				if( '' != $('input.variation_id').val() ) {
					var var_id = $('input.variation_id').val();
					alert('You just selected variation #' + var_id);

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

4 thoughts on “WooCommerce: Get Currently Selected Variation ID

  1. Hi,

    I would like for the single product page to display the parent SKU even when a variation has been selected, while ensuring the variation’s SKU is still distinct from the parent SKU.
    As a workaround of the above I would even accept not to display any SKU when a variation is selected.
    I do have a meta.php within my theme that is echoing product meta. The condition there is given by this line:

    <?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>

    I am seeking for a condition that defines when a variation is selected.

    Thank you.

    1. Hello Attila – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  2. Hi Rodolfo, this snippet works perfectly, but please, what about if I need to display the Name of selected variation? (in my case adding this after the title?)


    1. Luca – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

Questions? Feedback? Support? Leave your Comment Now!
If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.