I have standard woocommerce select dropdown on checkout page with shipping_country
and billing_country
.
- Can I remove first
option
withvalue=default
using any hook? Or only by jQuery? - After the country have been changed woocommerce triggers an action
updated_cart_totals
. But I do not need it. To not triggerupdated_cart_totals
it is enough to removeupdate_totals_on_change
class. Can I remove it using any hook? Or the same only by jQuery?
<p class="form-row form-row-wide address-field update_totals_on_change" id="shipping_country_field" data-priority="40">
<select name="shipping_country" id="shipping_country" class="country_to_state" autocomplete="country" tabindex="-1" aria-hidden="true">
<option value="default">Land/Region auswählen …</option>
<option value="BE">Belgien</option>
<option value="DE">Deutschland</option>
<option value="LU">Luxemburg</option>
<option value="NL" selected="selected">Niederlande</option>
</select>
</p>
UPDATE
1 question. This is how fields are generated in form-shipping.php or form-billing.php:
foreach ( $fields as $key => $field ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
Look in woocommerce_form_field
code:
2726 function woocommerce_form_field( $key, $args, $value = null ) {
.....
2803 switch ( $args['type'] ) {
case 'country':
$countries = 'shipping_country' === $key ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
if ( 1 === count( $countries ) ) {
$field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
$field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys( $countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" readonly="readonly" />';
} else {
$data_label = ! empty( $args['label'] ) ? 'data-label="' . esc_attr( $args['label'] ) . '"' : '';
2816 $field = '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country_to_state country_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ? $args['placeholder'] : esc_attr__( 'Select a country / region…', 'woocommerce' ) ) . '" ' . $data_label . '><option value="">' . esc_html__( 'Select a country / region…', 'woocommerce' ) . '</option>';
foreach ( $countries as $ckey => $cvalue ) {
$field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . esc_html( $cvalue ) . '</option>';
}
$field .= '</select>';
$field .= '<noscript><button type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country / region', 'woocommerce' ) . '">' . esc_html__( 'Update country / region', 'woocommerce' ) . '</button></noscript>';
}
break;
In the end of 2816 line we can see . '><option value="">' . esc_html__( 'Select a country / region…', 'woocommerce' ) . '</option>';
So 1st question solved - it is impossible to remove this option using any hook ((( only js/jQuery.
UPDATE
2 question.
Removing update_totals_on_change
class can help not to updated_cart_totals
only if we use select2
(selectwoo
). If we deenqueue select2
then updated_cart_totals
triggers every time select changed even without above class.
So the question is still how to disable that trigger on select country change?