Automattic\WooCommerce\Blocks\Shipping

PickupLocation::has_valid_pickup_location()protectedWC 1.0

Checks if a given address is complete.

Метод класса: PickupLocation{}

Хуков нет.

Возвращает

true|false.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->has_valid_pickup_location( $address );
$address(массив) (обязательный)
Address.

Код PickupLocation::has_valid_pickup_location() WC 9.4.2

protected function has_valid_pickup_location( $address ) {
	// Normalize address.
	$address_fields = wp_parse_args(
		(array) $address,
		array(
			'city'     => '',
			'postcode' => '',
			'state'    => '',
			'country'  => '',
		)
	);

	// Country is always required.
	if ( empty( $address_fields['country'] ) ) {
		return false;
	}

	// If all fields are provided, we can skip further checks.
	if ( ! empty( $address_fields['city'] ) && ! empty( $address_fields['postcode'] ) && ! empty( $address_fields['state'] ) ) {
		return true;
	}

	// Check validity based on requirements for the country.
	$country_address_fields = wc()->countries->get_address_fields( $address_fields['country'], 'shipping_' );

	foreach ( $country_address_fields as $field_name => $field ) {
		$key = str_replace( 'shipping_', '', $field_name );

		if ( isset( $address_fields[ $key ] ) && true === $field['required'] && empty( $address_fields[ $key ] ) ) {
			return false;
		}
	}

	return true;
}