Since Woo 3.5.1 the billing state field is mandatory for certain countries and I'm trying to figure out how to not make it mandatory.
This is the code I'm using, which is not working. It seems to be a core change and I don't know if there's a new hook for it?
Code:
add_filter( 'woocommerce_billing_fields', 'remove_mandatory_state_billing', 10, 1 );
function remove_mandatory_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = false;
return $address_fields;
}
add_filter( 'woocommerce_shipping_fields', 'remove_mandatory_state_shipping', 10, 1 );
function remove_mandatory_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = false;
return $address_fields;
}
Any help is very much appreciated.
The following code will make the state field optional for all countries in Woocommerce:
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale_state_optional', 10, 1 );
function custom_country_locale_state_optional( $locale ) {
foreach( $locale as $country_code => $state_field ) {
if( isset($locale[$country_code]['state']) ) {
$locale[$country_code]['state']['required'] = false;
}
}
return $locale;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Related: Make state checkout field required for a specific country in Woocommerce
Related
In woocommerce already the shipping country in restricted to IN
On checkout the default value of Country, state & city is set
I want to do that same in my-account/edit-address/ only in shipping address
tried using this code but its not working
add_filter( 'woocommerce_shipping_fields' , 'default_shipping_address' );
function default_shipping_address( $fields ) {
$fields['shipping_city']['default'] = 'Mumbai';
$fields['shipping_state']['default'] = 'Maharashtra';
return $fields;
}
this is not working let me know what's wrong or is there any other way to do this?
You are pretty near… As Shipping State value in WooCommerce is a state code, you need to replace 'Maharashtra' with 'MH' (For India) as follows:
add_filter( 'woocommerce_shipping_fields' , 'set_default_my_account_shipping_city_state_values' );
function set_default_my_account_shipping_city_state_values( $fields ) {
// Targeting My account section
if( is_account_page() ) {
$fields['shipping_city']['default'] = 'Mumbai';
$fields['shipping_state']['default'] = 'MH'; // <== HERE
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Alternative: If it really doesn't work for you, you can try to use the following:
add_action( 'template_redirect' , 'set_default_shipping_city_and_state_values' );
function set_default_shipping_city_and_state_values( $fields ) {
if( is_wc_endpoint_url( 'edit-address' ) ) {
if ( WC()->customer->get_shipping_city() === '' ) {
WC()->customer->Set_shipping_city('Mumbai');
}
if ( WC()->customer->get_shipping_state() === '' ) {
WC()->customer->Set_shipping_state('MH');
}
}
}
You can use the address hook.
add_filter( 'woocommerce_default_address_fields' , 'default_shipping_address' );
function default_shipping_address( $fields ) {
$fields['shipping_city']['value'] = 'Mumbai';
$fields['shipping_state']['value'] = 'Maharashtra';
return $fields;
}
I am trying to clear the billing_po_no field value from my WooCommerce checkout billing Form, by adding the following code into my functions.php file:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_po_no'] = '';
return $fields;
}
But it does not appear to be working. Can someone point me in the right direction?
Try the following instead:
add_filter( 'woocommerce_checkout_get_value' , 'clear_specific_checkout_field' , 10, 2 );
function clear_specific_checkout_field( $value, $input ){
if( $input === 'billing_po_no' )
$value = '';
return $value;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
I have updated the placeholders for basket fields via the WooCommerce settings, as follows:
However on the frontend, the previous placeholders remain:
I have also used the following code, adapted from another StackOverflow thread which aims to override the placeholder content, but the defaults still persist.
add_filter('woocommerce_default_address_fields', 'override_default_address_checkout_fields', 20, 1);
function override_default_address_checkout_fields( $address_fields ) {
$address_fields['state']['placeholder'] = 'State';
$address_fields['postcode']['placeholder'] = 'Postcode';
return $address_fields;
}
Please advise on how I can achieve the desired placeholder text!
In woocommerce default there is no settings like you describe. So you are using a third party plugin, to customize fields. So you need to try multiple ways with a higher hook priority.
So try those one by one:
1). Using woocommerce_default_address_fields filter hook (with a higher priority hook):
add_filter('woocommerce_default_address_fields', 'customize_default_address_fields', 10000, 1 );
function customize_default_address_fields( $address_fields ) {
$address_fields['state']['placeholder'] = __('State', 'woocommerce');
$address_fields['postcode']['placeholder'] = __('Postcode', 'woocommerce');
return $address_fields;
}
2). Using woocommerce_checkout_fields filter hook:
add_filter('woocommerce_checkout_fields', 'customize_checkout_fields', 10000, 1 );
function customize_checkout_fields( $fields ) {
$fields['billing']['billing_state']['placeholder'] = __('State', 'woocommerce');
$fields['shipping']['shipping_state']['placeholder'] = __('State', 'woocommerce');
$fields['billing']['billing_postcode']['placeholder'] = __('Postcode', 'woocommerce');
$fields['shipping']['shipping_postcode']['placeholder'] = __('Postcode', 'woocommerce');
return $fields;
}
3). Using woocommerce_billing_fields and woocommerce_shipping_fields filters hooks:
add_filter('woocommerce_billing_fields', 'customize_billing_fields', 10000, 1 );
function customize_billing_fields( $billing_fields ) {
$billing_fields['billing_state']['placeholder'] = __('State', 'woocommerce');
$billing_fields['billing_postcode']['placeholder'] = __('Postcode', 'woocommerce');
return $billing_fields;
}
add_filter('woocommerce_shipping_fields', 'customize_shipping_fields', 10000, 1 );
function customize_shipping_fields( $shipping_fields ) {
$shipping_fields['shipping_state']['placeholder'] = __('State', 'woocommerce');
$shipping_fields['shipping_postcode']['placeholder'] = __('Postcode', 'woocommerce');
return $shipping_fields;
}
All code goes in function.php file of your active child theme (or active theme).
I hope that one of those will work. Without a third party plugin each of those codes works perfectly.
So this problem may have a easy solution, but I'm stuck for the moment. After the last update (Woocommerce 3.3.5) I have a problem with the state field on the checkout page, because it is not mandatory and people just skip it. I really need this thing to be mandatory, because I have connected my website to the delivery company server through and API to send the order info directly to them.
I tried adding this to my functions.php and the thing is that when I go to the checkout page, the field has an asterisk, but for like one second.
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing',
10, 1 );
add_filter( 'woocommerce_shipping_fields',
'woo_filter_state_shipping', 10, 1 );
function woo_filter_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = true;
return $address_fields;
}
Any help is appreciated. Thanks!
By default in Last woocommerce version 3.3.5, state field is required… So in your case something is making that field "not" required.
You can try this (work for billing and shipping fields at the same time):
add_filter( 'woocommerce_default_address_fields' , 'make_state_field_required', 90, 1 );
function make_state_field_required( $address_fields ) {
$address_fields['state']['required'] = true;
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). It could works.
This worked better for me as of January 2019
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 );
function woo_filter_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = true;
return $address_fields;
}
Maybe you mean "country"? I had that problem, being that field inexplicably optional.
In that case, the code would be:
add_filter( 'woocommerce_billing_fields' , 'make_country_field_required', 90, 1 );
function make_country_field_required( $address_fields ) {
$address_fields['billing_country']['required'] = true;
return $address_fields;
}
I have been using this custom function below in the previous versions of WooCommerce in order to pre-fill the City and ZIP code fields:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['default'] = '90210';
return $fields;
}
It has been working great until the new WC updates.
The city still works, but the default ZIP code field doesn't seem to work anymore. It doesn't automatically pre-polulate the value.
Anything changed? Is there any other workaround for this?
Thanks
Setting a value for 'post-code' fields doesn't work anymore as there is an autocomplete feature. Even when disable "autocomplete", this doesn't work. So the workaround is to use jQuery in this case.
So your code is going to be:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 10, 1 );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['autocomplete'] = "off"; // Removing autocomplete
return $fields;
}
add_action( 'woocommerce_after_checkout_form' , 'my_custom_checkout_field_postcode' );
function my_custom_checkout_field_postcode( ) {
?>
<script>
(function($){
$('#billing_postcode').val('90210');
})(jQuery);
</script>
<?php
}
This will set correctly your the desired value in "post-code" checkout billing field.
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Not necessary have to use java-script when you use the woocommerce callback made for the job: 'woocommerce_checkout_get_value'.
The 'woocommerce_after_checkout_form' call is made for setting the attributes of the check out fields, such as disabling the autocomplete.
The point to note is this function will be repeatedly called for each and every field within the check out form. So you switch on the field and return the value you wish assigned to the check-out form:
Based on your above code. here ya go...
function populating_checkout_fields ($fields, $input)
{
global $woocommerce;
switch($input)
{
case 'billing_city':
$FieldValue = 'Beverly Hills';
return $FieldValue;
break;
}
return $fields; // return the default value
}
add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function ModifyAutoComplete($fields)
{
$fields['billing']['billing_postcode']['autocomplete'] = null;
}
add_filter( 'woocommerce_checkout_fields' , 'ModifyAutoComplete', 10, 1 );