0

I have standard woocommerce select dropdown on checkout page with shipping_country and billing_country.

  1. Can I remove first option with value=default using any hook? Or only by jQuery?
  2. After the country have been changed woocommerce triggers an action updated_cart_totals. But I do not need it. To not trigger updated_cart_totals it is enough to remove update_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&nbsp;…</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&hellip;', 'woocommerce' ) ) . '" ' . $data_label . '><option value="">' . esc_html__( 'Select a country / region&hellip;', '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&hellip;', '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?

Prizzrak
  • 1
  • 2
  • There is a reason to update after changing. You may need to restrict payment options or add additional taxes depending on the client's delivery. You can dequeue the scripts on checkout but make sure u test everything. As for the default option Billing if you set up your store address will be by default your store country. If the client is logged in and has saved billing/shipping address will use these. – Snuffy Jun 14 '22 at 06:22
  • @MartinMirchev I do not need to dequeue the script on checkout, only turn off the trigger when country changed. It is selected option you mentioned as default. It works as you described. I need just to remove/delete the default option which is "Select country..." from dropdown. – Prizzrak Jun 14 '22 at 11:55
  • One option is check this answer - https://stackoverflow.com/questions/60201103/woocommerce-checkout-add-placeholder-in-country-dropdown and more specificly this function add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 ); – Snuffy Jun 14 '22 at 12:28

0 Answers0