Keilor Brown
Keilor Brown

Reputation: 51

WooCommerce Checkout Field html customization issue

In Woocommerce, I'm adding a footnote to a specific checkout field using this code:

add_action( 'woocommerce_form_field_text','add_address_disclaimer', 100, 2 );
function add_address_disclaimer( $field, $key ){
    global $woocommerce;    
    if ( is_checkout() && ( $key == 'billing_address_2') ) {
        $field .= '<div class="fields-shipping-disclaimer"><p>' . __('We cannot ship to PO Boxes and FPO/APO addresses.') . '</p></div>';
    }
    return $field;
}

It works as it should on init... the footnote displays under the billing address 2 field:

enter image description here

However, on updating any fields that triggers the "update checkout" option, the custom footnote is not reflected in the refreshed checkout fields:

enter image description here

Anyone have a clue as to why this is happening?

Upvotes: 2

Views: 774

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253773

First the hook you are using is a filter hook not an action hook, so should use add_filter() and global $woocommerce; is not needed.

Try this code version (without a div tag):

add_filter( 'woocommerce_form_field_text' , 'add_address_disclaimer', 10, 2 );
function add_address_disclaimer( $field, $key) {
    if ( is_checkout() && $key == 'billing_address_2' ) {
        $text   = __("We cannot ship to PO Boxes and FPO/APO addresses.");
        $field .= '<p class="form-row fields-shipping-disclaimer">'.$text.'</p>';
    }
    return $field;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work (stays on update_checkout JS event).

Related thread: WooCommerce add customs notice below checkout email field

Upvotes: 2

Related Questions