Woocommerce中的邮政编码

时间:2018-08-16 08:41:09

标签: wordpress woocommerce

在Woocommerce中,我在带有定义值的下拉列表中更改了邮政编码文本字段。

我在主题文件夹的functions.php文件中使用以下代码:

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' ); 
function custom_override_default_postcode_field( $address_fields ) {     
    // Your postcodes array     
    $postcode_array = array( ‘1000‘=>"1000", ‘1000‘=>"1000", ‘1001‘=>"1001", ‘1002‘=>"1002", ‘1003‘=>"1003", ‘1004‘=>"1004", ‘1005‘=>"1005", ‘1006‘=>"1006", ‘1007‘=>"1007", ‘1008‘=>"1008", ‘1009‘=>"1009", ‘1010‘=>"1010", ‘1011‘=>"1011", ‘1012‘=>"1012", ‘1013‘=>"1013", ‘1014‘=>"1014", ‘1015‘=>"1015", ‘1016‘=>"1016", ‘1017‘=>"1017", ‘1018‘=>"1018", ‘1035‘=>"1035", ‘1040‘=>"1040", ‘1045‘=>"1045", ‘1050‘=>"1050", ‘1051‘=>"1051", ‘1052‘=>"1052", ‘1053‘=>"1053", ‘1054‘=>"1054", ‘1055‘=>"1055", ‘1056‘=>"1056", ‘1057‘=>"1057", ‘1058‘=>"1058", ‘1059‘=>"1059", ‘1060‘=>"1060", ‘1061‘=>"1061", ‘1062‘=>"1062", ‘1063‘=>"1063", ‘1064‘=>"1064", ‘1065‘=>"1065", ‘1066‘=>"1066", ‘1067‘=>"1067", ‘1068‘=>"1068", ‘1069‘=>"1069", ‘1070‘=>"1070", ‘1071‘=>"1071", ‘1072‘=>"1072", ‘1073‘=>"1073", ‘1074‘=>"1074", ‘1075‘=>"1075", ‘1076‘=>"1076", ‘1077‘=>"1077", ‘1078‘=>"1078", ‘1079‘=>"1079", ‘1080‘=>"1080", ‘1081‘=>"1081", ‘1082‘=>"1082", ‘1083‘=>"1083", ‘1084‘=>"1084", ‘1085‘=>"1085", ‘1086‘=>"1086", ‘1087‘=>"1087", ‘1088‘=>"1088", ‘1089‘=>"1089", ‘1090‘=>"1090", ‘1091‘=>"1091", ‘1092‘=>"1092", ‘1093‘=>"1093", ‘1094‘=>"1094", ‘1095‘=>"1095", ‘1096‘=>"1096", ‘1100‘=>"1100", ‘1100‘=>"1100", ‘1100‘=>"1100", ‘1100‘=>"1100", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1101‘=>"1101", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1102‘=>"1102", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1103‘=>"1103", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1104‘=>"1104", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1105‘=>"1105", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1106‘=>"1106", ‘1107‘=>"1107", ‘1107‘=>"1107", ‘1108‘=>"1108", ‘1108‘=>"1108", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1109‘=>"1109", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1110‘=>"1110", ‘1111‘=>"1111", ‘1111‘=>"1111", ‘1111‘=>"1111", ‘1111‘=>"1111", ‘1111‘=>"1111", ‘1111‘=>"1111", ‘1112‘=>"1112", ‘1112‘=>"1112", ‘1112‘=>"1112", ‘1112‘=>"1112", ‘1112‘=>"1112", ‘1112‘=>"1112", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1113‘=>"1113", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1114‘=>"1114", ‘1115‘=>"1115", ‘1115‘=>"1115", ‘1115‘=>"1115", ‘1115‘=>"1115", ‘1115‘=>"1115", ‘1115‘=>"1115", ‘1116‘=>"1116", ‘1116‘=>"1116", ‘1116‘=>"1116", ‘1116‘=>"1116", ‘1116‘=>"1116", ‘1116‘=>"1116", ‘1117‘=>"1117", ‘1117‘=>"1117", ‘1117‘=>"1117", ‘1117‘=>"1117", ‘1117‘=>"1117", ‘1118‘=>"1118", ‘1118‘=>"1118", ‘1119‘=>"1119", ‘1119‘=>"1119", ‘1119‘=>"1119", ‘1120‘=>"1120", ‘1120‘=>"1120", ‘1121‘=>"1121", ‘1121‘=>"1121", ‘1122‘=>"1122", ‘1123‘=>"1123", ‘1123‘=>"1123", ‘1124‘=>"1124", ‘1125‘=>"1125", ‘1125‘=>"1125", ‘1126‘=>"1126", ‘1126‘=>"1126", ‘1127‘=>"1127", ‘1128‘=>"1128", ‘1128‘=>"1128", ‘1135‘=>"1135", ‘1138‘=>"1138", ‘1141‘=>"1141", ‘1144‘=>"1144", ‘1147‘=>"1147", ‘1150‘=>"1150", ‘1151‘=>"1151", ‘1152‘=>"1152", ‘1153‘=>"1153", ‘1154‘=>"1154", ‘1155‘=>"1155", ‘1156‘=>"1156", ‘1157‘=>"1157", ‘1158‘=>"1158", ‘1159‘=>"1159", ‘1160‘=>"1160", ‘1161‘=>"1161", ‘1162‘=>"1162", ‘1163‘=>"1163", ‘1164‘=>"1164", ‘1165‘=>"1165", ‘1166‘=>"1166", ‘1167‘=>"1167", ‘1168‘=>"1168", ‘1169‘=>"1169", ‘1170‘=>"1170", ‘1171‘=>"1171", ‘1172‘=>"1172", ‘1173‘=>"1173", ‘1174‘=>"1174", ‘1175‘=>"1175", ‘1176‘=>"1176", ‘1177‘=>"1177", ‘1200‘=>"1200", ‘1201‘=>"1201", ‘1202‘=>"1202", ‘1203‘=>"1203", ‘1204‘=>"1204", ‘1205‘=>"1205", ‘1206‘=>"1206", ‘1207‘=>"1207", ‘1208‘=>"1208", ‘1209‘=>"1209", ‘1210‘=>"1210", ‘1211‘=>"1211", ‘1212‘=>"1212", ‘1213‘=>"1213", ‘1214‘=>"1214", ‘1215‘=>"1215", ‘1216‘=>"1216", ‘1217‘=>"1217", ‘1218‘=>"1218", ‘1219‘=>"1219", ‘1220‘=>"1220", ‘1221‘=>"1221", ‘1222‘=>"1222", ‘1223‘=>"1223", ‘1224‘=>"1224", ‘1225‘=>"1225", ‘1226‘=>"1226", ‘1227‘=>"1227", ‘1228‘=>"1228", ‘1229‘=>"1229", ‘1230‘=>"1230", ‘1231‘=>"1231", ‘1232‘=>"1232", ‘1233‘=>"1233", ‘1234‘=>"1234", ‘1235‘=>"1235", ‘1250‘=>"1250", ‘1251‘=>"1251", ‘1252‘=>"1252", ‘1253‘=>"1253", ‘1254‘=>"1254", ‘1255‘=>"1255", ‘1256‘=>"1256", ‘1257‘=>"1257", ‘1258‘=>"1258", ‘1259‘=>"1259", ‘1260‘=>"1260", ‘1261‘=>"1261", ‘1262‘=>"1262", ‘1263‘=>"1263", ‘1264‘=>"1264", ‘1265‘=>"1265", ‘1266‘=>"1266", ‘1267‘=>"1267", ‘1268‘=>"1268", ‘1269‘=>"1269", ‘1270‘=>"1270", ‘1271‘=>"1271", ‘1272‘=>"1272", ‘1273‘=>"1273", ‘1274‘=>"1274", ‘1275‘=>"1275", ‘1276‘=>"1276", ‘1277‘=>"1277", ‘1278‘=>"1278", ‘1279‘=>"1279", ‘1280‘=>"1280", ‘1281‘=>"1281", ‘1282‘=>"1282", ‘1283‘=>"1283", ‘1284‘=>"1284", ‘1285‘=>"1285", ‘1286‘=>"1286", ‘1287‘=>"1287", ‘1288‘=>"1288", ‘1289‘=>"1289", ‘1290‘=>"1290", ‘1291‘=>"1291", ‘1292‘=>"1292", ‘1293‘=>"1293", ‘1294‘=>"1294", ‘1295‘=>"1295", ‘1296‘=>"1296", ‘1297‘=>"1297", ‘1298‘=>"1298", ‘1299‘=>"1299", ‘1300‘=>"1300", ‘1301‘=>"1301", ‘1302‘=>"1302", ‘1303‘=>"1303", ‘1304‘=>"1304", ‘1305‘=>"1305", ‘1306‘=>"1306", ‘1307‘=>"1307", ‘1308‘=>"1308", ‘1309‘=>"1309", ‘1350‘=>"1350", ‘1351‘=>"1351", ‘1352‘=>"1352", ‘1353‘=>"1353", ‘1354‘=>"1354", ‘1375‘=>"1375", ‘1376‘=>"1376", ‘1377‘=>"1377", ‘1378‘=>"1378", ‘1379‘=>"1379", ‘1380‘=>"1380", ‘1381‘=>"1381", ‘1400‘=>"1400", ‘1401‘=>"1401", ‘1402‘=>"1402", ‘1403‘=>"1403", ‘1404‘=>"1404", ‘1405‘=>"1405", ‘1406‘=>"1406", ‘1407‘=>"1407", ‘1408‘=>"1408", ‘1409‘=>"1409", ‘1409‘=>"1409", ‘1409‘=>"1409", ‘1410‘=>"1410", ‘1411‘=>"1411", ‘1411‘=>"1411", ‘1412‘=>"1412", ‘1412‘=>"1412", ‘1413‘=>"1413", ‘1413‘=>"1413", ‘1420‘=>"1420", ‘1421‘=>"1421", ‘1422‘=>"1422", ‘1423‘=>"1423", ‘1424‘=>"1424", ‘1425‘=>"1425", ‘1426‘=>"1426", ‘1427‘=>"1427", ‘1428‘=>"1428", ‘1440‘=>"1440", ‘1441‘=>"1441", ‘1442‘=>"1442", ‘1442‘=>"1442", ‘1442‘=>"1442", ‘1443‘=>"1443", ‘1444‘=>"1444", ‘1444‘=>"1444", ‘1444‘=>"1444", ‘1444‘=>"1444", ‘1444‘=>"1444", ‘1444‘=>"1444", ‘1445‘=>"1445", ‘1446‘=>"1446", ‘1447‘=>"1447", ‘1448‘=>"1448", ‘1469‘=>"1469", ‘1470‘=>"1470", ‘1471‘=>"1471", ‘1472‘=>"1472", ‘1473‘=>"1473", ‘1474‘=>"1474", ‘1475‘=>"1475", ‘1476‘=>"1476", ‘1477‘=>"1477", ‘1478‘=>"1478", ‘1479‘=>"1479", ‘1480‘=>"1480", ‘1485‘=>"1485", ‘1489‘=>"1489", ‘1490‘=>"1490", ‘1500‘=>"1500", ‘1502‘=>"1502", ‘1503‘=>"1503", ‘1504‘=>"1504", ‘1530‘=>"1530", ‘1531‘=>"1531", ‘1532‘=>"1532", ‘1533‘=>"1533", ‘1534‘=>"1534", ‘1535‘=>"1535", ‘1536‘=>"1536", ‘1550‘=>"1550", ‘1551‘=>"1551", ‘1552‘=>"1552", ‘1553‘=>"1553", ‘1554‘=>"1554", ‘1555‘=>"1555", ‘1556‘=>"1556", ‘1600‘=>"1600", ‘1601‘=>"1601", ‘1602‘=>"1602", ‘1603‘=>"1603", ‘1604‘=>"1604", ‘1605‘=>"1605", ‘1606‘=>"1606", ‘1607‘=>"1607", ‘1608‘=>"1608", ‘1609‘=>"1609", ‘1610‘=>"1610", ‘1611‘=>"1611", ‘1612‘=>"1612", ‘1620‘=>"1620", ‘1621‘=>"1621", ‘1630‘=>"1630", ‘1631‘=>"1631", ‘1632‘=>"1632", ‘1633‘=>"1633", ‘1634‘=>"1634", ‘1636‘=>"1636", ‘1637‘=>"1637", ‘1638‘=>"1638", ‘1650‘=>"1650", ‘1651‘=>"1651", ‘1652‘=>"1652", ‘1653‘=>"1653", ‘1654‘=>"1654", ‘1655‘=>"1655", ‘1656‘=>"1656", ‘1657‘=>"1657", ‘1658‘=>"1658", ‘1659‘=>"1659", ‘1660‘=>"1660", ‘1661‘=>"1661", ‘1662‘=>"1662", ‘1663‘=>"1663", ‘1664‘=>"1664", ‘1665‘=>"1665", ‘1666‘=>"1666", ‘1667‘=>"1667", ‘1668‘=>"1668", ‘1669‘=>"1669", ‘1670‘=>"1670", ‘1671‘=>"1671", ‘1672‘=>"1672", ‘1673‘=>"1673", ‘1674‘=>"1674", ‘1675‘=>"1675", ‘1676‘=>"1676", ‘1677‘=>"1677", ‘1678‘=>"1678", ‘1679‘=>"1679", ‘1680‘=>"1680", ‘1681‘=>"1681", ‘1682‘=>"1682", ‘1683‘=>"1683", ‘1684‘=>"1684", ‘1685‘=>"1685", ‘1686‘=>"1686", ‘1687‘=>"1687", ‘1688‘=>"1688", ‘1689‘=>"1689", ‘1690‘=>"1690", ‘1691‘=>"1691", ‘1692‘=>"1692", ‘1693‘=>"1693", ‘1694‘=>"1694", ‘1695‘=>"1695", ‘1696‘=>"1696", ‘1697‘=>"1697", ‘1698‘=>"1698", ‘1699‘=>"1699", ‘1700‘=>"1700", ‘1701‘=>"1701", ‘1702‘=>"1702", ‘1703‘=>"1703", ‘1704‘=>"1704", ‘1705‘=>"1705", ‘1706‘=>"1706", ‘1707‘=>"1707", ‘1708‘=>"1708", ‘1709‘=>"1709", ‘1710‘=>"1710", ‘1711‘=>"1711", ‘1712‘=>"1712", ‘1713‘=>"1713", ‘1714‘=>"1714", ‘1714‘=>"1714", ‘1715‘=>"1715", ‘1716‘=>"1716", ‘1717‘=>"1717", ‘1718‘=>"1718", ‘1719‘=>"1719", ‘1719‘=>"1719", ‘1720‘=>"1720", ‘1740‘=>"1740", ‘1741‘=>"1741", ‘1742‘=>"1742", ‘1743‘=>"1743", ‘1744‘=>"1744", ‘1745‘=>"1745", ‘1746‘=>"1746", ‘1747‘=>"1747", ‘1748‘=>"1748", ‘1749‘=>"1749", ‘1750‘=>"1750", ‘1751‘=>"1751", ‘1752‘=>"1752", ‘1770‘=>"1770", ‘1771‘=>"1771", ‘1772‘=>"1772", ‘1773‘=>"1773", ‘1774‘=>"1774", ‘1775‘=>"1775", ‘1776‘=>"1776", ‘1777‘=>"1777", ‘1780‘=>"1780", ‘1799‘=>"1799", ‘1800‘=>"1800", ‘1801‘=>"1801", ‘1802‘=>"1802", ‘1803‘=>"1803", ‘1804‘=>"1804", ‘1805‘=>"1805", ‘1806‘=>"1806", ‘1807‘=>"1807", ‘1808‘=>"1808", ‘1809‘=>"1809", ‘1810‘=>"1810", ‘1811‘=>"1811", ‘1812‘=>"1812", ‘1820‘=>"1820", ‘1850‘=>"1850", ‘1860‘=>"1860", ‘1870‘=>"1870", ‘1880‘=>"1880", ‘1900‘=>"1900", ‘1920‘=>"1920", ‘1930‘=>"1930", ‘1960‘=>"1960", ‘0401‘=>"0401", ‘0410‘=>"0410", ‘0420‘=>"0420", ‘0550‘=>"0550", ‘0560‘=>"0560", ‘0700‘=>"0700", ‘0701‘=>"0701", ‘0702‘=>"0702", ‘0703‘=>"0703", ‘0704‘=>"0704", ‘0705‘=>"0705", ‘0706‘=>"0706", ‘0707‘=>"0707", ‘0708‘=>"0708", ‘0709‘=>"0709", ‘0710‘=>"0710", ‘0711‘=>"0711", ‘0712‘=>"0712", ‘0713‘=>"0713", ‘0714‘=>"0714", ‘0715‘=>"0715", ‘0716‘=>"0716", ‘0717‘=>"0717", ‘0718‘=>"0718", ‘0719‘=>"0719", ‘0720‘=>"0720", ‘0721‘=>"0721", ‘0722‘=>"0722", ‘0723‘=>"0723", ‘0724‘=>"0724", ‘0725‘=>"0725", ‘0726‘=>"0726", ‘0727‘=>"0727", ‘0728‘=>"0728", ‘0729‘=>"0729", ‘0730‘=>"0730", ‘0731‘=>"0731", ‘0732‘=>"0732", ‘0733‘=>"0733", ‘0740‘=>"0740", ‘0750‘=>"0750", ‘0760‘=>"0760", ‘0770‘=>"0770", ‘0780‘=>"0780", ‘0788‘=>"0788", ‘0790‘=>"0790", ‘0800‘=>"0800", ‘0801‘=>"0801", ‘0802‘=>"0802", ‘0810‘=>"0810", ‘0820‘=>"0820", ‘0830‘=>"0830", ‘0840‘=>"0840", ‘0850‘=>"0850", ‘0860‘=>"0860", ‘0870‘=>"0870", ‘0880‘=>"0880", ‘0900‘=>"0900", ‘0901‘=>"0901", ‘0902‘=>"0902", ‘0903‘=>"0903", ‘0904‘=>"0904", ‘0905‘=>"0905", ‘0906‘=>"0906", ‘0907‘=>"0907", ‘0908‘=>"0908", ‘0909‘=>"0909", ‘0910‘=>"0910", ‘0911‘=>"0911", ‘0912‘=>"0912", ‘0913‘=>"0913", ‘0914‘=>"0914", ‘0915‘=>"0915", ‘0916‘=>"0916", ‘0917‘=>"0917", ‘0918‘=>"0918", ‘0919‘=>"0919", ‘0920‘=>"0920", ‘0921‘=>"0921", ‘0922‘=>"0922", ‘0923‘=>"0923", ‘0924‘=>"0924", ‘0925‘=>"0925", ‘0926‘=>"0926", ‘0927‘=>"0927", ‘0928‘=>"0928", ‘0929‘=>"0929", ‘0930‘=>"0930", ‘0940‘=>"0940", ‘0950‘=>"0950", ‘0960‘=>"0960", ‘0970‘=>"0970", ‘0980‘=>"0980", ‘0990‘=>"0990" );
    $address_fields['postcode']['type'] = 'select';     
    $address_fields['postcode']['options'] = $postcode_array;      
    return $address_fields; 
}

