210412 – 現在都只要使用RY的免費外掛就可以囉 – RY WC City Select
只要在”佈景主題編輯器” > function.php
裡面加入
//自動帶入郵遞區號
add_filter("woocommerce_after_checkout_form", "twzipcodefield");
function twzipcodefield() {
$output = '
<script src="https://hellowp.cc/jquery.twzipcode-1.7.14.min.js"> </script>
<script>
var $ = jQuery.noConflict();
function updateValue(){
$("#billing_state").val($("select[name=\'county\']").val());
$("#billing_city").val($("select[name=\'district\']").val());
$("#billing_postcode").val($("input[name=\'zipcode\']").val());
}
$(document).ready(function(){
$("form.woocommerce-checkout").twzipcode({
"detect": function (coords) {
updateValue();
}
});
$("select[name=\'county\']").appendTo($("#billing_state_field"));
$("select[name=\'district\']").appendTo($("#billing_city_field"));
$("input[name=\'zipcode\']").appendTo($("#billing_postcode_field"));
$("select[name=\'county\'],select[name=\'district\']").change(function(){updateValue();})
$("input[name=\'zipcode\']").keyup(function(){updateValue();})
$("#billing_postcode,#billing_state,#billing_city").hide();
})
</script>';
echo $output;
}
就可以了
參考網站:https://hellowp.cc/woocommerce-custom-checkout-filed/
20190731更新
目前確定使用自動帶入郵遞區號時
若使用綠界的貨到付款
在選擇便利商店後
跳轉回來 所有基本資料都會不見需要重填
所以不能自動帶入郵遞區號