【问题标题】:Set maxlength for checkout postcode field in WooCommerce在 WooCommerce 中为结帐邮政编码字段设置 maxlength
【发布时间】:2020-10-10 15:49:33
【问题描述】:

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

到目前为止,我已经在我的 childtheme 的 functions.php 上尝试了所有这些功能

#选项 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 个字符?

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

【问题讨论】:

    标签: php wordpress woocommerce checkout maxlength


    【解决方案1】:

    一种方法是通过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' );
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2021-11-08
      • 2018-05-31
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多