效果很好。

但是当我使用基于运费的插件时,当城市与邮政编码不匹配时,该插件会显示错误的运费。

因此,如果可能,我将需要在特定的“城市”字段(用户输入MAKATI)中仅在此下拉列表中显示相关的“ Makati”邮政编码。

感谢您的帮助。

更新:以下是MAKATI的邮政编码:

$makati_postcodes = array(‘1200‘=>"1200", ‘1201‘=>"1201", ‘1202‘=>"1202", ‘1203‘=>"1203", ‘1204‘=>"1204", ‘1205‘=>"1205", ‘1206‘=>"1206", ‘1207‘=>"1207", ‘1208‘=>"1208", ‘1209‘=>"1209", ‘1210‘=>"1210", ‘1211‘=>"1211", ‘1212‘=>"1212", ‘1213‘=>"1213", ‘1214‘=>"1214", ‘1215‘=>"1215", ‘1216‘=>"1216", ‘1217‘=>"1217", ‘1218‘=>"1218", ‘1219‘=>"1219", ‘1220‘=>"1220", ‘1221‘=>"1221", ‘1222‘=>"1222", ‘1223‘=>"1223", ‘1224‘=>"1224", ‘1225‘=>"1225", ‘1226‘=>"1226", ‘1227‘=>"1227", ‘1228‘=>"1228", ‘1229‘=>"1229", ‘1230‘=>"1230", ‘1231‘=>"1231", ‘1232‘=>"1232", ‘1233‘=>"1233", ‘1234‘=>"1234", ‘1235‘=>"1235", ‘1250‘=>"1250", ‘1251‘=>"1251", ‘1252‘=>"1252", ‘1253‘=>"1253", ‘1254‘=>"1254", ‘1255‘=>"1255", ‘1256‘=>"1256", ‘1257‘=>"1257", ‘1258‘=>"1258", ‘1259‘=>"1259", ‘1260‘=>"1260", ‘1261‘=>"1261", ‘1262‘=>"1262", ‘1263‘=>"1263", ‘1264‘=>"1264", ‘1265‘=>"1265", ‘1266‘=>"1266", ‘1267‘=>"1267", ‘1268‘=>"1268", ‘1269‘=>"1269", ‘1270‘=>"1270", ‘1271‘=>"1271", ‘1272‘=>"1272", ‘1273‘=>"1273", ‘1274‘=>"1274", ‘1275‘=>"1275", ‘1276‘=>"1276", ‘1277‘=>"1277", ‘1278‘=>"1278", ‘1279‘=>"1279", ‘1280‘=>"1280", ‘1281‘=>"1281", ‘1282‘=>"1282", ‘1283‘=>"1283", ‘1284‘=>"1284", ‘1285‘=>"1285", ‘1286‘=>"1286", ‘1287‘=>"1287", ‘1288‘=>"1288", ‘1289‘=>"1289", ‘1290‘=>"1290", ‘1291‘=>"1291", ‘1292‘=>"1292", ‘1293‘=>"1293", ‘1294‘=>"1294", ‘1295‘=>"1295", ‘1296‘=>"1296", ‘1297‘=>"1297", ‘1298‘=>"1298", ‘1299‘=>"1299");

