Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Tijmen Smit

    (@tijmensmit)

    Let me know if your comfortable with code, then I can point you in the right direction how to make this work.

    Thread Starter Ordog

    (@ordog)

    Hi Tijmen,

    Yeah I have a reasonable understanding. I implemented your code from your well written guide that I linked to in my first post and then modified this to work with the Advanced Custom Fields instead.

    So hopefully I should be ok with this too ??

    Many thanks

    Thread Starter Ordog

    (@ordog)

    Hi @tijmensmit,

    I just wondered if you’ve managed to find the time to take a look at this for me please.

    Much appreciated

    Thread Starter Ordog

    (@ordog)

    Hi @tijmensmit

    Apologies for pushing for this but it has been over 2 weeks now and I can see you’ve been helping others (as recent as 2 hours ago) but there has been no update on this from your last comment.

    Your help would be very much appreciated.

    Kind regards

    Plugin Author Tijmen Smit

    (@tijmensmit)

    Sorry for not responding before, I only got a notification for your last response.

    The first thing you need to do is make sure you have included a CSS class as explained here.

    Next use wp_enqueue_script to load a custom script, in the script use ajaxComplete to run code after a search is complete that listens for changes to change to a checkbox in your custom template.

    The code inside ajaxComplete should look for the custom CSS class, and based on the above checkbox show / hide it.

    Thread Starter Ordog

    (@ordog)

    Thank you @tijmensmit and don’t worry about the delay – I appreciate you taking the time to come on here and help.

    All of what you’ve said makes absolute sense however I don’t think I explained it very well.

    I don’t want to toggle the style of featured stores. I’d like a dropdown box on the frontend that will show featured stores at the top of the results list by default and then the option of changing the results to be distance only.

    So going off what you’ve said, inside my ajaxComplete function I’d need something like:

    
    $(document).ajaxComplete(function () {
      	
    	$("#dropdown").change(function () { 
    		if($(this).val() == 'featured_first'){
    			// Put featured stores at the top
    		} else {
    			// Sort by distance (featured stores appear in normal distance order)
    		}
    	});
    	
    });
    

    I can see that the function for sorting by featured first is as follows, so I just need a way of merging the two together somehow.

    
    add_filter( 'wpsl_store_data', 'custom_store_data_sort' );
    
    function custom_store_data_sort( $stores ) {
    	
        $premium_list = array();
    
        foreach ( $stores as $k => $store ) {
    
                // Check if the location is a premium one.
                if ( isset( $store['featured'] ){
    
                    // Move the premium location to a new array
                    $premium_list[] = $store;
    
                    // Remove it from the existing $stores array
                    unset( $stores[$k] );
                }
        }
    
        /**
         * Merge the list of premium locations with the existing list.
         * This will make sure the premium location show up before the normal locations.
         */
        $results = array_merge( $premium_list, $stores );
    
        return $results;
    
    }
    

    I hope this makes sense.

    Plugin Author Tijmen Smit

    (@tijmensmit)

    I don’t have a working code snippet showing how to do this, but this and this article should point you in the right direction how to make it work.

    Edit, you may for the distance short want to add the distance itself as a data attribute on the li element so you can use that value to sort on when you want to restore it.

    Or keep two copies of the li list in two different vars, one with the distance sorting, and one with the features stores.

    • This reply was modified 3 years, 6 months ago by Tijmen Smit.
    • This reply was modified 3 years, 6 months ago by Tijmen Smit.
    Thread Starter Ordog

    (@ordog)

    Ohhh I see why you mentioned adding classes now. That’s a far better solution compared to the overengineered one I was trying to do.

    Thank you very much for your time and help with this.

    Thread Starter Ordog

    (@ordog)

    Hi @tijmensmit

    I just wanted to follow up on this to let you know that I’ve now done what I needed thanks to your help.

    In the end I went with the data attribute as you suggested. One for distance and one that tracks the original order. Then I had a function that when the dropdown changes it either sorts by distance or sorts stores back into their original order.

    Your support has been very much appreciated, thank you once again.

    Plugin Author Tijmen Smit

    (@tijmensmit)

    Happy to hear it’s resolved now ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Toggle Featured Stores’ is closed to new replies.