Last active
August 29, 2015 14:13
-
-
Save dsyddall/fb2bf7b3bbcdf395a2de to your computer and use it in GitHub Desktop.
Lock aspect ratio in WooCommerce 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 | |
function display_aspect_ratio() { | |
global $post; | |
if ($post) { | |
$aspect = get_post_meta($post->ID, 'aspect_ratio', true); | |
if ($aspect) { | |
echo '<input type="hidden" id="aspect_ratio" name="aspect_ratio" value="' . esc_attr( $aspect ) . '" />'; | |
} | |
} | |
} | |
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 ( parseFloat(jQuery( '#aspect_ratio' ).val()) && parseFloat(jQuery( '#length_needed' ).val()) ) { | |
jQuery( '#width_needed' ).val( Math.round(parseFloat(jQuery( '#length_needed' ).val()) * parseFloat(jQuery( '#aspect_ratio' ).val())) ).keyup(); | |
} | |
jQuery( '#width_needed' ).bind('keyup', update_height); | |
}; | |
update_height = function() { | |
jQuery( '#length_needed' ).unbind('keyup', update_width); | |
if ( parseFloat(jQuery( '#aspect_ratio' ).val()) && parseFloat(jQuery( '#width_needed' ).val()) ) { | |
jQuery( '#length_needed' ).val( Math.round(parseFloat(jQuery( '#width_needed' ).val()) / parseFloat(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