Change a checkout billing field placeholder in Woocommerce - php

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

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:
Unset default Address 2 field placeholder (for both shipping and billing fields)
Customize only billing Address 2 field placeholder
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

Related

Change Woocommerce checkout "Order Comments" to a text input field with a defined maxlength

I would like to change the "Order Comments" field in Woocommerce from a textbox to an input box with maximum characters of 18. Plus change the name to "Order Instructions"
I have found a way to edit the field in functions.php using the following code:
// remove Order Notes from checkout field in Woocommerce
add_filter( 'woocommerce_checkout_fields' , 'alter_woocommerce_checkout_fields' );
function alter_woocommerce_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
Can this be done with a filter in functions.php?
Updated: You can use the following for "Order Comments" checkout field, to:
Change the field type to an input text
Set a max length
Change the label to "Order Instructions"
Here is the code
// Change Order Notes type to 'text' on Woocommerce checkout
add_filter( 'woocommerce_checkout_fields' , 'alter_woocommerce_checkout_fields' );
function alter_woocommerce_checkout_fields( $fields ) {
// Change field type
$fields['order']['order_comments']['type'] = 'text';
// Limit to a max length
$fields['order']['order_comments']['custom_attributes'] = array('maxlength' => 18);
// Change the label name
$fields['order']['order_comments']['label'] = __('Order Instructions', "woocommerce");
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Display custom checkout address field 2 label in Woocommerce

I found a way to change the placeholder but not the label of the checkout input fields in woocommerce.
I would like to change the label for the adress_field_2.
Here is my attempt that did not change anything. I tried [label] and [label_class] but that did not work...
add_filter( 'woocommerce_default_address_fields', 'new_checkout_field_label', 10, 1 );
function new_checkout_field_label( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( '', 'woocommerce' );
$address_fields['address_2']['label'] = __( 'Apt, Unit, Etc (optional)', 'woocommerce' );
return $address_fields;
}
Your code works and the correct related html is there. But it's hidden from the tag class screen-reader-text by a CSS rule.
To make the <label> visible, you need to remove the class from the <label> tag adding this line:
$address_fields['address_2']['label_class'] = array(); // No label class
So in your code:
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'] = __( '', 'woocommerce' );
$address_fields['address_2']['label'] = __( 'Apt, Unit, Etc (optional)', 'woocommerce' );
$address_fields['address_2']['label_class'] = array(); // No label class
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Make state checkout field required for a Vietnam country in WooCommerce

The country of Vietnam in WooCommerce does not have defined states, so I
added some states to my checkout page.
This is my code:
add_filter( 'woocommerce_states', 'vietnam_cities_woocommerce' );
function vietnam_cities_woocommerce( $states ) {
$states['VN'] = array(
'HCM' => __('Hồ Chí Minh', 'woocommerce') ,
'HANOI' => __('Hà Nội', 'woocommerce') ,
'HAIPHONG' => __('Hải Phòng', 'woocommerce') ,
);
return $states;
}
It do work as I would like, but it is an optional field for Vietnam.
How to make this state field as required for Vietnam?
Any help is appreciated.
The following function will make for Vietnam the state field as a required in woocommerce:
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale', 10, 1 );
function custom_default_address_fields( $locale ) {
$locale['VN']['state']['required'] = true;
return $locale;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
Explanations: Each country have specific "locale" fields settings in WooCommerce. We are adding/altering the default WooCommerce country locale settings that are defined on WC_Countries get_country_locale() method

Remove postcode from Woocommerce cart shipping calculator

In Woocommerce, I have been able to remove postcode checkout field using this code:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
unset($address_fields['postcode']);
return $address_fields;
}
How can I remove the postcode field from the shipping calculator in cart page?
This can be done adding the following:
add_filter( 'woocommerce_shipping_calculator_enable_postcode', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). tested and works.
add_filter( 'woocommerce_default_address_fields', 'custom_override_address_fields', 999, 1 );
function custom_override_address_fields( $address_fields ) {
// set as not required
$address_fields['postcode']['required'] = false;
// remove validation
unset( $address_fields['postcode']['validate'] );
return $address_fields;
}
(this is the real code that devalidates the postcode)

Remove phone from billing/shipping fields everywhere in Woocommerce?

I don't need to collect customer phone numbers in woocommerce so I've used the following code to remove them from the checkout process:
add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['shipping']['shipping_phone']);
$fields['billing']['billing_email']['class'] = array('form-row-wide');
return $fields;}
(sorry for hideous code pasting, first time!)
That works great on the checkout page but once in the "my account" area, the user is still required to add a phone number if they edit their billing or shipping address. I have added this code:
function pbj_remove_billingphone($fields) {
unset( $fields ['billing_phone'] );
return $fields;}
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billingphone' );
which has removed it from billing, but I can't get anything to work to remove it from shipping. Any help on either something that universally removes the phone number requirement everywhere, or tips on what to filter/unset to remove the shipping phone number on the account page? Thank you!
Here is the complete way to do it in account and checkout pages (for both pages):
// Remove billing phone (and set email field class to wide)
add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, 1 );
function remove_billing_phone_field($fields) {
$fields ['billing_phone']['required'] = false; // To be sure "NOT required"
$fields['billing_email']['class'] = array('form-row-wide'); // Make the field wide
unset( $fields ['billing_phone'] ); // Remove billing phone field
return $fields;
}
// Remove shipping phone (optional)
add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 );
function remove_shipping_phone_field($fields) {
$fields ['shipping_phone']['required'] = false; // To be sure "NOT required"
unset( $fields ['shipping_phone'] ); // Remove shipping phone field
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Normally shipping email and phone fields doesn't exist by default in WooCommmerce

Categories