Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get the customer address in checkout #9622

Open
webzunft opened this issue Feb 13, 2023 · 2 comments
Open

Add function to get the customer address in checkout #9622

webzunft opened this issue Feb 13, 2023 · 2 comments

Comments

@webzunft
Copy link

There is a lot of logic to get the customer address in

function edd_default_cc_address_fields() {
/**
* Allow the address fields to be replaced.
* @since 3.1
*/
if ( null !== apply_filters( 'edd_pre_cc_address_fields', null ) ) {
do_action( 'edd_cc_address_fields' );
return;
}
$logged_in = is_user_logged_in();
$customer = EDD()->session->get( 'customer' );
$customer = wp_parse_args( $customer, array(
'address' => array(
'line1' => '',
'line2' => '',
'city' => '',
'zip' => '',
'state' => '',
'country' => '',
),
) );
$customer['address'] = array_map( 'sanitize_text_field', $customer['address'] );
if ( $logged_in ) {
$user_address = edd_get_customer_address();
foreach ( $customer['address'] as $key => $field ) {
if ( empty( $field ) && ! empty( $user_address[ $key ] ) ) {
$customer['address'][ $key ] = $user_address[ $key ];
} else {
$customer['address'][ $key ] = '';
}
}
}
/**
* Filter the billing address details that will be pre-populated on the checkout form..
*
* @since 2.8
*
* @param array $address The customer address.
* @param array $customer The customer data from the session
*/
$customer['address'] = apply_filters( 'edd_checkout_billing_details_address', $customer['address'], $customer );

I would like to have these moved into a function in /includes/checkout/functions.php so that we can get the customer address in the checkout in our custom code without copying this logic from the template.

@slr1979
Copy link

slr1979 commented Jul 8, 2023

@webzunft I currently use this to put address fields in the checkout and have done for many years. Not sure if there is a better way to do it but this works fine. Will display for users who are registered and those who need to.

function edd_add_billing_address_to_checkout() {
        if( ! did_action( 'edd_after_cc_fields', 'edd_default_cc_address_fields' ) ) {
		edd_default_cc_address_fields();
	}
}
add_action( 'edd_purchase_form_after_user_info', 'edd_add_billing_address_to_checkout' );
add_action( 'edd_register_fields_before', 'edd_add_billing_address_to_checkout' );

@webzunft
Copy link
Author

@slr1979 Thanks for sharing your solution. I don’t want to display the address visually in the checkout, but use it with some custom code in there. I just thought, that the logic that EDD uses here would do great in a new function so that every developer can use it without repeating the same code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants