WooCommerce shipping rates not calculating at all when some fields are unset/removed

Hi there,
this article is WordPress developer related, so if you search for plugin solution you will not find your answer here. Article explains which checkout fields affect WooCommerce shipping rates workings as expected.

My WooCommerce shipping rates case

The checkout flow in this project required one country, one city, one state/region. The client was an local bakery with few bakery shops in one city. Users can choose a delivery or pickup from chosen location.

I struggled few days customizing the checkout flow with additional custom fields, and removing unnecessary fields as ‘billing_last_name’, ‘billing_country’, ‘billing_address_2’, ‘billing_state’, ‘billing_city’, ‘billing_postcode’ with filter provided by WooCommerce ‘woocommerce_checkout_fields’.

When I unsetted the ‘billing_country’, ‘billing_state’, ‘billing_city’ shipping rates stopped updating.

<?php 
add_filter( 'woocommerce_checkout_fields', 'smpl_add_conditional_checkout_fields' );
function smpl_add_conditional_checkout_fields( $fields ) {
        // print_r($fields); // you can see the initial fields if uncomment;
        unset( $fields['billing']['billing_last_name'] );
        // unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_address_2'] );
        // unset( $fields['billing']['billing_state'] );
        // unset( $fields['billing']['billing_city'] );
        // unset( $fields['billing']['billing_postcode'] );
}

So, I tested, tried to understand when and in what conditions the shipping rates not working properly.

My conclusion was:

  • ‘billing_country’ is mandatory field. If you want to unset it, better to change it’s type to hidden
  • ‘billing_state’ is mandatory field. If you want to unset it, better to change it’s type to hidden
  • ‘billing_city’ is mandatory field. If you want to unset it, better to change it’s type to hidden
  • ‘billing_postcode’ – is required field, if you apply different fees for different city postcodes If you want to unset it, better to change it’s type to hidden, and set one default code, or change it via JS.
  • ‘billing_address_1’ – can be removed it does not affect shipping rates.
<?php 
        // Hides Country Field
        $fields['billing']['billing_country']['type'] = 'hidden';
        $fields['billing']['billing_country']['clear'] = true;
        // Hides State Field and set default value
        $fields['billing']['billing_state']['type'] = 'hidden';
        $fields['billing']['billing_state']['default'] = 'BG-23';
        $fields['billing']['billing_state']['clear'] = true;
        
        // Hides State Field and set default value
        $fields['billing']['billing_postcode']['default'] = 1000;
        $fields['billing']['billing_postcode']['type'] = 'hidden';
        $fields['billing']['billing_postcode']['clear'] = true;
        // Hides City Field and set default value
        $fields['billing']['billing_city']['type'] = 'hidden';
        $fields['billing']['billing_city']['default'] = 'Sofia';
        $fields['billing']['billing_city']['clear'] = true;

When fields are hidden it doesn’t hide their containers on the frontend. So you need to hide them with CSS also. Each field wrapper has an ID containing field name with suffix ‘_field’ as shown below.

 #billing_state_field,
 #billing_postcode_field,
 #billing_city_field,
 #billing_country_field  {
    visibility: hidden;
    opacity: 0;
    display: none;
 }

And as a final suggestion, if you messing around with WooCommerce Checkout Fields and something went wrong with your WooCommerce Shipping Rates and doesn’t work as excepted, try to change to default theme to try is working fine there.
Then go back to your theme and try comment rows by field in your code until you notice is working fine.

Share you experience in comments.

I rarely write but there some WordPress / WooCommerce dev articles you can take a look.

Share: Facebook, Twitter, Google Plus

Leave a Comment:

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.