Change a checkout billing field placeholder in Woocommerce
Asked Answered
L

1

5

I'm trying to change the fields placeholder of "billing address 2" in the checkout page, using the following code:

add_filter( 'woocommerce_checkout_fields' ,  'custom_override_checkout_fields',9999 );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
   $fields['billing']['billing_address_2']['placeholder']="dssfsd";
   return $fields;
}

it changes only for a moment than return back to it's defualt value. see the following video (10s): https://www.youtube.com/watch?v=-qOZ67gFQ98

Lepine answered 3/1, 2018 at 15:39 Comment(0)
G
13

The only way to get this is using woocommerce_default_address_fields, but it will change both billing and shipping placeholder for Address 2 checkout fields:

add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
    $address_fields['address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );

    return $address_fields;
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works


Now if you want to change only the Address 2 billing field placeholder you will:

  1. Unset default Address 2 field placeholder (for both shipping and billing fields)
  2. Customize only billing Address 2 field placeholder
  3. Put back shipping Address 2 field placeholder

Here is the code on two hooked function:

add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
    // Remove labels for "address 2" shipping fields
    unset($address_fields['address_2']['placeholder']);

    return $address_fields;
}

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields', 90, 1 );
function custom_override_checkout_fields( $fields ) {
    // Add custom billing "address 2" label
    $fields['billing']['billing_address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
    // Put back shipping "address 2" label
    $fields['shipping']['shipping_address_2']['placeholder'] = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );

    return $fields;
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works

Globuliferous answered 3/1, 2018 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.