WC_Customer::has_full_shipping_address
Checks whether the address is "full" in the sense that it contains all required fields to calculate shipping rates. This method uses the current country's locale to determine if a field is required, or falls back to the default locale if there's no country-specific setting for that field.
This method is only used internally by StoreAPI, and not by the classic/shortcode checkout.
Method of the class: WC_Customer{}
No Hooks.
Returns
true|false. Whether the customer has a full shipping address (city, state, postcode, country). Only required fields are checked based on the country locale.
Usage
$WC_Customer = new WC_Customer(); $WC_Customer->has_full_shipping_address();
Changelog
| Since 9.8.0 | Introduced. |
WC_Customer::has_full_shipping_address() WC Customer::has full shipping address code WC 10.5.0
public function has_full_shipping_address() {
// These are the important fields required to get the shipping rates. Note that while we're respecting the filters
// for the shipping calculator below (city, postcode, state), we're not respecting the filter for the country field.
// The country field is always required as a bare minimum for shipping.
$shipping_address = array(
'country' => $this->get_shipping_country(),
'city' => $this->get_shipping_city(),
'state' => $this->get_shipping_state(),
'postcode' => $this->get_shipping_postcode(),
);
$address_fields = WC()->countries->get_country_locale();
$locale_key = ! empty( $shipping_address['country'] ) && array_key_exists( $shipping_address['country'], $address_fields ) ? $shipping_address['country'] : 'default';
$default_locale = $address_fields['default'];
$country_locale = $address_fields[ $locale_key ] ?? array();
/**
* Checks all shipping address fields against the country's locale settings.
*
* If there's a `required` setting for the field in the country-specific locale, that setting is used, otherwise
* the default locale's setting is used. If the default locale doesn't have a setting either, the field is
* considered optional and therefore valid, even if empty.
*/
foreach ( $shipping_address as $key => $value ) {
// Skip further checks if the field has a value. From this point on $value is empty.
if ( ! empty( $value ) ) {
continue;
}
// If the field is hidden in the country-specific locale, we can skip it.
if ( isset( $country_locale[ $key ]['hidden'] ) && true === wc_string_to_bool( $country_locale[ $key ]['hidden'] ) ) {
continue;
}
// Check if the field is hidden in the default locale, if so, we can skip too (because it wasn't hidden in country-specific locale).
if ( isset( $default_locale[ $key ]['hidden'] ) && true === wc_string_to_bool( $default_locale[ $key ]['hidden'] ) ) {
continue;
}
$locale_to_check = isset( $country_locale[ $key ]['required'] ) ? $country_locale : $default_locale;
// If the locale requires the field return false.
if ( isset( $locale_to_check[ $key ]['required'] ) && true === wc_string_to_bool( $locale_to_check[ $key ]['required'] ) ) {
return false;
}
}
return true;
}