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.
The Store API uses this check. The classic cart uses it only for a show_shipping() call nested twice inside its own read of the shipping address fields.
Метод класса: WC_Customer{}
Хуков нет.
Возвращает
bool. Whether the customer has a full shipping address (city, state, postcode, country). Only required fields are checked based on the country locale.
Использование
$WC_Customer = new WC_Customer(); $WC_Customer->has_full_shipping_address();
Список изменений
| С версии 9.8.0 | Введена. |
Код WC_Customer::has_full_shipping_address() WC Customer::has full shipping address WC 11.3.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'] ?? null;
$country_locale = $address_fields[ $locale_key ] ?? array();
// The default entry is missing while the locale settings are first built, and a locale filter callback can drop or replace it.
// Country rules are not available during that build, so the default fields apply to every country until it finishes.
if ( ! is_array( $default_locale ) ) {
$default_locale = array();
// A woocommerce_default_address_fields callback can call this method again, so the nested call skips the filter and applies no rules.
if ( ! self::$loading_default_address_fields ) {
self::$loading_default_address_fields = true;
try {
$default_locale = WC()->countries->get_default_address_fields();
} finally {
self::$loading_default_address_fields = false;
}
}
}
/**
* 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;
}