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

Commit

Permalink
Hide "collection from" text when a location has an incomplete address. (
Browse files Browse the repository at this point in the history
#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 <[email protected]>
  • Loading branch information
mikejolley and nielslange authored Jul 3, 2023
1 parent bbea406 commit 6e9b47c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
14 changes: 3 additions & 11 deletions assets/js/base/components/cart-checkout/pickup-location/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -36,28 +36,22 @@ const PickupLocation = (): JSX.Element | null => {
const selectedRatePickupAddress = selectedRateMetaData.value;
return {
pickupAddress: selectedRatePickupAddress,
pickupMethod: selectedCollectableRate.name,
};
}
}

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;
}

Expand All @@ -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>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -87,7 +87,7 @@ describe( 'PickupLocation', () => {

render( <PickupLocation /> );
expect(
screen.getByText( /Collection from Local pickup/ )
).toBeInTheDocument();
screen.queryByText( /Collection from / )
).not.toBeInTheDocument();
} );
} );
58 changes: 57 additions & 1 deletion src/Shipping/PickupLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
*/
class PickupLocation extends WC_Shipping_Method {

/**
* Pickup locations.
*
* @var array
*/
protected $pickup_locations = [];

/**
* Cost
*
* @var string
*/
protected $cost = '';

/**
* Constructor.
*/
Expand All @@ -31,6 +45,48 @@ public function init() {
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.
*
Expand All @@ -51,7 +107,7 @@ public function calculate_shipping( $package = array() ) {
'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'] ),
),
)
Expand Down
6 changes: 5 additions & 1 deletion src/Shipping/ShippingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ public function show_local_pickup_details( $return, $order ) {
$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;
}
Expand Down

0 comments on commit 6e9b47c

Please sign in to comment.