Sarathlal N

Get variation product data on selecting variation options - WooCommerce

In one of my recent WooCommerce project, I have to show some custom message on variation selection in product detail page as per selected variation.

I have checked few options and finally find out some interesting jQuery events in WooCommerce add-to-cart-variation.js. The WooCommerce provides triggers throughout the add-to-cart-variation.js which allows us to hook into change events.

Below you can find two jQuery events that fire after user select a variation using drop down options. The details about the variation is also available as an object.

$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
	alert( variation.variation_id );
	console.log( variation );
} );


$( document ).on( "found_variation.first", function ( e, v ) {
	alert( v.variation_id );
	console.log( v );
} );

Just check your console and you can see whole data like variation price, image, stock status etc.

Additionally, if you have to do some on variation option selection, use below event.

$( ".variations_form" ).on( "woocommerce_variation_select_change", function () {
	alert( "Options changed" );
} );

The woocommerce_variation_select_change Fires when the select is changed.

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.