在账单/运输表格中自定义占位符 [英] Customizing placehorlder in billing/shipping forms

查看:43
本文介绍了在账单/运输表格中自定义占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 woocommerce_checkout_fields 过滤器来自定义 WooCommerce 结帐页面中的占位符.

 add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );函数 custom_override_checkout_fields( $fields ) {$fields['billing']['billing_first_name']['placeholder'] = '姓名';$fields['billing']['billing_last_name']['placeholder'] = 'Surname';$fields['billing']['billing_email']['placeholder'] = 'Email';$fields['billing']['billing_company']['placeholder'] = '公司名称';$fields['billing']['billing_phone']['placeholder'] = 'Phone';$fields['billing']['billing_postcode']['placeholder'] = '邮政编码';$fields['billing']['billing_city']['placeholder'] = 'City';$fields['shipping']['shipping_first_name']['placeholder'] = 'Name';$fields['shipping']['shipping_last_name']['placeholder'] = 'Surname';$fields['shipping']['shipping_city']['placeholder'] = 'City';$fields['shipping']['shipping_postcode']['placeholder'] = '邮政编码';返回 $fields;}

这仅适用于结帐页面表单.

如何在所有运输和账单表格中自定义占位符"?

谢谢.

解决方案

如果您的意思是在 我的帐户 页面中的 Billing and Shipping 表单,那么唯一可以执行以下操作的过滤器钩子(据我所知)技巧是:

add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );函数 custom_form_field_args( $args, $key, $value ) {//你的代码返回 $args;};

它位于 wc-template-1734 行 上的functions.php,由my_account 子文件夹下的woocommerce 模板中的woocommerce_form_field() 函数触发,form-edit-address.php 文件(显示表单域).

<小时>

这是默认的 $args,您可以使用它来定位您要在过滤器函数中进行的更改:

$defaults = array('类型' =>'文本','标签' =>'','说明' =>'','占位符' =>'','最大长度' =>错误的,'必需' =>错误的,'自动完成' =>错误的,'id' =>$key,'类' =>大批(),'label_class' =>大批(),'input_class' =>大批(),'返回' =>错误的,'选项' =>大批(),'custom_attributes' =>大批(),'验证' =>大批(),'默认' =>'',);

<块引用>

注意:经过 Stone 的一些更改后,它可以正常工作了. (请参阅评论).

使用:
您可以通过这种方式使用占位符"来定位您需要更改的每个占位符:

if ( $args['id'] == 'your_slug1' ) {$args['placeholder'] = 'your_new_placeholder1';} elseif ( $args['id'] == 'your_slug2' ) {$args['placeholder'] = 'your_new_placeholder2';}//elseif ... 继续

帐单地址和送货地址的占位符通常相同……因此您的代码将类似于:

add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );函数 custom_form_field_args( $args, $key, $value ) {如果 ( $args['id'] == 'your_slug1' ) {$args['placeholder'] = 'your_new_placeholder1';} elseif ( $args['id'] == 'your_slug2' ) {$args['placeholder'] = 'your_new_placeholder2';}//elseif ... 继续返回 $args;};

I am using woocommerce_checkout_fields filter for customizing the placeholders in WooCommerce checkout page.

  add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_first_name']['placeholder'] = 'Name';
    $fields['billing']['billing_last_name']['placeholder'] = 'Surname';
    $fields['billing']['billing_email']['placeholder'] = 'Email';
    $fields['billing']['billing_company']['placeholder'] = 'Company Name';
    $fields['billing']['billing_phone']['placeholder'] = 'Phone';
    $fields['billing']['billing_postcode']['placeholder'] = 'Zip Code';
    $fields['billing']['billing_city']['placeholder'] = 'City';

    $fields['shipping']['shipping_first_name']['placeholder'] = 'Name';
    $fields['shipping']['shipping_last_name']['placeholder'] = 'Surname';
    $fields['shipping']['shipping_city']['placeholder'] = 'City';
    $fields['shipping']['shipping_postcode']['placeholder'] = 'Zip Code';
    return $fields;
  }

This work fine only for checkout page form.

How to customize 'placeholders' in all shipping and billing forms?

Thanks.

解决方案

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;
};

这篇关于在账单/运输表格中自定义占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