mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
✨ New hook to return AXO billing details
This commit is contained in:
parent
9234a833dd
commit
e9a3c0bb6d
1 changed files with 46 additions and 0 deletions
|
@ -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 ] );
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue