wc_get_customer_default_location()WC 2.3.0

Get the customer's default location.

Filtered, and set to base location or left blank. If cache-busting, this should only be used when 'location' is set in the querystring.

Return

Array.

Usage

wc_get_customer_default_location();

Changelog

Since 2.3.0 Introduced.

wc_get_customer_default_location() code WC 9.6.0

function wc_get_customer_default_location() {
	$set_default_location_to = get_option( 'woocommerce_default_customer_address', 'base' );

	// Unless the location should be blank, use the base location as the default.
	if ( '' !== $set_default_location_to ) {
		$default_location_string = get_option( 'woocommerce_default_country', 'US:CA' );
	}

	$default_location = wc_format_country_state_string(
		/**
		 * Filter the customer default location before geolocation.
		 *
		 * @since 2.3.0
		 * @param string $default_location_string The default location.
		 * @return string
		 */
		apply_filters( 'woocommerce_customer_default_location', $default_location_string ?? '' )
	);

	// Ensure defaults are valid.
	$allowed_countries = WC()->countries->get_allowed_countries();

	if ( ! in_array( $default_location['country'], array_keys( $allowed_countries ), true ) ) {
		$default_location = array(
			'country' => '',
			'state'   => '',
		);
	}

	// Geolocation takes priority if geolocation is possible.
	if ( in_array( $set_default_location_to, array( 'geolocation', 'geolocation_ajax' ), true ) ) {
		$default_location = wc_get_customer_geolocation( $default_location );
	}

	/**
	 * Filter the customer default location after geolocation.
	 *
	 * @since 2.3.0
	 * @param array $customer_location The customer location with keys 'country' and 'state'.
	 * @return array
	 */
	return apply_filters( 'woocommerce_customer_default_location_array', $default_location );
}