【问题标题】:How do I add HTML to the "billing_email" field at WooCommerce checkout?如何在 WooCommerce 结帐时将 HTML 添加到“billing_email”字段?
【发布时间】:2021-04-22 09:42:48
【问题描述】:

我想在billing_email 字段输入框下方添加一行文本。详情如下。

原始 HTML 输出。

<p class="form-row form-row-wide validate-required validate-email" id="billing_email_field" data-priority="110">
    <label for="billing_email" class="">Email address&nbsp;<abbr class="required" title="required">*</abbr></label>
    <span class="woocommerce-input-wrapper">
        <input type="email" class="input-text " name="billing_email" id="billing_email" placeholder="" value="" autocomplete="email username">
    </span>
</p>

修改后的 HTML 输出。

<p class="form-row form-row-wide validate-required validate-email" id="billing_email_field" data-priority="110">
    <label for="billing_email" class="">Email address&nbsp;<abbr class="required" title="required">*</abbr></label>
    <span class="woocommerce-input-wrapper">
        <input type="email" class="input-text " name="billing_email" id="billing_email" placeholder="" value="" autocomplete="email username">
    </span>
    <br>
    <span>Please enter the correct email address so that you can receive our emails.</span>
</p>

我尝试查看WooCommerce help documentation - Customizing checkout fields using actions and filters,但对我来说太难了,所以我在这里寻求帮助。

任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress woocommerce checkout


    【解决方案1】:

    要专门针对billing_email 字段进行更改,您可以使用'woocommerce_form_field_' . $args['type'], 过滤器挂钩。其中$args['type']可以替换为email类型

    $field 包含从&lt;p&gt; 到结尾&lt;/p&gt; 的所有HTML。但是,因为要在现有代码之间添加新的 HTML,而不是重写整个字段,可以使用str_replace

    所以你得到:

    function filter_woocommerce_form_field_email( $field, $key, $args, $value ) {   
        // Billing email
        if ( $key === 'billing_email') {
            // Replace existing html with new ones
            $field = str_replace( '</span>', '</span><br><span>Please enter the correct email address so that you can receive our emails.</span>', $field );
        }
        
        return $field;
    } 
    add_filter( 'woocommerce_form_field_email', 'filter_woocommerce_form_field_email', 10, 4 );
    

    相关:

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多