admin管理员组

文章数量:1335386

I've been struggling with this for a few days now. What i want to do, it to limit the Postcode/zip fields to 4 numbers. So, to add a maxlength and a type="number" to those fields.

Things i've tried so far:

1 - (functions.php)

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    $fields['billing']['postcode']['maxlength'] = 4;    
    return $fields;
}

2 - (functions.php) a jQuery way

add_action("wp_footer", "cod_set_max_length");

function cod_set_max_length(){

    ?>
    <script>

    jQuery(document).ready(function($){

        $("#postcode").attr('maxlength','4');

    });

    </script>

<?php

I've been struggling with this for a few days now. What i want to do, it to limit the Postcode/zip fields to 4 numbers. So, to add a maxlength and a type="number" to those fields.

Things i've tried so far:

1 - (functions.php)

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    $fields['billing']['postcode']['maxlength'] = 4;    
    return $fields;
}

2 - (functions.php) a jQuery way

add_action("wp_footer", "cod_set_max_length");

function cod_set_max_length(){

    ?>
    <script>

    jQuery(document).ready(function($){

        $("#postcode").attr('maxlength','4');

    });

    </script>

<?php
Share Improve this question edited Jan 31, 2016 at 7:26 certainstrings 1184 bronze badges asked Jan 26, 2016 at 9:31 Menno van der KriftMenno van der Krift 2911 gold badge5 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

By the time woocommerce_checkout_fields filter is run, the arrays for billing and shipping have already been created.

You can see that here:
https://docs.woothemes/wc-apidocs/source-class-WC_Checkout.html#101

If you want to effect the default fields, use this hook.

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

If you want to adjust just the default billing fields:

function wpse215677_checkout_fields ( $fields ) {
    $fields['billing_postcode']['maxlength'] = 4;
    return $fields;
}
add_filter('woocommerce_billing_fields', 'wpse215677_checkout_fields');

Otherwise you just need to edit your array declaration:

$fields['billing_postcode']['maxlength'] = 4;

You may add these functions in the functions.php of your theme:

function custom_override_checkout_fields2( $fields ) {
$fields['shipping_postcode']['maxlength'] = 5;    
return $fields;
}
add_filter( 'woocommerce_shipping_fields' , 'custom_override_checkout_fields2' );

function custom_override_checkout_fields( $fields ) {
    $fields['billing_postcode']['maxlength'] = 5;    
    return $fields;
}
add_filter( 'woocommerce_billing_fields' , 'custom_override_checkout_fields' );

本文标签: functionsWoocommerce checkout field maxlengthmake input number field only (postcode)