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
}
}
?>
@mitchpowell
Copy link

Hi kloon,
I'd like to know if this snippet will help me do what I need. (I can't quite tell, since I'm not sure what 'keyup' is.)

I hope to modify the Measurement Price Calculator so that the buyer can choose to enter a preferred width OR a preferred height, and the other measurement is dictated by the aspect ratio.

I don't think this is possible out of the box.

Buyers pay by the square inch for artworks at a fixed aspect ratio. They can choose paper or canvas at different pricing, and they can specify they want it to be 'X length' OR 'X width' and the calculation maintains the aspect ratio and also limits them to a minimum and maximum X.

I wonder if you have any suggestions. Thank you,
Mitch

@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