Skip to content

Commit ca3b964

Browse files
committed
fix: old detection of an address being previously set not working
fix: billing address not set on the cart also refactor M2-869 refactor: simplify billing/checkout logic refactor: refactor shipping and billing
1 parent b8bfba4 commit ca3b964

File tree

6 files changed

+174
-221
lines changed

6 files changed

+174
-221
lines changed

packages/theme/helpers/checkout/address.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isEqual } from 'lodash-es';
12
import type { CustomerAddress, CartAddressInterface } from '~/modules/GraphQL/types';
23

34
export const formatAddressReturnToData = (address: CartAddressInterface) => ({
@@ -53,3 +54,23 @@ export const getInitialCheckoutAddressForm = () : CheckoutAddressForm => ({
5354
postcode: '',
5455
telephone: '',
5556
});
57+
58+
/**
59+
* Try to find an address from the user's saved addresses that exactly matches the address that is bound to a cart.
60+
*
61+
* `useShipping().save()``sends an addressId to Magento to set the shipping address on the cart,
62+
* but when you download the cart after that - the cart's endpoint response doesn't contain that addressId, just the address fields (street etc.)
63+
* So the only choice left is to try to compare the fields of the addresses.
64+
*
65+
* This function exists because if a user returns to a cart whose shipping address was set before, we want the user address to be highlighted in the SfAddressPicker component.
66+
*
67+
* @param customerAddresses The addresses saved in a user's account
68+
* @param cartAddress The address that is bound to the cart, @see Cart["billing_address"] Cart["shipping_addresses"]
69+
*
70+
*/
71+
export const findUserAddressIdenticalToSavedCartAddress = (
72+
customerAddresses: CustomerAddress[] | null,
73+
cartAddress: CartAddressInterface,
74+
) : CustomerAddress => customerAddresses?.find(
75+
(userAddress) => isEqual(addressFromApiToForm(userAddress), addressFromApiToForm(cartAddress)),
76+
) ?? null;

packages/theme/modules/checkout/components/UserBillingAddresses.vue

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<div>
33
<SfAddressPicker
4-
:selected="`${selectedAddress}`"
4+
:selected="`${currentAddressId}`"
55
class="addresses"
6-
@change="setCurrentAddress($event)"
6+
@change="emitSetCurrentAddress($event)"
77
>
88
<SfAddress
99
v-for="billingAddress in addressesWithCountryName"
@@ -46,8 +46,8 @@ export default defineComponent({
4646
},
4747
props: {
4848
currentAddressId: {
49-
type: [String, Number],
50-
required: true,
49+
type: Number,
50+
default: null,
5151
},
5252
value: {
5353
type: Boolean,
@@ -65,21 +65,12 @@ export default defineComponent({
6565
},
6666
emits: ['setCurrentAddress'],
6767
setup(props, { emit }) {
68-
const setCurrentAddress = (addressId: string) => {
69-
const selectedAddress = props.billingAddresses.find((address) => address.id === Number(addressId));
70-
if (!selectedAddress) {
71-
return;
68+
const emitSetCurrentAddress = (addressId: number) => {
69+
const address = props.billingAddresses.find(({ id }) => id === Number(addressId));
70+
if (address) {
71+
emit('setCurrentAddress', address);
7272
}
73-
74-
emit('setCurrentAddress', selectedAddress);
7573
};
76-
77-
const selectedAddress = computed(() => (
78-
props.currentAddressId
79-
? props.currentAddressId
80-
: props.billingAddresses.find((address) => address.default_billing)?.id ?? ''
81-
));
82-
8374
const addressesWithCountryName = computed(() => props.billingAddresses.map((address) => ({
8475
...address,
8576
countryName: props.countries
@@ -89,8 +80,7 @@ export default defineComponent({
8980
})));
9081
9182
return {
92-
selectedAddress,
93-
setCurrentAddress,
83+
emitSetCurrentAddress,
9484
addressesWithCountryName,
9585
userBillingGetters,
9686
};

packages/theme/modules/checkout/components/UserShippingAddresses.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<SfAddressPicker
44
:selected="`${currentAddressId}`"
55
class="addresses"
6-
@change="setCurrentAddress($event)"
6+
@change="emitSetCurrentAddress($event)"
77
>
88
<SfAddress
99
v-for="shippingAddress in addressesWithCountryName"
@@ -49,8 +49,8 @@ export default defineComponent({
4949
},
5050
props: {
5151
currentAddressId: {
52-
type: [String, Number],
53-
required: true,
52+
type: Number,
53+
default: null,
5454
},
5555
value: {
5656
type: Boolean,
@@ -68,15 +68,12 @@ export default defineComponent({
6868
},
6969
emits: ['setCurrentAddress'],
7070
setup(props, { emit }) {
71-
const setCurrentAddress = (addressId: string | number) => {
72-
const selectedAddress = props.shippingAddresses.find((address) => address.id === Number(addressId));
73-
if (!selectedAddress) {
74-
return;
71+
const emitSetCurrentAddress = (addressId: number) => {
72+
const address = props.shippingAddresses.find(({ id }) => id === Number(addressId));
73+
if (address) {
74+
emit('setCurrentAddress', address);
7575
}
76-
77-
emit('setCurrentAddress', selectedAddress);
7876
};
79-
8077
const addressesWithCountryName = computed(() => props.shippingAddresses.map((address) => ({
8178
...address,
8279
countryName: props.countries
@@ -86,7 +83,7 @@ export default defineComponent({
8683
})));
8784
8885
return {
89-
setCurrentAddress,
86+
emitSetCurrentAddress,
9087
addressesWithCountryName,
9188
userShippingGetters,
9289
};

0 commit comments

Comments
 (0)