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
2 Answers
Reset to default 2By 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)
版权声明:本文标题:functions - Woocommerce checkout field maxlength, make input number field only (postcode) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372247a2462443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论