In WooCommerce checkout fields, I'm trying to make billing_address_2 above billing_address_1 on the checkout form.
So instead of it being:
Street Address
Apartment
I would like to have:
Apartment
Street Address
Please also note, I'm using the Theme called Avada.
How can I achieve this?
Thanks.
Update (related to your comment)…
Here you have an addition that removes "Address" text label from Address1 field and set it to Address2 field, making also that field (optionally) required and changing a little bit the place holder… I have also another solution (see below after the code).
Here is the code:
add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_order' );
function custom_billing_fields_order( $fields ) {
// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['label'] = ''; // Removing the label from Adress1
$fields['billing']['billing_address_2']['label'] = __('Address', 'theme_domain');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
// 2. Custom ordering the billing fields array (toggling address_1 with address_2)
$custom_fields_order = array(
'billing_first_name', 'billing_last_name',
'billing_company',
'billing_email', 'billing_phone',
'billing_country',
'billing_address_2', 'billing_address_1', ## <== HERE, changed order
'billing_postcode', 'billing_city'
);
foreach($custom_fields_order as $field)
$new_ordered_fields[$field] = $fields['billing'][$field];
// Replacing original fields order by the custom one
$fields['billing'] = $new_ordered_fields;
// Returning Checkout customized billing fields order
return $fields;
}
Instead of toggling that 2 fields, you could invert the fields placeholders and add make (optionally) required the field Address2, so you will not need to reorder the fields. You can do it this way:
add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_placeholders' );
function custom_billing_fields_placeholders( $fields ) {
// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Street address', 'woocommerce');
// Returning Checkout customized billing fields
return $fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Related answers: Checkout fields: Hiding and showing existing fields
Official documentation: Customizing checkout fields using actions and filters
Related
How do I clear a specfic checkout fields in WooCommerce after placing an order?
For example Billing email and Billing Phone. So that when the registered customer makes his/her next order, he/she has to fill that fields again?
I see this code, but this clean all the fields, i need only a specific field. Any advice?
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
The woocommerce_checkout_get_value hook has 2 arguments:
$value argument that is returned
$input argument to target checkout field(s)
So in your case, you get:
function filter_woocommerce_checkout_get_value( $value, $input ) {
// Target checkout fields. Multiple fields can be added, separated by a comma
if ( in_array( $input, array( 'billing_phone', 'billing_email' ) ) ) {
$value = '';
}
return $value;
}
add_filter( 'woocommerce_checkout_get_value' , 'filter_woocommerce_checkout_get_value' , 10, 2 );
You could probably achieve this with some custom javascript:
document.getElementById('fieldID').value = ''
Or jQuery:
$('#fieldID').val('');
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
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
I can unset fields from billing and shipping but why I can't unset fields from additional field section. I am adding a condition on these fields. Maybe I am using wrong meta keys.
function wc_remove_checkout_field( $fields ) {
unset( $fields['billing']['test_field'] ); //this one working
unset( $fields['additional']['delivery_time'] ); //this one not even if I replace additional with order.
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_remove_checkout_field' );
Creating fields usings this plugin WooCommerce Checkout Field Editor
You can use this filter to remove the order notes field:
add_filter('woocommerce_enable_order_notes_field', '__return_false');
If you want to stick with your method using unset, you need to replace "additional" with "order":
unset($fields['order']['order_comments']);
On Checkout page, I would like to show the comment field, only if a coupon code is applied. In this case this comment field should be a required field.
The example below works except for the required status to optional.
I made the comments required as a default and then I assumed that after unsetting them the required status would be ignored.
This is the snippet that makes the comments required:
$fields['order']['order_comments']['required'] = true;
This snippet looks for a coupon code and then shows a message. I dont need the message so I left that blank, and then I added the lines that hides the comments:
add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );
function product_checkout_custom_content() {
global $woocommerce;
$msgs = array('mycouponcode'=>'');
$applied_coupon = $woocommerce->cart->applied_coupons;
if( ! array_key_exists($applied_coupon[0], $msgs) ) {
// Hides the order comments
unset( $fields['order']['order_comments'] );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Here I need to make the order_comments optional, not required
// echo $msgs[$applied_coupon[0]];
}
}
How can I make the order comments optional within the same action?
To make that work, you don't need function product_checkout_custom_content(). Instead you have to make some change in the function where is included $fields['order']['order_comments']['required'] = true;.
I suppose that is a function hooked in woocommerce_checkout_fields. So in that function you will have to replace $fields['order']['order_comments']['required'] = true;, by the code inside the function:
// CHECKOUT PAGE - CUSTOMIZING comment field (conditional behavior).
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
//Set your coupon slug here:
$coupon = 'coupon_slug';
// Coupon is applied: Changing Comment field Label, placeholder and setting "REQUIRED"
if ( in_array( '$coupon, WC()->cart->applied_coupons ) ){
$fields['order']['order_comments']['label'] = __('Your comment label…', 'my_theme_slug');
$fields['order']['order_comments']['placeholder'] = __('Enter here something', 'my_theme_slug');
$fields['order']['order_comments']['required'] = true;
} else {
// Removes the comment field + block title
unset($fields['order']['order_comments']);
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
return $fields;
}
You don't need anything else…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Reference: Remove the Additional Information and Order Notes fields in WooCommerce