Merge pull request #452 from woocommerce/pcp-443-address

Fix shipping address handling (443)
This commit is contained in:
Emili Castells 2022-02-01 10:28:12 +01:00 committed by GitHub
commit ade654189a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 33 deletions

View file

@ -198,14 +198,20 @@ class OrderEndpoint {
return $is_purchase_unit;
}
);
$shipping_preferences = $contains_physical_goods
? $shipping_address_is_fixed ?
ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS
: ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE
: ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING;
if ( $this->has_items_without_shipping( $items ) ) {
$shipping_preferences = ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING;
$shipping_preference = ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING;
if ( $contains_physical_goods ) {
if ( $shipping_address_is_fixed ) {
// Checkout + no address given? Probably something weird happened, like no form validation?
// Also note that $items currently always seems to be an array with one item.
if ( $this->has_items_without_shipping( $items ) ) {
$shipping_preference = ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING;
} else {
$shipping_preference = ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS;
}
} else {
$shipping_preference = ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE;
}
}
$bearer = $this->bearer->bearer();
@ -218,7 +224,7 @@ class OrderEndpoint {
$items
),
'application_context' => $this->application_context_repository
->current_context( $shipping_preferences )->to_array(),
->current_context( $shipping_preference )->to_array(),
);
if ( $payer ) {
$data['payer'] = $payer->to_array();
@ -591,7 +597,7 @@ class OrderEndpoint {
/**
* Checks if there is at least one item without shipping.
*
* @param array $items The items.
* @param PurchaseUnit[] $items The items.
* @return bool Whether items contains shipping or not.
*/
private function has_items_without_shipping( array $items ): bool {

View file

@ -143,13 +143,15 @@ class Address {
* @return array
*/
public function to_array(): array {
return array(
'country_code' => $this->country_code(),
'address_line_1' => $this->address_line_1(),
'address_line_2' => $this->address_line_2(),
'admin_area_1' => $this->admin_area_1(),
'admin_area_2' => $this->admin_area_2(),
'postal_code' => $this->postal_code(),
return array_filter(
array(
'country_code' => $this->country_code(),
'address_line_1' => $this->address_line_1(),
'address_line_2' => $this->address_line_2(),
'admin_area_1' => $this->admin_area_1(),
'admin_area_2' => $this->admin_area_2(),
'postal_code' => $this->postal_code(),
)
);
}
}

View file

@ -112,7 +112,7 @@ class PurchaseUnitFactory {
if (
! $this->shipping_needed( ... array_values( $items ) ) ||
empty( $shipping->address()->country_code() ) ||
( $shipping->address()->country_code() && ! $shipping->address()->postal_code() )
( ! $shipping->address()->postal_code() && ! $this->country_without_postal_code( $shipping->address()->country_code() ) )
) {
$shipping = null;
}
@ -121,7 +121,6 @@ class PurchaseUnitFactory {
$payee = $this->payee_repository->payee();
$custom_id = (string) $order->get_id();
$invoice_id = $this->prefix . $order->get_order_number();
$retry = $order->get_meta( 'ppcp-retry' ) ? '-' . $order->get_meta( 'ppcp-retry' ) : '';
$soft_descriptor = '';
$purchase_unit = new PurchaseUnit(
@ -158,9 +157,8 @@ class PurchaseUnitFactory {
if ( $this->shipping_needed( ... array_values( $items ) ) && is_a( $customer, \WC_Customer::class ) ) {
$shipping = $this->shipping_factory->from_wc_customer( \WC()->customer );
if (
2 !== strlen( $shipping->address()->country_code() )
|| ( ! $shipping->address()->postal_code() )
|| $this->country_without_postal_code( $shipping->address()->country_code() )
2 !== strlen( $shipping->address()->country_code() ) ||
( ! $shipping->address()->postal_code() && ! $this->country_without_postal_code( $shipping->address()->country_code() ) )
) {
$shipping = null;
}
@ -276,9 +274,6 @@ class PurchaseUnitFactory {
*/
private function country_without_postal_code( string $country_code ): bool {
$countries = array( 'AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW' );
if ( in_array( $country_code, $countries, true ) ) {
return true;
}
return false;
return in_array( $country_code, $countries, true );
}
}