Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Contact Form 7 Product Enquiry form for variable products - Watch the variation dropdowns and copy the values to a CF7DTE hidden field. https://www.damiencarbery.com/2019/12/contact-form-7-product-enquiry-form-for-variable-products/
[dynamichidden selected_variations]
Selected variations: [selected_variations]
<?php
/*
Plugin Name: Contact Form 7 Product Enquiry form for variable products
Plugin URI: https://www.damiencarbery.com/2019/12/contact-form-7-product-enquiry-form-for-variable-products/
Description: Watch the variation dropdowns and copy the values to a CF7DTE hidden field.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_action( 'wp_head', 'dcwd_copy_variation_info_js' );
function dcwd_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';
$( $variations_form_select ).change( function() {
variations_values = [];
$( $variations_form_select + ' option:selected' ).each( function() {
variations_values.push( $( this ).text() );
});
//alert( "Variations values: " + variations_values.join( ', ' ) );
$("input[name=selected_variations]").val( variations_values.join( ', ' ) );
} );
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.