Change Default Country in WooCommerce Checkout Easily

How to change default country in woocommerce checkout; Set default country in woocommerce checkout; Woocommerce change checkout country; Default country woocommerce checkout page; Woocommerce set default billing country; Change default country and state in woocommerce; Woocommerce checkout default country setting; How to set default country in woocommerce; Woocommerce checkout page default country; Set default country woocommerce;

Explanation

Want to make sure your WooCommerce checkout page always starts with a specific country and state? This little tweak will do just that!

Here's what it does:

  • Sets a default country and state for both billing and shipping sections on the checkout page.
  • You can choose any country and state by changing the codes in the snippet.

How to use it:

  • Find the lines where it says 'US' and 'CA'.
  • Replace 'US' with the country code you want. For example, use 'GB' for the United Kingdom.
  • Replace 'CA' with the state code you want. For example, use 'NY' for New York.

Once you've made these changes, your checkout page will automatically show your chosen country and state as the default options. Easy peasy!

Code

<?php
/**
 * Change the default country and state in WooCommerce checkout.
 *
 * This function sets the default billing and shipping country and state
 * for the WooCommerce checkout page.
 *
 * @param array $fields The checkout fields.
 * @return array Modified checkout fields with default country and state.
 */
function wp_dudecom_set_default_checkout_country( $fields ) {
    // Set your desired default country and state
    $default_country = 'US'; // Change 'US' to your desired country code
    $default_state = 'CA'; // Change 'CA' to your desired state code

    // Set default billing country and state
    if ( isset( $fields['billing']['billing_country'] ) ) {
        $fields['billing']['billing_country']['default'] = $default_country;
    }
    if ( isset( $fields['billing']['billing_state'] ) ) {
        $fields['billing']['billing_state']['default'] = $default_state;
    }

    // Set default shipping country and state
    if ( isset( $fields['shipping']['shipping_country'] ) ) {
        $fields['shipping']['shipping_country']['default'] = $default_country;
    }
    if ( isset( $fields['shipping']['shipping_state'] ) ) {
        $fields['shipping']['shipping_state']['default'] = $default_state;
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wp_dudecom_set_default_checkout_country' );
?>

Instructions

File Location: Add the code to your theme's functions.php file or in a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Access your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor. Alternatively, use an FTP client to access your theme files.
  3. Locate and open the functions.php file of your active theme.
  4. Copy the provided code snippet.
  5. Paste the code at the end of the functions.php file.
  6. Modify the 'US' and 'CA' values to your desired country and state codes.
  7. Save the changes to the functions.php file.
  8. Visit your WooCommerce checkout page to verify that the default country and state have been updated.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to the experts at wp-dude.com for professional help.