I have tried the below code to make the default state for visitors blank, so they have to manually choose it.
I want it to work only for non logged in users, cause I would like the state to be preselected if a registered customer goes to checkout, because probably WooCommerce will have kept his state from when he was registered!
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
if ( ! is_user_logged_in() && is_checkout() ) {
return ''; //set it to blank.
}
}
Unfortunately the above code works even for logged in users. Any advice?
2
Answers
Make sure that there is always is something to return outside a condition when using filter hooks. Otherwise you get the chance that if the condition is not met, nothing will be returned or that you will receive an error message.
So you get:
This is the correct hook used for this
return apply_filters( 'default_checkout_' . $input, $value, $input );
line 1270 inwoocommerce/includes/class-wc-checkout.php
. There is always two parameters for this hook and there is no hookdefault_checkout_shipping_state
with single parameter.