Setting Up Predefined Shipping Areas and Shipping Accordingly picture of ships on water

WooCommerce offers a great set of default settings for payment and shipping. In some cases, you may want to set up predefined shipping areas for customers to choose from.  In this post I will show how you can set that up using code snippets/plugins.

Setting Up Shipping Areas

I’m going to assume that the current areas are not a part of the default WooCommerce checkout fields. We will be adding a new Living area drop down field to the checkout with a code snippet. I’ve prepared the code snippet below to work and add the new field, but if you want to read more about it, this is a great place.

The code snippet below adds the new checkout field. I’ve given it 4 options going from ‘Alpha’ to ‘ Delta’. You can add your own options in that list. Make sure that the slugs do not have spaces or capitals.

https://gist.github.com/47b89bbc31c84aba4d8b

Do note that if you change the shipping_living_area in $fields['shipping']['shipping_living_area'] it should be prefixed with shipping_, otherwise it will not automatically save to the database.

The new checkout field:

custom-woocommerce-shipping-area-checkout-field

Display Your Custom Field in the Admin

The new field will be automatically saved to the database, but it will not automatically show up in the admin order area. This is not required and you can skip this if you don’t want or need the Living area field to be displayed in the admin. If you do want to show the new field, the following snippet will enable and display the field in the backend at the order screen.

https://gist.github.com/466b1d80374b280e20fd

Resulting into this:

add-custom-field-to-admin-order-screen

Adding the Custom Field to the Emails

The new custom field also doesn’t show on the order confirmation mails by default. If you do want it to show up, you can use the following code snippet.

https://gist.github.com/35eb9af7f37eba4da0aa

That’s it for setting up the new custom shipping field. Next up: setting up shipping rates.

Setting Up Shipping Rates

Next is setting up shipping rates according to the new shipping field. Setting up a custom shipping method can be difficult and time consuming. As far as I know, there is no plugin that supports setting up shipping based on custom shipping fields. Not even my plugin, WooCommerce Advanced Shipping, does that by default.

What you can do with WooCommerce Advanced Shipping is add new conditions very easily. I’ve already done all the work here for you, so you only have to copy/paste the following script. This will add multiple new conditions, one for each shipping field (all RAW shipping fields are added as a new condition here, so there will be some duplicates such as ‘zipcode’).

https://gist.github.com/e3108b66c53485e6a8c9

This will add the shipping field conditions as following:

woocommerce-advanced-shipping-custom-shipping-fields

WooCommerce Advanced Shipping will now allow you to set up shipping rates, according to the new custom shipping field.

Update June ’16

We now have a plugin that allows you to set up custom checkout fields. If you’re not comfortable coding something custom as per the post above, you can take a look at the Advanced Checkout Fields plugin. It also works great with the Advanced Shipping plugin.

Get our best WooCommerce advice!

Delivered directly to your inbox

Your email is 100% private. We hate spam too. Powered by ConvertKit

Published by Shop Plugins

16 Comments

  1. Hi,

    many thanks for this, but I am confused what files do I add this coding to?

    Regards

    Rob

  2. Hi,

    Many thanks for that, I have added to Functions.php in child theme. The front end of the site is still working, but loggin in to WP-Admin is not possible as I have a PHP error.

    Fatal error: Call to a member function get_country() on null in /home/xxxx/public_html/xxxx/wp-content/plugins/woocommerce/includes/class-wc-checkout.php on line 773

    Also see this screen shot.

  3. Sorry there is no screenshot as I gave URL in Website field.

  4. Hi Rob,

    On what version of WC are you? I’ve just tested it and it seems to work correctly with me..
    If you keep getting the error, can you try removing the parts one by one to see which part is causing it..

    Thanks,
    Jeroen

  5. Hi

    I am using WP 4.3.1

    And I have just removed // Add all shipping fields matching functions and I can now login.

    In Settings/Advaced Shipping I get an error when I click ‘Add New Advanced Shipping method’

    This is the error: Add New Advanced Shipping method ‘atal error: Call to a member function get_country() on null in /xxxx/xxxxx/public_html/xxxxx/wp-content/plugins/woocommerce/includes/class-wc-checkout.php on line 773’

    And just as a side issue, is the ‘new shipping options – Alpha/Bravo/Charlie/Delta’ only appears in the ‘Shipping Address’ area. It doesn’t appear in ‘Address’ area, which obviously most customers will use as they will want delivery/shipping to occur to their homes.

    Many thanks
    Rob

    • Hi Rob,

      Sounds like theres still some error in there. I’ve just tested things and it does seem to work with me so I think there’s still got some more debugging to do :-)

      Can you check/debug some more and report back?
      Thanks

  6. Hi,

    I am not sure what I am to debug or test? The four lines of code I removed allow the wp-admin to work, but the predefined shipping doesn’t…

    Sorry PHP is not my strong point

  7. Hi Rob,

    Try removing some more code to see which part is causing it. Then you can try adding the following to prevent things from executing in the admin area:
    if ( is_admin() ) return;

    Cheers,
    Jeroen

  8. Sorry i don’t understand I have removed a part of the coding and that allowed me access to WP-admin.

    So now you are saying to remove more coding until the error in admin disappears?

    I presume then I would know which two parts of your module aren’t working for me.

  9. Actually what do you charge to make the thing work?

  10. Hi,
    I have done exactly as mentioned above but its not working for me. selecting different “local Area” does not change the shipping value while it works with any other field which was predefined in “Advance Shipping Method” such as City.

  11. Hi there Jeroen,

    I have tried to implement the script you suggested above to implement shipping areas (https://shopplugins.com/setting-up-predefined-shipping/)

    Would it be possible for you to assist?

    I get the following error when implementing the script:

    ==========================================================
    Fatal error: Call to a member function get_country() on null in /app/public/wp-content/plugins/woocommerce/includes/class-wc-checkout.php on line 802
    Call Stack

    Time Memory Function Location 1 0.0006 247520 {main}( ) …/admin.php:0 2 0.0009 248040 require_once( ’/app/public/wp-load.php’ ) …/admin.php:31 3 0.0014 248808 require_once( ’/app/public/wp-config.php’ ) …/wp-load.php:37 4 0.0017 250032 require_once( ’/app/public/wp-settings.php’ ) …/wp-config.php:81 5 0.6081 7022568 do_action( ) …/wp-settings.php:449 6 0.6081 7023240 WP_Hook->do_action( ) …/plugin.php:453 7 0.6081 7023336 WP_Hook->apply_filters( ) …/class-wp-hook.php:323 8 0.7829 8452152 call_user_func_array:{/app/public/wp-includes/class-wp-hook.php:298} ( ) …/class-wp-hook.php:298 9 0.7829 8452568 was_add_shipping_field_condition_matches( ) …/class-wp-hook.php:298 10 0.7830 8452600 WooCommerce->checkout( ) …/functions.php:953 11 0.7882 8721384 WC_Checkout::instance( ) …/woocommerce.php:512 12 0.7882 8721992 WC_Checkout->__construct( ) …/class-wc-checkout.php:64 13 0.7889 8729360 WC_Checkout->get_value( ) …/class-wc-checkout.php:100 ============================================================

  12. Hi Jeroen,

    I have now added each section at atime to try and find out which causes the issue. It appears that it is the “Setting Up Shipping Rates” section that is causing the error.

    I do however not know enough to see what exactly is causing the issue in that section. Would it be possible for you to assist?

    Kind regards
    Francois

    • Hi,

      Seems like it is related to the last function, and only happening in the admin area for me.
      Change the line to something like this and it won’t execute on the admin:
      if ( is_admin() || ! is_callable( array( WC(), 'checkout' ) ) ) :

      Cheers,
      Jeroen

Comments are closed.