Event Tickets Plus and WooCommerce: Set child input based on forced quantity sync with parent input.
<?php | |
/** | |
* Event Tickets Plus and WooCommerce: | |
* Set child input based on forced quantity sync with parent input. | |
* | |
* You need to edit the product IDs (1129 parent and 1128 child in this example code) | |
* | |
* From https://gist.github.com/cliffordp/5a769159a2bf64f0b1b1dbbde243d109 | |
* GIF preview: https://cl.ly/1F1N0h211b1w | |
* | |
* Note: does not protect against user altering their WooCommerce cart after correct quantities got added to cart | |
* For more robust protections against such user activity, you may need to refer to https://docs.woocommerce.com/document/group-bundle-products-woocommerce/ | |
*/ | |
function cliff_et_plus_woo_sync_quantity() { | |
wp_enqueue_script( 'jquery' ); | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready( function () { | |
// Get the parent | |
parent = jQuery( 'table.tribe-events-tickets input[name="quantity_1129"]' ); | |
// Get the child | |
child = jQuery( 'table.tribe-events-tickets input[name="quantity_1128"]' ); | |
// Disable child input | |
// child.prop( 'disabled', true ); // if input is disabled, will not be added to WooCommerce cart | |
// Set child quantity to double the parent quantity when parent quantity changes | |
jQuery( parent ).on( "input", child, function() { | |
child.val( Math.ceil( parent.val() * 2 ) ) // should not need to round up to next integer but is good safety net in case formula changes | |
}); | |
// Set parent quantity to half the parent quantity when child quantity changes | |
jQuery( child ).on( "input", parent, function() { | |
parent.val( Math.ceil( child.val() / 2 ) ) | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer', 'cliff_et_plus_woo_sync_quantity' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment