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

Hide "collection from" text when a location has an incomplete address. #9808

Merged
merged 8 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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
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 ( 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