Skip to content

Instantly share code, notes, and snippets.

@kloon
Created January 22, 2014 07:37
Show Gist options
  • Save kloon/8554848 to your computer and use it in GitHub Desktop.
Save kloon/8554848 to your computer and use it in GitHub Desktop.
Limit the width and height values customers can enter using Measurement Price calculator
<?php
// Limit the values customer can enter using measurement price calculator area ( LxW ) option
add_action( 'wp_head', 'limit_measurement_values', 99 );
function limit_measurement_values() {
if ( is_product() ) {
?>
<script>
jQuery( document ).ready(function() {
jQuery( '#length_needed' ).change( function() {
if ( jQuery( this ).val() > 100 ) jQuery( this ).val( 100 ).keyup();
});
jQuery( '#width_needed' ).change( function() {
if ( jQuery( this ).val() > 100 ) jQuery( this ).val( 100 ).keyup();
});
});
</script>
<?php
}
}
?>
@dsyddall
Copy link

dsyddall commented Jan 8, 2015

mitchpowell, to lock the aspect ratio, you could try this:

  1. Add a custom field 'aspect_ratio' to your product (width/height as a decimal).
  2. Add to functions.php or create a plugin:
ID, 'aspect_ratio', true); if ($aspect) { echo ''; } } } add_action( 'woocommerce_single_product_summary', 'display_aspect_ratio', 5 ); add_action( 'wp_head', 'lock_aspect_ratio', 99 ); function lock_aspect_ratio() { if ( is_product() ) { ?>
        < script>
            jQuery( document ).ready(function() {
                var update_width;
                var update_height;
                update_width = function() {
                    jQuery( '#width_needed' ).unbind('keyup', update_height);
                    if ( jQuery( '#aspect_ratio' ).val() ) jQuery( '#width_needed' ).val( Math.round(jQuery( '#length_needed' ).val() \*  jQuery( '#aspect_ratio' ).val()) ).keyup();
                    jQuery( '#width_needed' ).bind('keyup', update_height);
                };
                update_height = function() {
                    jQuery( '#length_needed' ).unbind('keyup', update_width);
                    if ( jQuery( '#aspect_ratio' ).val() ) jQuery( '#length_needed' ).val( Math.round(jQuery( '#width_needed' ).val() /  jQuery( '#aspect_ratio' ).val()) ).keyup();
                    jQuery( '#length_needed' ).bind('keyup', update_width);
                };
                jQuery( '#length_needed' ).bind('keyup', update_width);
                jQuery( '#width_needed' ).bind('keyup', update_height);
            });
        < /script>
    <?php
}

}

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment