In most cases WooCommerce will show a couple fields to allow the customer to calculate their shipping cost in the cart. This is always the country field and zip- / postcode field, and sometimes in between those the state / province field.

This is a good start, but for some of you who have shipping setup based on other kinds of variables using the Advanced Shipping plugin this isn’t enough. Say for example you have the shipping based on customer’s city, in that case he won’t be able to calculate his shipping in the cart, while this is a great feature to have.

Modifying the shipping calculator fields

In this post I’m going to give you some code snippets that show you how you can modify the shipping calculator by adding and/or deleting the available fields.

Enabling the city field / disable postcode / ZIP

WooCommerce has two fields build in that can easily be enabled / disabled from the calculator. You can turn on the city field and disable the with one line of code and disable the postcode field with another. Later on I’ll show how you can add other fields too, but that does require more effort.

With this code snippet you can enable the city field:

And with this code snippet to disable the existing postcode / ZIP field:

Going deeper: Modifying and adding other fields

As mentioned the other (country / state) fields cannot be easily disabled, nor can you easily (with one line of code) add other fields. To accomplish this you’d need to override the existing template file of the calculator in your child theme.

Don’t have a child theme or know how to override template files? Read these posts to catch up on that:
– Read more about child themes
– Read more about WooCommerce template overriding

Locate and override the template ‘templates/cart/shipping-calculator.php
Make sure you have the ‘shipping-calculator.php’ script copied to your own child theme and set to the correct directory (‘woocommerce/cart/’). In case you’re unsure if you got the right script, here’s a link to the source from WooCommerce: https://github.com/woocommerce/woocommerce/blob/2.6.0/templates/cart/shipping-calculator.php

Removing the country field
In the case where you only ship to one country it may be useful to remove the country field. Otherwise the customer would still see a dropdown with only one option. Instead it would be good to just show the country name.

You can also do the same thing for like the state field.
PS. you don’t have to comment out the old code, you can also remove it, but it can be good for reference.

Adding a custom field
If you’ve added a custom shipping field for example through my Advanced Checkout Fields plugin (or another plugin / code snippet) you can also add this to the shipping calculation. This does require some extra work to ensure WooCommerce is also saving the values you’re sending through the calculator.

Example scenario: You’ve added a dropdown at the checkout with a few options for the customer to choose from. I’m going to call my custom field ‘Area’.

First we want to add the same type of dropdown to the shipping calculator:

This is a example of adding a dropdown, but depending on your needs you may need to modify it to fully fit your needs. You can position this anywhere within the little form.

Now it is showing the dropdown, but not yet using it to calculate the shipping. For that we need a code snippet placed within the themes function.php file.

Saving the custom calculator field value
Next we need to ensure the custom field we created is also used properly for the calculation of the shipping. In this case we want to assign the value set to the shipping_area of the customer, which can also be setup as condition for the shipping.

The code snippet below checks if the value is available, and if so it will save it to the customer object.

And thats the end of it! Hope it helped you out, let me know if you run into anything as it is a bit more complex to set this up properly.

  • Jeroen Sormani

    I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

    Interested in talking about a WordPress project? Get in touch!

    Follow me on Twitter
  • 11 thoughts on “Modifying the WooCommerce cart shipping calculator

    Stavros January 25, 2017 at 12:49 am

    Very good Article Jeroen,

    Could you please post the snippet to remove the state from the shipping calculator.

    I want to leave just the country dropdown list

    Thank you

    Jeroen January 25, 2017 at 9:55 am

    Hi Stavros,

    I’m afraid there’s not an easy code that you can use to disable the state field (no hooks available).
    The only way to disable that is by overriding the shipping calculator template file and remove it directly in there.

    Cheers,
    Jeroen

    steve-o January 26, 2017 at 10:34 pm

    does the line below get placed inside the functions.php files?

    add_filter( ‘woocommerce_shipping_calculator_enable_postcode’, ‘__return_false’ );

    Jeroen January 27, 2017 at 9:02 am

    Hi Steve,

    That is correct, functions.php in your child theme would be the easiest way to integrate this.

    Cheers,
    Jeroen

    Reed February 21, 2017 at 2:07 am

    How do you modify the text for “Calculcate Shipping”?

    Diana February 23, 2017 at 7:33 pm

    Hi Jeroen ,
    I am struggling here with modifying the shipping calculator fields and hope you will be able to help.
    Can I make the postcode field required but only for specific countries?
    For example I want the post code to be required for UK but not for Denmark..Is this possible?
    help please….much appreciated!

    Thanks in advance

    Jeroen February 24, 2017 at 8:38 am

    Hi Diana,

    There is a work-around, but it isn’t the best.
    You could hook into the postcode validation functionality and allow postcodes in Denmark to be empty. The downside of that is that probably other areas such as the checkout may be affected by that too.

    Cheers,
    Jeroen

    Matt February 28, 2017 at 10:15 pm

    Hi Jeroen,

    Is there a way to make the postcode field mandatory

    Rodrigo March 11, 2017 at 1:53 am

    Hi Jeroen,

    I would like to know, how can I remove or hidden the coutry field and state field in my shipping calculator of the woocommerce. Because I would like to calculate the shipping by the zipcode. Is it possible? How can I do this?

    thanks.

    Sabina May 25, 2017 at 10:15 am

    Hi Jeroen,
    How can I place my shipping calculator on store page? I tried to modify the code numerous times but without satisfying effect. Would it be at all possible?

    Thank you

    Jeroen May 25, 2017 at 12:19 pm

    Hi,

    I do believe this is possible, but not something that is covered in this blog post. You’d need to go a different route to accomplish something like that then whats described in here.

    Cheers,
    Jeroen

    Leave a Reply