Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce - Set Maxlength to checkout field WordPress

  • SOLVED

Hi

My logistics company can't handle more than a certain amount of characters for each field. For example 50 for address.

So I simply want to put a maxlength="X" to different checkout field.

Example:

<input type="text" class="input-text " name="billing_address_1" id="billing_address_1" placeholder="Street address" value="">

Should be

<input type="text" class="input-text " name="billing_address_1" id="billing_address_1" placeholder="Street address" value="" maxlength="50">

Preferably I want a function for this.. or tips on how to edit this in good way =)

Answers (3)

2015-03-10

Francisco Javier Carazo Gil answers:

You can do it using jQuery. I send you a code for this now.


Francisco Javier Carazo Gil comments:

In your functions.php include:


add_action("wp_footer", "cod_set_max_length");

function cod_set_max_length(){
?>
<script>
jQuery(document).ready(function($){
$("#billing_address_1").attr('maxlength','20');
});
</script>
<?php
}


Francisco Javier Carazo Gil comments:

You can improve it:


add_action("wp_footer", "cod_set_max_length");


function cod_set_max_length(){
if( !is_checkout())
return;
?>

<script>

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

$("#billing_address_1").attr('maxlength','20');
$("#billing_address_2").attr('maxlength','20');
// more fields

});

</script>

<?php

}


If you have any doubt tell me.

2015-03-10

Arnav Joy answers:

you can also try following


add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );


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

2015-03-10

Reigel Gallarde answers:

you can do it like this..

add_filter('woocommerce_default_address_fields','woocommerce_default_address_fields');

function woocommerce_default_address_fields($fields){
$fields['address_1']['custom_attributes'] = array(
'maxlength' => 50
);
return $fields;
}


take note, the text billing was cut off... so instead of billing_address_1, use address_1...

just add more fields like this...

add_filter('woocommerce_default_address_fields','woocommerce_default_address_fields');

function woocommerce_default_address_fields($fields){
$fields['address_1']['custom_attributes'] = array(
'maxlength' => 50
);
$fields['address_2']['custom_attributes'] = array(
'maxlength' => 50
);
return $fields;
}


let me know if this works...