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

    (@tijmensmit)

    Are they logged in WP? If so, then you could add an extra field in the wpsl stores db field holding the user id that is allowed to see a specific selection of stores.

    Then modify the SQL query in /frontend/wpsl-ajax-functions.php so that the results are filtered based on the current user id. That way user 1 only gets to see stores that are assigned to user 1.

    Because you are changing core files, it will break once you upgrade in the future.

    Thread Starter lukem15

    (@lukem15)

    Hi Tijmen, thank you for your reply. I spent all yesterday and today trying to figure this out – it’s driving me crazy! What you suggested works great. However, generally they’re not logged in. Instead I want them to be able to filter the results by clicking a button.

    For example in the default template I’ve added a form with a hidden text field with value 1 and a submit button. If submit is pressed then that will filter all the results with the database value of 1. To filter the results I’ve added a simple modification to wpsl-ajax-functions.php:

    $tester = $_POST['listing-filter'];
    
         SELECT *, ( $distance_unit * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) )
                                        AS distance FROM $wpdb->wpsl_stores       WHERE active = 1 && user = '$tester'
                                        $sql_part
                                        ",
                                        $placeholders

    But this does not work, I cannot figure out why – this is where I’ve spent several hours trying to make this work! I’d highly appreciate your help?

    Thread Starter lukem15

    (@lukem15)

    I beginning to think this isn’t possible? I’ve been trying to think of ways to make it work but nothing is working correctly.

    I’m guessing there is something overwriting my $_POST – I’ve also tried using $_GET but still doesn’t work.

    Can anyone help please?

    Plugin Author Tijmen Smit

    (@tijmensmit)

    The SQL query uses a prepared statement, so you can’t just add the user = $tester in the query.

    Assuming it’s a string you need to set a placeholder like %s, and then include the $tester value in the placeholder array.

    You can find how to show SQL errors here.
    https://codex.www.ads-software.com/Class_Reference/wpdb#Show_and_Hide_SQL_Errors
    They will show in the console in your browser.

    Using the console in your browser also allows you to check values that are sent with the AJAX post.

    Thread Starter lukem15

    (@lukem15)

    Thank you Tijmen for you reply. Something like this? – still not working.

    } else {
            $max_results = ( isset( $_GET['max_results'] ) ) ? $_GET['max_results'] : '';
            if ( ( $max_results == 'NaN' ) || ( !$max_results ) ) {
                $max_results = get_default_list_value( $type = 'max_results' );
            }
                    $tester = $_GET['myvar'];
    
            $sql_part = ' WHERE user = %s HAVING distance < %d ORDER BY distance LIMIT 0, %d';
            $placeholders = array(
            $tester,
                $_GET["lat"],
                $_GET["lng"],
                $_GET["lat"],
                $_GET["radius"],
                $max_results
            );  
    
        }
    
        $result = $wpdb->get_results(
                        $wpdb->prepare( "
                                        SELECT *, ( $distance_unit * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) )
                                        AS distance FROM $wpdb->wpsl_stores
                                        $sql_part
                                        ",
                                        $placeholders  
    
                        )
                    );
    Plugin Author Tijmen Smit

    (@tijmensmit)

    The order of the placeholder values have to match with the order of the actual placeholders.

    The WHERE user = %s is the fourth placeholder, so you should place $tester before $_GET[“radius”] in the placeholder array.

    You can echo the last query with $wpdb->last_query, and check how it looks like in the browser console.

    Thread Starter lukem15

    (@lukem15)

    Thank you for such a speedy reply.

    I’ve changed the location of $tester but the results are not being filtered and is only showing results with the value of 0 – instead of 1.

    When I use $wpdb->last_query it returns /wp-admin/admin-ajax.php?action=store_search&lat=xxxxx&lng=xxxxx&max_results=25&radius=50 – I’m guessing this is where the problem is since myvar is not listed?

    P.S. do you accept donations? I would like to donate to you for your help and your great plugin.

    Thread Starter lukem15

    (@lukem15)

    I’ve fixed the problem, thanks to your direction.

    Instead of trying to use $_GET or $_POST I’ve added an extra dropdown menu, which contains the users. When search is pressed Ajax grabs it and send it to the query.

    Thank so much for your help Tijmen, do send me your paypal. I will also look forward to the release of 2.0 & premium.

    Plugin Author Tijmen Smit

    (@tijmensmit)

    Good to hear it’s working now ?? If you want to make a donation you can paypal it to info at tijmensmit.com, thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Locator with dedicated stores dependant on the user’ is closed to new replies.