Customizing placehorlder in billing/shipping forms

☆樱花仙子☆ 提交于 2019-12-04 15:25:26
LoicTheAztec

If you mean in My account pages for Billing and Shipping form, the only filter hook (that I know) that could do the trick is:

add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) { 
    // your code 
    return $args;
};

It is located in wc-template-functions.php on line 1734, triggered by woocommerce_form_field() function in woocommerce templates under my_account subfolder, form-edit-address.php file (displaying the form fields) .


This are the default $args you can use to target the changes you want to do inside your filter function:

$defaults = array(
    'type'              => 'text',
    'label'             => '',
    'description'       => '',
    'placeholder'       => '',
    'maxlength'         => false,
    'required'          => false,
    'autocomplete'      => false,
    'id'                => $key,
    'class'             => array(),
    'label_class'       => array(),
    'input_class'       => array(),
    'return'            => false,
    'options'           => array(),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);

NOTE: With some changes made by Stone it's working. (see the comment).

USE:
You could use 'placeholder' this way to target each placeholder you need to change:

if ( $args['id'] == 'your_slug1' ) {
    $args['placeholder'] = 'your_new_placeholder1';
} elseif ( $args['id'] == 'your_slug2' ) {
    $args['placeholder'] = 'your_new_placeholder2';
} // elseif … and go on

The placeholders are generally the same for billing and shipping address… So your code will be like:

add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) { 
    if ( $args['id'] == 'your_slug1' ) {
        $args['placeholder'] = 'your_new_placeholder1';
    } elseif ( $args['id'] == 'your_slug2' ) {
        $args['placeholder'] = 'your_new_placeholder2';
    } // elseif … and go on 

    return $args;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!