Last active
August 29, 2015 14:23
-
-
Save karpstrucking/a89cb6e9c50af18fa4df to your computer and use it in GitHub Desktop.
Delay the reveal of the WooCommerce - Gravity Forms Product Add-Ons form until clicked
This file contains hidden or 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
/* | |
* Delays the display of the associated Gravity Form until the visitor has clicked an "Add to cart"-style button | |
* Requires WooCommerce and WooCommerce - Gravity Forms Product Add-Ons plugins | |
*/ | |
add_action( 'wp_footer', 'gowp_woocommerce_gravityforms_reveal_delay' ); | |
function gowp_woocommerce_gravityforms_reveal_delay() { | |
if ( class_exists( 'WooCommerce' ) && class_exists( 'woocommerce_gravityforms' ) && is_product() ) { | |
?> | |
<script type="text/javascript"> | |
var gravityForm = jQuery( '.cart > .gform_wrapper:not(:has(.gform_validation_error))' ); | |
console.log( gravityForm ); | |
if ( gravityForm.length > 0 ) { | |
var addToCart = jQuery( '.cart .single_add_to_cart_button' ); | |
var selectOptions = addToCart.clone(); | |
gravityForm.hide(); | |
addToCart.hide(); | |
selectOptions.html( 'Select Options' ); | |
selectOptions.attr( { | |
class: 'alt button', | |
id: 'options_display_button' | |
} ); | |
selectOptions.insertAfter( addToCart ); | |
selectOptions.click( function() { | |
gravityForm.slideDown(); | |
addToCart.show(); | |
selectOptions.hide(); | |
return false; | |
} ); | |
} | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just a working example. In practice, the JS here should probably be queued correctly with a jquery dependency. Also, the JS itself can be optimized.