4

In WooCommerce admin settings (/wp-admin/admin.php?page=wc-settings) there are several fields to set the store address (street, city, country).

How can one retrieve this in a theme's templates? I wouldn't want to hardcode such informations into the theme.

2
  • store address? are you talking about getting shop url?
    – user145078
    Commented Nov 15, 2018 at 19:52
  • No sorry, I meant the physical store address. Street, city, country.
    – Buzut
    Commented Nov 16, 2018 at 8:52

5 Answers 5

14

The physical address of the store (and many other settings) are stored in WP's options table (wp_options where "wp_" is the table prefix being used on the site).

The option names are:

  • woocommerce_store_address
  • woocommerce_store_address_2
  • woocommerce_store_city
  • woocommerce_store_postcode
  • woocommerce_default_country

The tricky thing is the woocommerce_default_country value. That stores the country AND state/province info depending on your selection in the "Country/State" dropdown selector in the settings. If it's just a country, it will be the country code, but if it's country "plus" something, it will be separated by a colon ":" (such as "US:IL").

So the following is a generic way of doing a US address. Other countries/provinces may vary slightly depending on (1) what's in the woocommerce_default_country value (which may be just a country alone) and (2) how you want to output the info.

// The main address pieces:
$store_address     = get_option( 'woocommerce_store_address' );
$store_address_2   = get_option( 'woocommerce_store_address_2' );
$store_city        = get_option( 'woocommerce_store_city' );
$store_postcode    = get_option( 'woocommerce_store_postcode' );

// The country/state
$store_raw_country = get_option( 'woocommerce_default_country' );

// Split the country/state
$split_country = explode( ":", $store_raw_country );

// Country and state separated:
$store_country = $split_country[0];
$store_state   = $split_country[1];

echo $store_address . "<br />";
echo ( $store_address_2 ) ? $store_address_2 . "<br />" : '';
echo $store_city . ', ' . $store_state . ' ' . $store_postcode . "<br />";
echo $store_country;

This certainly could be condensed quite a bit and made neater, but I was verbose in the code for clarity.

Also, depending on the store location, etc, you may or may not want to include the country/state info at all, or you may want to alter what it output based on the saved value. For example, in the case of US addresses, the country is "US", not "USA", and the state info is the abbreviated mail code (i.e. "IL" for Illinois). So the actual value and how you intend to use it will determine what you want to do with it for output. (Hope that makes sense.)

2
  • 1
    The field name is woocommerce_store_address_2, with understore before '2'.
    – Zmey
    Commented Jan 13, 2019 at 12:40
  • 1
    whoops - not sure how I messed that one up. I've edited above accordingly.
    – butlerblog
    Commented Jan 13, 2019 at 13:59
13

Instead of the get_option() method mentioned above, it's much better to use the built-in functions via the main instance of WooCommerce WC(), thereby accessing the methods of the WC_Countries class:

WC()->countries->get_base_address();
WC()->countries->get_base_address_2();
WC()->countries->get_base_city();
WC()->countries->get_base_postcode();
WC()->countries->get_base_state();
WC()->countries->get_base_country();

For one, the place where the information is stored could change, but the method to get it stays the same. And secondly, in case a plugin changes the value using filters. Looking at the source the latter gets obvious:

public function get_base_address() {
    $base_address = get_option( 'woocommerce_store_address', '' );
    return apply_filters( 'woocommerce_countries_base_address', $base_address );
}

Additional information:

1
  • The state is selected from woocommerce with the same dropdown that says Country / RegionIs this "Region" stored separately and can be accessed through get_base_state()?
    – svelandiag
    Commented May 1, 2020 at 21:26
1

To get the shop country full name :

WC()->countries->countries[WC()->countries->get_base_country()];
0

There's not a specific function in WooCommerce to return shop (or other) URLs (at least that I know of). But there is a function wc_get_page_id() that will get the page ID by name, and the ID can be used in WP's get_permalink() function to return the URL.

Here are some examples:

Shop page URL:

echo get_permalink( wc_get_page_id( 'shop' ) );

Cart page URL:

echo get_permalink( wc_get_page_id( 'cart' ) );

Checkout page URL:

echo get_permalink( wc_get_page_id( 'checkout' ) );

My Account page URL:

echo get_permalink( wc_get_page_id( 'myaccount' ) );

2
  • Thank you for your answer. I am actually looking to retrieve the physical addresse (like street etc)
    – Buzut
    Commented Nov 16, 2018 at 8:53
  • Ahhhh... sorry - I was thinking URL address. I'll leave this answer in case anyone later needs this (if it comes up in someone's search for URL address), but will post another answer for physical address which is stored in the options table.
    – butlerblog
    Commented Nov 16, 2018 at 12:37
0

The following function will return the store location in the same format as that used in WooCommerce for shipping addresses. (This is useful for example if you are building a shipping method calculator and you need to do address processing.)

function get_store_location() {
    $countries = WC()->countries;
    $location = [];

    foreach ( [ 'country', 'state', 'postcode', 'city', 'address', 'address_2' ] as $field ) {
        $location[ $field ] = $countries->{"get_base_$field"}();
    }

    return $location;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.