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 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.
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.