How to Sync Vendor Location to Listings

The Solution

To fix this issue, I modified the code to include syncing of latitude and longitude, ensuring that vendor locations were properly mapped.

1) Before applying the code, make sure to enable both Listings and Vendors in the Geolocation settings. :point_down:

2) Here’s the updated snippet:

add_filter(
    'hivepress/v1/models/listing/attributes',
    function( $attributes ) {
        if ( isset( $attributes['location'] ) ) {
            $attributes['location']['synced'] = true;
            // Explicitly define syncing for latitude and longitude
            $attributes['latitude']['synced'] = true;
            $attributes['longitude']['synced'] = true;
        }

        return $attributes;
    },
    1000
);

Why This Works

Latitude and Longitude Syncing: By adding $attributes['latitude']['synced'] = true; and $attributes['longitude']['synced'] = true;, the location data now includes geographical coordinates, making search results accurate.

Vendors Settings :point_down:

Listing Submit Form :point_down:

Bonus Tip

Restricting Vendors from Editing Locations

If you want to restrict vendors from changing the synced location while listing, you can use this line:

$attributes['location']['editable'] = false; // Prevent editing

This will hide the location field on the listing submission page, ensuring that vendors can only use their registered location.

Final Thoughts

With this updated snippet, syncing vendor locations to listings works perfectly, and searches by location now display accurate results. If you’ve faced similar challenges, this snippet should solve the problem for you.

Enjoy the code and happy syncing! :wink:

3 Likes