Book Image

WooCommerce Cookbook

By : Patrick Rauland
Book Image

WooCommerce Cookbook

By: Patrick Rauland

Overview of this book

Table of Contents (17 chapters)
WooCommerce Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a default country and state to the checkout page


If you are just starting out, you may not have a very wide reach, you may not have any international orders, and you might not even have orders from outside your state or province. If that's the case, why make people type it in? With a touch of code, we can set some defaults that will help people get through the checkout process.

How to do it…

This code can either go in your theme's functions.php file or in a custom WooCommerce plugin. Let's take a look at the following steps:

  1. Open either your theme's functions.php file or a custom plugin file.

  2. We'll start by setting just the default country. At the bottom of the file, add the following code:

    add_filter( 'default_checkout_country', 'woocommerce_cookbook_default_checkout_country' ); 
    // change the default country to USA function 
    woocommerce_cookbook_default_checkout_country() { 
        return 'US'; 
    }
  3. If you also want to set a default state, you'll have to add a bit more code. In the following...