Skip to content

Instantly share code, notes, and snippets.

@zainaali
Created September 10, 2021 09:32
Show Gist options
  • Save zainaali/9fd03ffe0f6738c8a74fb4ba7d3b9db4 to your computer and use it in GitHub Desktop.
Save zainaali/9fd03ffe0f6738c8a74fb4ba7d3b9db4 to your computer and use it in GitHub Desktop.
change product quantity based on variation selection
add_action('wp_footer', 'method');
function method()
{
global $post;
//get_product id
$product = get_product( $post->ID );
//if Product is variable add a message
if(is_product() && $product->is_type( 'variable' ) ){
?>
<script>
jQuery(document).ready(function() {
var cort_pric = parseFloat(jQuery('#cart_pric').val()).toFixed(2);
var sample_pric = parseFloat(jQuery('#simple_pric').val()).toFixed(2);
if(isNaN(cort_pric) && isNaN(sample_pric)){
console.log('is not number')
}
else{
jQuery( '.variations_form' ).each( function() {
jQuery(this).on( 'found_variation', function( event, variation ) {
var str = variation.attributes.attribute_pa_shippingtype;
var matches = str.match(/(\d+)/);
if (matches) {
var shiping_number = matches[0];
var get_pric = jQuery('#cart_pric').val();
var cart_prc = parseFloat(get_pric).toFixed(2);
jQuery('[name="quantity"]').val(shiping_number);
}
else{
jQuery('.per_pc').remove();
jQuery('[name="quantity"]').val('1');
}
});
});
}
});
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment