Created
January 22, 2014 07:37
-
-
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
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
<?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, to lock the aspect ratio, you could try this:
- Add a custom field 'aspect_ratio' to your product (width/height as a decimal).
- Add to functions.php or create a plugin:
< 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
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