Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Woocommerce county field missing the CSS class form-control making it styled different to other textfields #2087

Open
hsankala opened this issue Feb 11, 2023 · 5 comments · Fixed by #2089

Comments

@hsankala
Copy link

hsankala commented Feb 11, 2023

Understrap v1.2.2
Clean Fresh Install of WordPress 6.1.1
Only Plugins Woocommerce

On the Woocommerce Checkout page (/checkout/) the county field has the incorrect CSS class applied, making it styled different and it looks janky.

On a fresh installation of WordPress and understrap, you'll notice that the County text field is styled differently in the below screenshot

image

The HTML for this textbox is:

<input type="text" class="input-text " value="" placeholder="" name="billing_state" id="billing_state" data-plugin="select2" data-allow-clear="true" aria-hidden="true" autocomplete="address-level1" data-input-classes="">
It's missing the class form-control which all other text fields have on this page it makes it look odd and stand out.

Could you suggest a workaround for this I've attempted to look to override the form-checkout.php but this page calls <?php do_action( 'woocommerce_checkout_billing' ); ?> which appears to be spitting out the HTML form so I can't simply add this missing class.

@hsankala hsankala changed the title Please provide a short summary of the bug (approx. 50 characters) Woocommerce county field missing the CSS class form-control making it styled different to other textfields Feb 11, 2023
@IanDelMar
Copy link
Contributor

IanDelMar commented Feb 11, 2023

Add this to your functions.php

add_filter( 'woocommerce_form_field_args', 'slug_state_field_args', 10, 2 );
function slug_state_field_args( $args, $key ) {
	if ( 'state' !== $args['type'] ) {
		return $args;
	}

	// Get country.
	$country = $args['country'] ?? null;
	if ( ! $country ) {
		$ctx     = $key ? 'billing_country' : 'shipping_country';
		$country = WC()->checkout->get_value( $ctx );
	}

	// Add form-control if country has no states.
	if ( false === WC()->countries->get_states( $country ) ) {
		if ( isset( $args['input_class'] ) ) {
			$args['input_class'][] = 'form-control';
		} else {
			$args['input_class'] = array( 'form-control' );
		}
	}

	return $args;
}

@hsankala
Copy link
Author

@IanDelMar Oh you WordPress wizard you, thanks for the workaround!

@IanDelMar
Copy link
Contributor

Your're welcome!

@IanDelMar
Copy link
Contributor

@hsankala You better use this:

add_filter( 'woocommerce_form_field_args', 'slug_state_field_args', 10 );
function slug_state_field_args( $args ) {
	if ( 'state' !== $args['type'] ) {
		return $args;
	}

	if ( isset( $args['input_class'] ) ) {
		$args['input_class'][] = 'form-control';
	} else {
		$args['input_class'] = array( 'form-control' );
	}

	return $args;
}

Reason: WooCommerce uses AJAX to switch between countries. This has the following effect: If on load there is an input[type=text] field, e.g. for UK, and you select, e.g. US, it shows a select2 state select. Everything works as expected. You can then switch back to UK and the input field is still properly styled. But if on load there is a select2 and you select a country that produces an input[type=text] field it will not be properly styled.

Adding the form-control to the input_class does not change the select2 field but always styles the text input field.

@IanDelMar
Copy link
Contributor

@bacoords needs label "woocommerce"

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

Successfully merging a pull request may close this issue.

3 participants