Skip to content

Instantly share code, notes, and snippets.

@jetsloth
Created July 29, 2021 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jetsloth/d02ec7e8c83fb07a4b5c258e3ecc16c9 to your computer and use it in GitHub Desktop.
Save jetsloth/d02ec7e8c83fb07a4b5c258e3ecc16c9 to your computer and use it in GitHub Desktop.
Toggle the WooCommerce elements (subtotal, total, quantity and add to cart) based on whether the last Collapsible Section is open or not. For use with WooCommerce Gravity Forms Product Addons and Collapsible Sections. The code below can be added to a HTML field in your form.
<script type="text/javascript">
(function($){
function toggleAddToCart( $form, show ) {
if ( typeof $form === 'undefined' || !$form.length ) {
return;
}
if ( show !== true ) {
show = false;
}
$form.find('.product_totals, .quantity, button[name="add-to-cart"]').toggle( show );
}
$(document).ready(function(){
var $lastSection = $('.gform_variation_wrapper .collapsible-sections-field:last');
var lastSectionID = $lastSection.attr('id');
var csDelay;
$('body').on('click', '.collapsible-sections-field', function(e){
var $section = $(this);
clearTimeout(csDelay);
csDelay = setTimeout(function(){
var show = ( $section.attr('id') === lastSectionID && $section.data('collapsible-closed') === false );
toggleAddToCart( $section.closest('form'), show );
}, 100);
});
setTimeout(function(){
toggleAddToCart( $lastSection.closest('form'), $lastSection.data('collapsible-closed') === false );
}, 100);
});
})(jQuery);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment