Created
February 13, 2020 05:47
-
-
Save webtoffee-git/47f0d212d30101b12c5c0dc87684e81a to your computer and use it in GitHub Desktop.
WooCommerce restrict only Shipping State for selected country
This file contains 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
add_action( 'wp_footer', 'webtoffee_checkout_shipping_filter_it_states' ); | |
function webtoffee_checkout_shipping_filter_it_states() { | |
if ( ! is_checkout() ) { | |
return; | |
} | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
$(document.body).on('country_to_state_changed', function() { | |
var $shipping_country = $('#shipping_country'); | |
var new_shipping_state = ''; | |
switch($shipping_country.val()) { | |
case 'IT': | |
new_shipping_state = {'TV': "Treviso", "CA": "Carità", "Cs": "Castrette"}; | |
break; | |
} | |
if( ! $.isEmptyObject(new_shipping_state)) { | |
var optionsAsString = ""; | |
for (var key in new_shipping_state) { | |
optionsAsString += "<option value='" + key + "'>" + new_shipping_state[key] + "</option>"; | |
} | |
$( 'select[name="shipping_state"]' ).html( optionsAsString ); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment