Autofill checkout inputs based on custom post type match / query Autofill checkout inputs based on custom post type match / query wordpress wordpress

Autofill checkout inputs based on custom post type match / query


If you only have a couple of hundred 'sites' then just localize a script.

https://developer.wordpress.org/reference/functions/wp_localize_script/

Then, when you have that CDATA variable available in the DOM, you can just use jQuery/javascript to find the matching job and fill in the form fields. From there, I recommend using the WC documentation and their hooks/filters to add fields to the checkout AND validate those fields on checkout. Here is a link that explains that process as well.

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

Bottom line? If you have a few hundred fields just localize the data in a variable that can be read on the front end. If many more, then you may want to use AJAX. But anything fewer than a couple hundred will actually be less resources used on the server and faster load.


You can do whatever you want using custom code as well as some of your best logic as below I'm going to explaining to you.

Follow the logic/step below.

Create custom field checkout

first of creating a custom field on checkout page woo-commerce : custom field

https://wisdmlabs.com/blog/add-custom-fields-woocommerce-checkout-page/

and also don't forget to add check button

create ajax call ( WP-ajax ) and set-up on a button.

Create ajax call by clicking on a button and get user detail by job id

You have to write code to get the user data from the job_number on the ajax function that is the returning user that matches the ajax function. ( you )

get the user by user meta ( you have to store job_number with user meta to get user detail by job_number) get_user_meta : you will get user id from user meta.

as you will get user id you can easily find the user woocommerce user meta that seems shipping address, billing address, etc. and you suppose to return those data with your custom ajax call and set in the field of woocommerce checkout using jquery.

I hope you getting an idea from my logic and steps.

Thanks.