WordPress Contact Enquiry Form get Product Variation Data (Title, Color, Size etc)

iEntegre
2 min readJul 20, 2022

First You need a plugin for use short code (dynamictext, hiddendynamic)

You can download here (Contact Form 7 Dynamic Text Extension)

Here we will make additional modifications to reflect the Product information as additional information in the Enquiry form.

You can obtain additional information by editing the codes below as you wish.

Add the following codes to your theme’s functions.php file or you can embed it in any plugin.

/**
* iEntegre WordPress Modification
* WordPress Contact Enquiry Form get Product Variation Data (Title, Color, Size etc)
*/
// Clear cache First Remove action Modification
remove_action( 'wp_footer', 'ientegre_copy_variation_info_js' );
// Add Modification to WordPress Footer Section
add_action( 'wp_footer', 'ientegre_copy_variation_info_js' );
function ientegre_copy_variation_info_js() {
if ( !is_product() ) {
return;
}
$product = wc_get_product( get_the_ID() );
if ( ! 'variable' == $product->get_type() ) {
return;
}

?>
<script>
jQuery( document ).ready( function( $ ) {
var $variations_form_select = '.variations_form select';

var copyVariationsToInput = function() {

variations_values = [];
jQuery( $variations_form_select + ' option:selected' ).each( function() {
variations_values.push( jQuery( this ).text() );
});

// Debug Area
// alert( "Variations values: " + variations_values.join( ', ' ) );

// Product Title And Selected Variations (Color, Size etc...)
// Example Output: Product Name / Yellow,Large
document.getElementById("selected_product").setAttribute("value", "<?php echo $product->get_title();?> / " + variations_values.join( ', ' ));

// Only Selected Variations Data (Color, Size etc...)
jQuery("input[name=selected_variations]").val( variations_values.join( ', ' ) );
}

// Copy when dropdowns change.
jQuery('body').on('found_variation', function(){
copyVariationsToInput()
});
});
</script>
<?php
}

Now, Edit Contact Form (Enquiry Form)

Mailing Form

[selected_product]

you can get data with this shortcode

now go test…

--

--

iEntegre
0 Followers

We teach those who want to learn