Load field group for default variation values

Plugin Contributor
Sark

(@sarkware)

I will look into it, meanwhite put the following snippet on your active theme’s functions.php

add_action('woocommerce_after_variations_table', 'trigger_change_event_snippet');
function trigger_change_event_snippet() { ?>

	<script type="text/javascript">
		jQuery(document).ready(function() {			
			$("table.variations select:first-child").trigger("change");
		});
	</script>

	<?php
}

@sarkware thanks for your quick reaction!
I have added this snippet, but there is no changes on the product page yet:
https://shop.e-locks.eu/product/product-1/

Plugin Contributor
Sark

(@sarkware)

Sorry, I made a mistake in the snippet, here is the updated one.

add_action('woocommerce_after_variations_table', 'trigger_change_event_snippet');
function trigger_change_event_snippet() { ?>

	<script type="text/javascript">
		jQuery(document).ready(function() {			
			jQuery("table.variations select:first-child").trigger("change");
		});
	</script>

	<?php
}

@sarkware thank you, Sark! I reuploaded the snippet, but nothing changed. If I open that page with pre-selected variations – field group still isn’t loading.

Plugin Contributor
Sark

(@sarkware)

I think it triggering the change event way too early. we can add a time out like the below. It may solve the issue.

add_action('woocommerce_after_variations_table', 'trigger_change_event_snippet');
function trigger_change_event_snippet() { ?>

	<script type="text/javascript">
		jQuery(document).ready(function() {			
			setTimeout(function() {
				jQuery("table.variations select:first-child").trigger("change");
			}, 800);			
		});
	</script>

	<?php
}

This one worked well! Even 400ms is OK!
Thank you a lot!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer