简体   繁体   中英

How do I add HTML to the "billing_email" field at WooCommerce checkout?

I would like to add a line of text below the billing_email field input box. The details are as follows.

Original HTML output.

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

Modified HTML output.

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

I tried to check the WooCommerce help documentation - Customizing checkout fields using actions and filters , but it was too difficult for me, so I am asking for help here.

Any help would be appreciated!

To make a change specifically and only for the billing_email field, you can use the 'woocommerce_form_field_'. $args['type'], 'woocommerce_form_field_'. $args['type'], filter hook. Where $args['type'] can be replaced with type email

$field contains all HTML from <p> to the closing </p> . However, because you want to add new HTML between the existing code, instead of rewriting the entire field, you can use str_replace

So you get:

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

Related:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM