Set default values for Location & Country for Vendor on /register-vendor page

We capture an Address from a User when they register for our site.

When the user visits the /register-vendor page, they are prompted again to provide a Location (thanks to hivepress-geolocation) and they are prompted for a Country.

Is there a way I can auto-populate these fields with the information I’ve already collected on the User’s Billing Address fields?

I’ve tried something as simple as the following, and this doesn’t seem to work:

// set default values for Vendor Profile page
add_action('hivepress/v1/models/vendor/create', function($vendor_id) {
	update_post_meta($vendor_id, 'hp_country', 'US'); // simple example


The code snippet is correct if you have a custom attribute named country.
But if you have a select attribute, you must do it through the terms, not the meta.

Unfortunately, the location itself cannot be pre-filled, only the text.

Here’s what I ended up having to go with (for anyone else who may follow)

add_action('hivepress/v1/models/vendor/create', function($listing_id) {
	$user = get_userdata( get_current_user_id() );
	$location = $user->billing_address_1 .' '. $user->billing_address_2. ', '. $user->billing_city. ', ' . $user->billing_state. ', '. $user->billing_country;

    $apiKey = get_option('hp_gmaps_api_key'); // Google maps now requires an API key.
	$url = ''.urlencode($location).'&sensor=false&key='.$apiKey;
	// update_post_meta($listing_id, 'debug_url', $url); // throws request URL in DB for debugging

    $geocode=wp_remote_retrieve_body( wp_remote_get( $url ) );
    $output= json_decode($geocode);
	// update_post_meta($listing_id, 'debug_geocode', $geocode); // throws response JSON in DB for debugging
    $latitude = $output->results[0]->geometry->location->lat;
    $longitude = $output->results[0]->geometry->location->lng;
	$ac = $output->results[0]->address_components;
	$hp_location = $ac[1]->long_name . ', ' . $ac[2]->long_name . ', ' . $ac[3]->long_name . ', ' . $ac[4]->long_name . ', ' . $ac[5]->long_name;

	update_post_meta($listing_id, 'hp_country', $ac[5]->short_name); //this field added by hivepress-marketplace

	update_post_meta($listing_id, 'hp_location', $hp_location); // these 3 fields added by hivepress-geolocation
	update_post_meta($listing_id, 'hp_latitude', $latitude);
	update_post_meta($listing_id, 'hp_longitude', $longitude);

In this solution, I’m using Google Maps API, and I had to disable Application Restriction so that the PHP server could make the raw API request. I’d be open to suggestions on how to lock that back down (or any other improvements to this code).

I’m also using hivepress-marketplace with hivepress-geolocation, requiring me to update a bunch of post_meta fields to reduce duplicative data entry.

Thanks for sharing!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.