1 个答案:

答案 0 :(得分:0)

为此您需要JavaScript并使其更易于阅读和编辑,我将邮政编码减少到了两个半角,当然您可以稍后将它们添加到JavaScript对象和您的函数中:

您的主要功能:

add_filter('woocommerce_default_address_fields', 'custom_override_default_postcode_field');
function custom_override_default_postcode_field($address_fields)
{
    // Your postcodes array
    $postcode_array = array("1300" => "1300", "1301" => "1301");
    $address_fields['postcode']['type'] = 'select';
    $address_fields['postcode']['options'] = $postcode_array;
    return $address_fields;
}

只需添加以下功能,即可完成此工作:

add_action('wp_footer', 'dynamic_postcode');

function dynamic_postcode()
{
    //// Only on checkout page
    if (!(is_checkout() && !is_wc_endpoint_url())) {
        return;
    }

    ?>
    <script>
    jQuery(document).ready(function ($) {
'use strict';
//Hide post code if the city is empty to avoid confusion
$(function () {
    if ($('#billing_city').val() == '' || $('#billing_city').val() == ' ') {
    $('label[for=billing_postcode], select#billing_postcode').hide();

    }
    //check if the billing city is makati put the new value els put the other value and of course display the post code
    $('#billing_city').change(function () {

    if ($(this).val().toLowerCase() == 'makati') {

        $('#billing_postcode')
        .find('option')
        .remove()
        .end()
        var makati_postcodes = { //You can add as many object as you want here 
        1200: '1200',
        1201: '1201',
        };

        $.each(makati_postcodes, function (key, value) {
        $('#billing_postcode')
            .append('<option value=' + key + '>' + value + '</option>')

        });
        $('label[for=billing_postcode], select#billing_postcode').show();
    } else {
    $('#billing_postcode')
        .find('option')
        .remove()
        .end()
        var other_postcodes = { //You can add as many object as you want here 
        1300: '1300',
        1301: '1301',
        };
        $.each(other_postcodes, function (key, value) {
        $('#billing_postcode')
            .append('<option value=' + key + '>' + value + '</option>')

        });
        $('label[for=billing_postcode], select#billing_postcode').show();
    }
    });

});

    });
</script> ;

<?php
}

测试并运行

相关问题