Hide "collection from" text when a location has an incomplete address. (https://github.com/woocommerce/woocommerce-blocks/pull/9808)
* Hide "collection from" text when a location has an incomplete address. * Fix display on confirmation page * has_valid_pickup_location helper * Missing isset * Update test * Fix pickup text assertion --------- Co-authored-by: Niels Lange <info@nielslange.de>
This commit is contained in:
parent
c1f2949a5d
commit
a6b8e0e542
|
@ -10,7 +10,7 @@ import { isPackageRateCollectable } from '@woocommerce/base-utils';
|
|||
* Shows a formatted pickup location.
|
||||
*/
|
||||
const PickupLocation = (): JSX.Element | null => {
|
||||
const { pickupAddress, pickupMethod } = useSelect( ( select ) => {
|
||||
const { pickupAddress } = useSelect( ( select ) => {
|
||||
const cartShippingRates = select( 'wc/store/cart' ).getShippingRates();
|
||||
|
||||
const flattenedRates = cartShippingRates.flatMap(
|
||||
|
@ -36,7 +36,6 @@ const PickupLocation = (): JSX.Element | null => {
|
|||
const selectedRatePickupAddress = selectedRateMetaData.value;
|
||||
return {
|
||||
pickupAddress: selectedRatePickupAddress,
|
||||
pickupMethod: selectedCollectableRate.name,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -44,20 +43,15 @@ const PickupLocation = (): JSX.Element | null => {
|
|||
if ( isObject( selectedCollectableRate ) ) {
|
||||
return {
|
||||
pickupAddress: undefined,
|
||||
pickupMethod: selectedCollectableRate.name,
|
||||
};
|
||||
}
|
||||
return {
|
||||
pickupAddress: undefined,
|
||||
pickupMethod: undefined,
|
||||
};
|
||||
} );
|
||||
|
||||
// If the method does not contain an address, or the method supporting collection was not found, return early.
|
||||
if (
|
||||
typeof pickupAddress === 'undefined' &&
|
||||
typeof pickupMethod === 'undefined'
|
||||
) {
|
||||
if ( typeof pickupAddress === 'undefined' ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -67,9 +61,7 @@ const PickupLocation = (): JSX.Element | null => {
|
|||
{ sprintf(
|
||||
/* translators: %s: shipping method name, e.g. "Amazon Locker" */
|
||||
__( 'Collection from %s', 'woo-gutenberg-products-block' ),
|
||||
typeof pickupAddress === 'undefined'
|
||||
? pickupMethod
|
||||
: pickupAddress
|
||||
pickupAddress
|
||||
) + ' ' }
|
||||
</span>
|
||||
);
|
||||
|
|
|
@ -26,7 +26,7 @@ jest.mock( '@woocommerce/settings', () => {
|
|||
};
|
||||
} );
|
||||
describe( 'PickupLocation', () => {
|
||||
it( `renders an address if one is set in the method's metadata`, async () => {
|
||||
it( `renders an address if one is set in the methods metadata`, async () => {
|
||||
dispatch( CHECKOUT_STORE_KEY ).setPrefersCollection( true );
|
||||
|
||||
// Deselect the default selected rate and select pickup_location:1 rate.
|
||||
|
@ -54,7 +54,7 @@ describe( 'PickupLocation', () => {
|
|||
)
|
||||
).toBeInTheDocument();
|
||||
} );
|
||||
it( 'renders the method name if address is not in metadata', async () => {
|
||||
it( 'renders no address if one is not set in the methods metadata', async () => {
|
||||
dispatch( CHECKOUT_STORE_KEY ).setPrefersCollection( true );
|
||||
|
||||
// Deselect the default selected rate and select pickup_location:1 rate.
|
||||
|
@ -87,7 +87,7 @@ describe( 'PickupLocation', () => {
|
|||
|
||||
render( <PickupLocation /> );
|
||||
expect(
|
||||
screen.getByText( /Collection from Local pickup/ )
|
||||
).toBeInTheDocument();
|
||||
screen.queryByText( /Collection from / )
|
||||
).not.toBeInTheDocument();
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -8,6 +8,20 @@ use WC_Shipping_Method;
|
|||
*/
|
||||
class PickupLocation extends WC_Shipping_Method {
|
||||
|
||||
/**
|
||||
* Pickup locations.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $pickup_locations = [];
|
||||
|
||||
/**
|
||||
* Cost
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $cost = '';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
|
@ -31,6 +45,48 @@ class PickupLocation extends WC_Shipping_Method {
|
|||
add_filter( 'woocommerce_attribute_label', array( $this, 'translate_meta_data' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given address is complete.
|
||||
*
|
||||
* @param array $address Address.
|
||||
* @return bool
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate shipping.
|
||||
*
|
||||
|
@ -51,7 +107,7 @@ class PickupLocation extends WC_Shipping_Method {
|
|||
'cost' => $this->cost,
|
||||
'meta_data' => array(
|
||||
'pickup_location' => wp_kses_post( $location['name'] ),
|
||||
'pickup_address' => wc()->countries->get_formatted_address( $location['address'], ', ' ),
|
||||
'pickup_address' => $this->has_valid_pickup_location( $location['address'] ) ? wc()->countries->get_formatted_address( $location['address'], ', ' ) : '',
|
||||
'pickup_details' => wp_kses_post( $location['details'] ),
|
||||
),
|
||||
)
|
||||
|
|
|
@ -135,9 +135,13 @@ class ShippingController {
|
|||
$location = $shipping_method->get_meta( 'pickup_location' );
|
||||
$address = $shipping_method->get_meta( 'pickup_address' );
|
||||
|
||||
if ( ! $address ) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
// Translators: %s location name.
|
||||
__( 'Pickup from <strong>%s</strong>:', 'woo-gutenberg-products-block' ),
|
||||
__( 'Collection from <strong>%s</strong>:', 'woo-gutenberg-products-block' ),
|
||||
$location
|
||||
) . '<br/><address>' . str_replace( ',', ',<br/>', $address ) . '</address><br/>' . $details;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue