New hook to return AXO billing details

This commit is contained in:
Philipp Stracker 2024-09-11 16:04:08 +02:00
parent 9234a833dd
commit e9a3c0bb6d
No known key found for this signature in database

View file

@ -1,5 +1,6 @@
import { useCallback, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
export const useCustomerData = () => {
const customerData = useSelect( ( select ) =>
@ -40,3 +41,48 @@ export const useCustomerData = () => {
]
);
};
export const useTokenizeCustomerData = () => {
const customerData = useSelect( ( select ) =>
select( 'wc/store/cart' ).getCustomerData()
);
const isValidAddress = ( address ) => {
// At least one name must be present.
if ( ! address.first_name && ! address.last_name ) {
return false;
}
// Street, city, postcode, country are mandatory; state is optional.
return (
address.address_1 &&
address.city &&
address.postcode &&
address.country
);
};
// Memoize the customer data to avoid unnecessary re-renders (and potential infinite loops).
return useMemo( () => {
const { billingAddress, shippingAddress } = customerData;
// Prefer billing address, but fallback to shipping address if billing address is not valid.
const mainAddress = isValidAddress( billingAddress )
? billingAddress
: shippingAddress;
return {
cardholderName: {
fullName: `${ mainAddress.first_name } ${ mainAddress.last_name }`,
},
billingAddress: {
addressLine1: mainAddress.address_1,
addressLine2: mainAddress.address_2,
adminArea1: mainAddress.state,
adminArea2: mainAddress.city,
postalCode: mainAddress.postcode,
countryCode: mainAddress.country,
},
};
}, [ customerData ] );
};