微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在WooCommerce中设置结帐邮政编码字段的最大长度

如何解决在WooCommerce中设置结帐邮政编码字段的最大长度

我正在尝试删除增加使用Woocommerce开发的WordPress网站上的认最大长度(如今设置为4)。

到目前为止,我已经在孩子主题functions.PHP

上尝试了所有这些功能

#OPTION 1

function wpse215677_checkout_fields ( $fields ) {
    $fields['postcode']['maxlength'] = 5;
    return $fields;
}
add_filter('woocommerce_default_address_fields','wpse215677_checkout_fields');

#OPTION 2-有和无['custom_attributes']

add_filter( 'woocommerce_checkout_fields','my_override_checkout_fields');

function my_override_checkout_fields( $fields ) {
    $fields['billing']['postcode']['custom_attributes']['maxlength'] = 5;
     $fields['billing']['billing_postcode']['custom_attributes']['maxlength'] = 5;
     return $fields;
}

#OPTION 3-有和无['custom_attributes']

function my_wc_custom_billing_fields( $fields ) {
    $fields['billing_postcode']['custom_attributes']['maxlength'] = 5;

    return $fields;
}

add_filter( 'woocommerce_billing_fields','my_wc_custom_billing_fields' );

function my_wc_custom_shipping_fields( $fields ) {
    $fields['shipping_postcode']['custom_attributes']['maxlength'] = 5;

    return $fields;
}

add_filter( 'woocommerce_shipping_fields','my_wc_custom_shipping_fields' );

所有这些都可以在购物车计算器上正常工作(现在我可以写4以上的任何数字),但是当我转到结帐页面并尝试在邮政编码(运输或帐单)上写4个以上字符的数字时,输入仍然保持最大长度为4(我已经使用Chrome的工具对其进行了检查)。

有什么方法可以覆盖结帐时的整个输入,以使我可以在此输入上写入4个以上的字符?

或者我在这功能上做错了什么,这就是为什么它们在结帐页面上不起作用的原因?

解决方法

一种方法是通过jQuery进行操作,尽管您已经在此处发布的代码可以正常使用

function action_wp_footer() {
    // Returns true on the checkout page,when false,return
    if ( ! is_checkout() ) return;
    ?>
    <script>
        jQuery(document).ready(function($){
            $( '#billing_postcode' ).attr( 'maxlength','5' );
            $( '#shipping_postcode' ).attr( 'maxlength','5' );
        });
    </script>
    <?php
}
add_action( 'wp_footer','action_wp_footer' );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。