我需要覆盖WooCommerce结帐中的所有状态,但是我只需要针对运送部分执行此操作。
所以“ billing_state”在所有当前的意大利州都可以正常工作,我只需要用新州覆盖“ shipping_state”。
使用下面的代码将覆盖billing_states和发货状态。如何修改代码以覆盖“ shipping_states”
预先感谢
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['IT'] = array(
//I have to display only these 3 states in shipping Checkout
'TV' => 'Treviso',
'CA' => 'Carità',
'CS' => 'Castrette',
);
return $states;
}
答案 0 :(得分:0)
这就是您要寻找的。将此添加到您的活动子主题functions.php
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
}