How to Sync Vendor Location to Listings

Syncing vendor locations with listings is essential for accurate searches and better user experience. Like you, I struggled to sync vendor locations to listings and tried many solutions before finally getting it right. Here’s a detailed guide on how I solved this issue, including the challenges and the code snippet that worked for me.

The Problem

When using the snippet shared in the HivePress community, I noticed an issue:
The code only synced the text of the vendor’s location. As a result, when searching by location, listings wouldn’t appear, even though they had synced locations.

The Initial Code

Here’s the original code I found:

add_filter(
    'hivepress/v1/models/listing/attributes',
    function($attributes) {
        if(isset($attributes['location'])) {
            $attributes['location']['synced'] = true;
        }
        return $attributes;
    },
    1000
);

While this code enabled basic syncing of location text, it didn’t handle geographical coordinates, which are critical for accurate location-based searches.

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

Hello @shibfox , Vanakkam. How can i contact you ? I have a similar problem. And where exactly to add these snippets ?

Vanakkam!

You can add this code to your child theme’s functions.php file to solve your issue.

Feel free to connect with me on Instagram - @shibfox

Hi,

Thank you for sharing this solution.

1 Like

It’s very interesting @shibfox !

What if I want to preset (sync) the listing category with the one from the vendor, instead of geolocation (it’s a digital services marketplace, location does not matter) ?
I tried to customize your code, like so :

add_filter(
    'hivepress/v1/models/listing/attributes',
    function( $attributes ) {
        if ( isset( $attributes['category'] ) ) {
            $attributes['category']['synced'] = true;
        }
        return $attributes;
    },
    1000
);

but it does nothing.

Thanks !

I tried similar code to sync vendors social links. Check it :point_down:
https://community.hivepress.io/t/how-to-sync-vendor-social-links-on-listing-page/13846