Vendor Listing by Location

Solution provided by Randall – http://www.wcvendors.com/help/topic/sort-vendor-by-shop-setting-field/

Overall, here is the concept. I needed a shortcode that would allow me to separate vendors by location. To do this, I required users to select a location during their registration and in their store settings. Then I pulled that meta info into a shortcode which filtered and displayed only those vendors.

Here is an example: [wc_vendors_location location=”Alabama”]

So here is how I set everything up. I use WP User Frontend Pro. Found here. This is important because that is how I attached the location meta to the vendor. So, here we go.

First I went into the registration form designated for my vendors settings page. (Please refer to this post here for that form) I added a dropdown with all the locations I needed my vendors to select. This is/was the easiest part.

Next I created this code:

/**
  *     list of vendors by location 
  * 
  *     @param $atts shortcode attributs 
*/
function wc_vendors_location( $atts ) {
    $html=""; 

    extract( shortcode_atts( array(
            'orderby'       => 'display_name',
            'order'         => 'ASC',
            'per_page'      => '12',
            'columns'       => '4', 
            'show_products' => 'yes',
            'location'      => '',
            'org'           => '',
        ), $atts ) );
    // Hook into the user query to modify the query to return users that have at least one product 
    if ($show_products == 'yes') remove_action( 'pre_user_query', array( $this, 'vendors_with_products') );
    // Get all vendors 
    $vendor_total_args = array ( 
        'role'              => 'vendor', 
        'meta_key'          => 'pv_shop_slug', 
        'meta_value'        => '',
        'meta_key'          => 'location', 
        'meta_value'        => $location,
        'meta_key'          => 'organization', 
        'meta_value'        => $org,
        'orderby'           => $orderby,
        'order'             => $order,
    );
    if ($show_products == 'yes') $vendor_total_args['query_id'] = 'vendors_with_products'; 
    $vendor_query = New WP_User_Query( $vendor_total_args ); 
    $all_vendors =$vendor_query->get_results(); 

    ob_start();
    // Loop through all vendors and output a simple link to their vendor pages
    foreach ($all_vendors as $vendor) {
       wc_get_template( 'vendor-list.php', array(
                                            'shop_link'         => WCV_Vendors::get_vendor_shop_page($vendor->ID), 
                                                    'shop_name'         => $vendor->pv_shop_name, 
                                                    'vendor_id'         => $vendor->ID, 
                                                    'shop_description'  => $vendor->pv_shop_description, 
                                            ), 'wc-vendors/front/', wcv_plugin_dir . 'templates/front/' );
    } // End foreach 

    $html .= '<ul class="wcv_vendorslist">' . ob_get_clean() . '</ul>';
    return $html; 
}
add_shortcode('wc_vendors_location', 'wc_vendors_location');

/* END */

I placed that in my child theme’s functions.php.

To give you a quick breakdown of how that worked, I simply copied the shortcode for showing ALL vendors (See posts above). I then took out the pieces that related to pagination (I didn’t need it, you might).

Then under the – “extract ( shortcode_atts……”” I added – ‘location’ => ”,

This is responsible for the part of the shortcode that can be adjusted.

[wc_vendors_location location="filtered piece"]

Then I added in the arguments needed for when this shortcode searches for vendors. Under “$vendor_total_args = array…..”

I added:

‘meta_key’ => ‘location’,
‘meta_value’ => $location,

meta_key is telling it to look at the meta information “location” but not what that actual word is. That is handled by meta_value. That is why both are needed.

Once those are both inserted, I simply deleted the pagination as I don’t have that many vendors, but you might want to leave it in if you need it. If that is the case, don’t forget to add your meta keys and values there.

Ultimately, this ended up working for me. I by no means say I am an expert in php. I have enough knowledge to get me into trouble. So please, if something doesn’t work for you, lets figure it out!

Also, if you don’t have WP User Frontend, you can refer to this post to create your own area to fill out. (http://www.wcvendors.com/help/topic/manipulating-the-vendors-shop-settings/)

I really hope this helps someone and that you all have a great day!

Thanks!
Randall