Skip to content

Instantly share code, notes, and snippets.

@corsonr
Created August 1, 2013 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corsonr/6129163 to your computer and use it in GitHub Desktop.
Save corsonr/6129163 to your computer and use it in GitHub Desktop.
WooCommerce - Make "state" field not required on checkout page
<?php
/**
* WooCommerce Extra Feature
* --------------------------
*
* Make "state" field not required on checkout
*
*/
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'] = false;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = false;
return $address_fields;
}
@bogdanvoi
Copy link

Hi,

I want to make state/province a required field so I tried to set billing_state and shipping_state to true but it doesn't work.
I have billing & shipping state not 2 forms : one for shipping and one for billing.

Can you guide me how to do it? If state/province is not a required field, it will not calculate the shipping fee correctly. I have one shipping fee for local and one for province. If the state/province is not filled (is not a required filed right now) it will give me the higher province rate not the local one.

Thanks

@RobertMPop
Copy link

bogdanvoi, I have the same issue. Did you find a solution?

It's strange, because people say that the State field should be "required" by default since 3.3.5 version of Woocommerce.

@lukecav
Copy link

lukecav commented May 25, 2021

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;
}

@christiandegeus
Copy link

@lukecav, I've just tried your solution. When the page loads; it shows the asterix for a second and then returns to optional field.
Any ideas here?

@Laurent1983next
Copy link

Hi, thanks for sharing this details. How can i do if i need to select specific country's to change from optional to required. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment