2022-02-22 17:45:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
defaultAddressFields,
|
|
|
|
AddressFields,
|
|
|
|
ShippingAddress,
|
|
|
|
BillingAddress,
|
2022-10-07 07:50:26 +00:00
|
|
|
getSetting,
|
2022-02-22 17:45:01 +00:00
|
|
|
} from '@woocommerce/settings';
|
|
|
|
import { useCallback } from '@wordpress/element';
|
2022-06-10 16:33:15 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
|
2022-02-22 17:45:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useCustomerData } from './use-customer-data';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { useShippingData } from './shipping/use-shipping-data';
|
2022-02-22 17:45:01 +00:00
|
|
|
|
|
|
|
interface CheckoutAddress {
|
|
|
|
shippingAddress: ShippingAddress;
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress: BillingAddress;
|
2022-10-06 14:48:52 +00:00
|
|
|
setShippingAddress: ( data: Partial< ShippingAddress > ) => void;
|
|
|
|
setBillingAddress: ( data: Partial< BillingAddress > ) => void;
|
2022-02-22 17:45:01 +00:00
|
|
|
setEmail: ( value: string ) => void;
|
|
|
|
setBillingPhone: ( value: string ) => void;
|
|
|
|
setShippingPhone: ( value: string ) => void;
|
|
|
|
useShippingAsBilling: boolean;
|
|
|
|
setUseShippingAsBilling: ( useShippingAsBilling: boolean ) => void;
|
|
|
|
defaultAddressFields: AddressFields;
|
|
|
|
showShippingFields: boolean;
|
|
|
|
showBillingFields: boolean;
|
2022-10-07 07:50:26 +00:00
|
|
|
forcedBillingAddress: boolean;
|
2022-12-23 15:05:56 +00:00
|
|
|
useBillingAsShipping: boolean;
|
2023-01-12 11:43:34 +00:00
|
|
|
needsShipping: boolean;
|
2023-01-12 11:55:54 +00:00
|
|
|
showShippingMethods: boolean;
|
2022-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom hook for exposing address related functionality for the checkout address form.
|
|
|
|
*/
|
|
|
|
export const useCheckoutAddress = (): CheckoutAddress => {
|
2022-03-04 17:43:45 +00:00
|
|
|
const { needsShipping } = useShippingData();
|
2022-10-06 12:00:30 +00:00
|
|
|
const { useShippingAsBilling, prefersCollection } = useSelect(
|
|
|
|
( select ) => ( {
|
|
|
|
useShippingAsBilling:
|
|
|
|
select( CHECKOUT_STORE_KEY ).getUseShippingAsBilling(),
|
|
|
|
prefersCollection: select( CHECKOUT_STORE_KEY ).prefersCollection(),
|
|
|
|
} )
|
2022-06-10 16:33:15 +00:00
|
|
|
);
|
2022-10-05 10:04:16 +00:00
|
|
|
const { __internalSetUseShippingAsBilling } =
|
|
|
|
useDispatch( CHECKOUT_STORE_KEY );
|
2022-02-22 17:45:01 +00:00
|
|
|
const {
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
|
|
|
setBillingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
} = useCustomerData();
|
|
|
|
|
|
|
|
const setEmail = useCallback(
|
|
|
|
( value ) =>
|
2022-06-10 16:59:25 +00:00
|
|
|
void setBillingAddress( {
|
2022-02-22 17:45:01 +00:00
|
|
|
email: value,
|
|
|
|
} ),
|
2022-06-10 16:59:25 +00:00
|
|
|
[ setBillingAddress ]
|
2022-02-22 17:45:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setBillingPhone = useCallback(
|
|
|
|
( value ) =>
|
2022-06-10 16:59:25 +00:00
|
|
|
void setBillingAddress( {
|
2022-02-22 17:45:01 +00:00
|
|
|
phone: value,
|
|
|
|
} ),
|
2022-06-10 16:59:25 +00:00
|
|
|
[ setBillingAddress ]
|
2022-02-22 17:45:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const setShippingPhone = useCallback(
|
|
|
|
( value ) =>
|
|
|
|
void setShippingAddress( {
|
|
|
|
phone: value,
|
|
|
|
} ),
|
|
|
|
[ setShippingAddress ]
|
|
|
|
);
|
2022-10-07 07:50:26 +00:00
|
|
|
const forcedBillingAddress: boolean = getSetting(
|
|
|
|
'forcedBillingAddress',
|
|
|
|
false
|
|
|
|
);
|
2022-02-22 17:45:01 +00:00
|
|
|
return {
|
|
|
|
shippingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
setShippingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
setBillingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
setEmail,
|
|
|
|
setBillingPhone,
|
|
|
|
setShippingPhone,
|
|
|
|
defaultAddressFields,
|
|
|
|
useShippingAsBilling,
|
2022-10-05 10:04:16 +00:00
|
|
|
setUseShippingAsBilling: __internalSetUseShippingAsBilling,
|
2023-01-12 11:43:34 +00:00
|
|
|
needsShipping,
|
2022-10-06 12:00:30 +00:00
|
|
|
showShippingFields:
|
|
|
|
! forcedBillingAddress && needsShipping && ! prefersCollection,
|
2023-01-12 11:55:54 +00:00
|
|
|
showShippingMethods: needsShipping && ! prefersCollection,
|
2022-10-06 12:00:30 +00:00
|
|
|
showBillingFields:
|
|
|
|
! needsShipping || ! useShippingAsBilling || prefersCollection,
|
2022-10-07 07:50:26 +00:00
|
|
|
forcedBillingAddress,
|
2022-12-23 15:05:56 +00:00
|
|
|
useBillingAsShipping: forcedBillingAddress || prefersCollection,
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
};
|