Skip to content

Commit

Permalink
Autocorrect typical user input errors for address 1 field (in case st…
Browse files Browse the repository at this point in the history
…reet number validation is active), e.g. Bootstr.12 -> Bootstraße 12. Fix 0 street number validation in block checkout.
  • Loading branch information
dennisnissle committed Oct 8, 2024
1 parent 556bd7b commit ed0bf17
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
38 changes: 38 additions & 0 deletions includes/class-wc-gzd-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function __construct() {
if ( 'never' !== get_option( 'woocommerce_gzd_checkout_validate_street_number' ) ) {
// Maybe force street number during checkout
add_action( 'woocommerce_after_checkout_validation', array( $this, 'maybe_force_street_number' ), 10, 2 );
add_filter( 'woocommerce_checkout_posted_data', array( $this, 'maybe_format_address_1' ), 10 );
}

/**
Expand Down Expand Up @@ -433,6 +434,43 @@ public function filter_de_states_locale( $locale ) {
return $locale;
}

/**
* Enforces whitespace between street name and house number, e.g. typical input issues
* like "Street12" instead of "Street 12".
*
* @param string $address_1
*
* @return string
*/
public function format_address_1( $address_1 ) {
if ( function_exists( 'wc_gzd_split_shipment_street' ) ) {
$do_validate = get_option( 'woocommerce_gzd_checkout_validate_street_number' );

if ( apply_filters( 'woocommerce_gzd_autocorrect_address_1', in_array( $do_validate, array( 'always', 'base_only', 'eu_only' ), true ) ) ) {
$parts = wc_gzd_split_shipment_street( $address_1 );

if ( '' !== $parts['number'] && ! empty( $parts['street'] ) ) {
$address_1 = trim( str_replace( $parts['street'], ' ' . $parts['street'] . ' ', $address_1 ) ); // replace the street name tailored with whitespace
$address_1 = preg_replace( '/\s+/', ' ', $address_1 ); // Remove duplicate whitespace
}
}
}

return $address_1;
}

public function maybe_format_address_1( $data ) {
if ( ! empty( $data['billing_address_1'] ) ) {
$data['billing_address_1'] = $this->format_address_1( $data['billing_address_1'] );
}

if ( ! empty( $data['shipping_address_1'] ) ) {
$data['shipping_address_1'] = $this->format_address_1( $data['shipping_address_1'] );
}

return $data;
}

/**
* @param array $data
* @param WP_Error $errors
Expand Down
22 changes: 21 additions & 1 deletion src/Blocks/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function ( $errors, $fields, $group ) {

if ( in_array( $country, $countries, true ) ) {
$address_parts = wc_gzd_split_shipment_street( $address_1 );
$is_valid = empty( $address_parts['number'] ) ? false : true;
$is_valid = '' === $address_parts['number'] ? false : true;
}

if ( ! apply_filters( 'woocommerce_gzd_checkout_is_valid_street_number', $is_valid, $fields ) ) {
Expand All @@ -288,6 +288,26 @@ function ( $errors, $fields, $group ) {
3
);

add_action(
'woocommerce_store_api_checkout_update_customer_from_request',
function ( $customer, $request ) {
if ( 'never' !== get_option( 'woocommerce_gzd_checkout_validate_street_number' ) ) {
$billing = $request['billing_address'];
$shipping = $request['shipping_address'];

if ( ! empty( $billing['address_1'] ) ) {
$customer->set_billing_address_1( \WC_GZD_Checkout::instance()->format_address_1( $billing['address_1'] ) );
}

if ( ! empty( $shipping['address_1'] ) ) {
$customer->set_shipping_address_1( \WC_GZD_Checkout::instance()->format_address_1( $shipping['address_1'] ) );
}
}
},
10,
2
);

/**
* This hook does not contain any request data, therefor has only limited value.
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/unit-tests/util/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ protected function clean_newlines( $post_content ) {
return str_replace( array( "\r", "\n"), '', $post_content );
}

public function test_format_address_1() {
update_option( 'woocommerce_gzd_checkout_validate_street_number', 'always' );

$checkout = WC_GZD_Checkout::instance();

$this->assertEquals( 'Bootstr. 12', $checkout->format_address_1( 'Bootstr.12' ) );
$this->assertEquals( 'Bootstr. 12', $checkout->format_address_1( 'Bootstr. 12' ) );
$this->assertEquals( 'Bootstraße 12', $checkout->format_address_1( 'Bootstraße 12' ) );
$this->assertEquals( 'Bootstraße 12', $checkout->format_address_1( 'Bootstraße12' ) );
}

/**
* Tests wc_gzd_get_post_plain_content().
*
Expand Down
7 changes: 0 additions & 7 deletions woocommerce-germanized.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,6 @@ function () {
}

\Vendidero\Germanized\PluginsHelper::init();

add_action(
'init1',
function () {
$product = wc_get_product( 4871 );
}
);
}

public function declare_feature_compatibility() {
Expand Down

0 comments on commit ed0bf17

Please sign in to comment.