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
Related
I am trying to tackle an issue by renaming the default message below when there are no shipping methods available, either set by rule based plugins or there simply isn't any methods set in WooCommerce.
No shipping method has been selected. Please double check your address, or contact us if you need any help.
I have used the below function using the snippets plugin and I can confirm it changes the message in the cart and checkout underneath the shipping label.
However, if someone then attempts to checkout they get a red WooCommerce error message at the top of the screen which is still the default message above.
How do I also change this error message ? I would like to change it to something different than what my code below shows to give me the flexibility to put a short message in the cart totals box and a longer more informational message as to why no methods are available.
My Function:
add_filter( 'woocommerce_cart_no_shipping_available_html', 'no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'no_shipping_available_html' );
function no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.', $all_countries[ $country ] );
}
return 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.';
}
This text is located in WC_Checkout Class inside validate_checkout() method. There are no filters to make changes to it, but you can use WordPress gettext filter for that as follows:
add_filter( 'gettext', 'change_checkout_no_shipping_method_text', 10, 3 );
function change_checkout_no_shipping_method_text( $translated_text, $text, $domain ) {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
$original_text = 'No shipping method has been selected. Please double check your address, or contact us if you need any help.';
$new_text = 'Here your own text replacement.';
if ( $text === $original_text ) {
$translated_text = $new_text;
}
}
return $translated_text;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You could use a plugin like Loco translate if you want, that makes easier to do your job.. but you could try this code, similar to #LoicTheAztec inside functions.php (either on your theme or child theme file)
add_filter( 'gettext', 'ds_translate_woocommerce_strings', 999, 3 );
function ds_translate_woocommerce_strings( $translated,
$untranslated, $domain ) {
if ( ! is_admin() ) {
switch ($translated) {
case 'the message you want to be translated exactly as you see it in WP':
$translated = 'your new message';
break;
}
}
return $translated;
}
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
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
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
Is there a way to remove the required asterisk from a billing field on the checkout page in php? I have the following code which isn't working.
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
$fields['billing']['billing_address_2']['required'] = false;
return $fields;
}
It would probably be easier to do with CSS, and be cleaner and better for screen-readers:
.woocommerce-checkout abbr.required {
display: none;
}
The .woocommerce-checkout is a body class that is only appended to the checkout page so it won't affect any other woo page that might have the .required class in an abbr element.
To make the field not required with a function:
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_2']['required'] = false;
return $address_fields;
}
There are two filters to manage checkout fields. You can use woocommerce_checkout_fields filter to make field "not-required", but the red asterisk will not be removed.
When dealing with default address fields with woocommerce_checkout_fields filter, some of your changes will not take effect, because woocommerce_default_address_fields filter and its default values may override your changes.
Only partly functional code:
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_1']['required'] = false ;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
Address field (#1) is not required anymore, but still has red asterisk.
Fully functional code:
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_1'][ 'required' ] = false;
return $address_fields;
}
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
Now this field is not required, and red asterisk is gone.
Documentation says that "In some specific cases you will need to use the woocommerce_default_address_fields filter. This filter is applied to all billing and shipping default fields."
You have to add the following code to your functions.php file. The example below is for postal code:
/* Seteaza campul Cod postal ne-obligatoriu */
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_postcode', 10, 1 );
function wc_npr_filter_postcode( $address_fields ) {
$address_fields['billing_postcode']['required'] = false;
return $address_fields;
}
/* End - Seteaza campul Cod postal ne-obligatoriu */