Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Checkout/Store API - Allow partial pushes without country #8425

Merged
merged 5 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions assets/js/data/cart/push-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@ const isBillingAddress = (
return 'email' in address;
};

export const trimAddress = ( address: BillingOrShippingAddress ) => {
const trimmedAddress = {
...address,
};
Object.keys( address ).forEach( ( key ) => {
trimmedAddress[ key as keyof BillingOrShippingAddress ] =
address[ key as keyof BillingOrShippingAddress ].trim();
} );

trimmedAddress.postcode = trimmedAddress.postcode
? trimmedAddress.postcode.replace( ' ', '' ).toUpperCase()
: '';

/**
* Trims and normalizes address data for comparison.
*/
export const normalizeAddress = ( address: BillingOrShippingAddress ) => {
const trimmedAddress = Object.entries( address ).reduce(
( acc, [ key, value ] ) => {
if ( key === 'postcode' ) {
acc[ key as keyof BillingOrShippingAddress ] = value
.replace( ' ', '' )
.toUpperCase();
} else {
acc[ key as keyof BillingOrShippingAddress ] = value.trim();
}
return acc;
},
{} as BillingOrShippingAddress
);
return trimmedAddress;
};

/**
* Does a shallow compare of important address data to determine if the cart needs updating on the server. This takes
* the current and previous address into account, as well as the billing email field.
* Does a shallow compare of all address data to determine if the cart needs updating on the server.
*/
const isAddressDirty = < T extends CartBillingAddress | CartShippingAddress >(
// An object containing all previous address information
Expand All @@ -68,13 +71,12 @@ const isAddressDirty = < T extends CartBillingAddress | CartShippingAddress >(
return true;
}

return (
!! address.country &&
! isShallowEqual(
trimAddress( previousAddress ),
trimAddress( address )
)
const addressMatches = isShallowEqual(
normalizeAddress( previousAddress ),
normalizeAddress( address )
);

return ! addressMatches;
};

type BaseAddressKey = keyof CartBillingAddress | keyof CartShippingAddress;
Expand Down Expand Up @@ -174,7 +176,7 @@ const updateCustomerData = debounce( (): void => {
processErrorResponse( response );
} );
}
}, 1000 );
}, 3000 );

/**
* After cart has fully initialized, pushes changes to the server when data in the store is changed. Updates to the
Expand Down
10 changes: 1 addition & 9 deletions src/StoreApi/Schemas/V1/AbstractAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,7 @@ public function validate_callback( $address, $request, $param ) {
$errors = new \WP_Error();
$address = $this->sanitize_callback( $address, $request, $param );

if ( empty( $address['country'] ) ) {
$errors->add(
'missing_country',
__( 'Country is required', 'woo-gutenberg-products-block' )
);
return $errors;
}

if ( ! in_array( $address['country'], array_keys( wc()->countries->get_countries() ), true ) ) {
if ( ! empty( $address['country'] ) && ! in_array( $address['country'], array_keys( wc()->countries->get_countries() ), true ) ) {
$errors->add(
'invalid_country',
sprintf(
Expand Down