Last active
August 27, 2023 11:19
-
-
Save damiencarbery/d95c8fa438ddfda210e3df4d4bfc707a to your computer and use it in GitHub Desktop.
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[dynamic_hidden selected_variations] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Selected variations: [selected_variations] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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: http://www.damiencarbery.com | |
Version: 0.5 | |
WC tested up to: 8.0.2 | |
*/ | |
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() ); | |
// This code is only needed on variable products. | |
if ( ! 'variable' == $product->get_type() ) { | |
return; | |
} | |
?> | |
<script> | |
jQuery( document ).ready( function( $ ) { | |
var variationValuesOnly = true; // Set to false to get variation name and value. | |
var copyVariationsToInput = function() { | |
variations_values = []; | |
if ( variationValuesOnly ) { | |
// If only variation values are needed then use this code. | |
$( '.variations_form select option:selected' ).each( function() { | |
variations_values.push( $( this ).text() ); | |
}); | |
} | |
else { | |
// If variation names and values are required then use this code. | |
$( '.variations tr' ).each( function() { | |
variations_values.push( $(this).find('label').text() + ': ' + $(this).find('select option:selected').text() ); | |
}); | |
} | |
/* | |
// Get price/sale price | |
variation_price = $( '.woocommerce-variation-price bdi' ).text(); | |
variation_sale_price = $( '.woocommerce-variation-price ins' ).text(); | |
// If there is a sale price then variation_price will have both values so remove the sale price. | |
if ( variation_sale_price != '' ) { | |
variation_price = variation_price.replace( variation_sale_price, '' ); | |
} | |
variations_values.push( 'Variation price: ' + variation_price ); | |
if ( variation_sale_price != '' ) { | |
variations_values.push( "Sale price: " + variation_sale_price ); | |
} | |
*/ | |
$("input[name=selected_variations]").val( variations_values.join( ', ' ) ); | |
} | |
// Copy when dropdowns change (really it waits until WooCommerce updates the price info). | |
jQuery('body').on('found_variation', function(){ | |
copyVariationsToInput() | |
}); | |
// If 'WooCommerce Quote or Enquiry Contact Form 7' plugin is used then wait for enquiry button to be clicked. | |
// If this does not work then there may be a need for a short delay (as the form is reset when it's opened). | |
jQuery("body").on("click", ".wqoecf_enquiry_button", function () { | |
copyVariationsToInput() | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment