【问题标题】:Customizing placehorlder in billing/shipping forms在帐单/运输表单中自定义占位符
【发布时间】:2016-10-02 18:18:38
【问题描述】:

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

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

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

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

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce placeholder account


    【解决方案1】:

    如果您的意思是在我的帐户页面中的账单和运输表单,唯一可以解决问题的过滤器挂钩(据我所知)是:

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

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


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

    $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'           => '',
    );
    

    注意:Stone 进行了一些更改,它正在工作。 (见评论)

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

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

    帐单和送货地址的占位符通常相同......所以您的代码将如下所示:

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

    【讨论】:

    • 谢谢! @LoicTheAztec 你的解决方案很完美,我使用像这样的“id”字段if ($args['id'] == 'billing_first_name') $args['placeholder'] = 'Name';
    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2018-06-13
    • 2018-03-01
    • 2013-12-12
    • 2021-01-12
    • 2017-01-01
    相关资源
    最近更新 更多