Adding a custom filter to Woocommerce using Divi's WooCommerce Product module

77 Views Asked by At

This is my first time working with wp_query. Was wondering if I'm going about this correctly. I'm creating a custom filter for a WordPress website that uses WooCommerce. I've created a basic form that gets some basic location info and passes it, along with the search distance, in the query string. I'm using the divi page builder for the site and have a WooCommerce Products module on the page that shows the search results.

I've created the below function (I've cleaned out a bunch of junk to make it easier to read). The function works for the most part, except it doesn't refresh the results on the screen. I've checed the SQL fromt the Query using echo $query->request; and then pasted it into a query window on the database and confirmed that the query is returing results. I've also tried both hooks at the bottom of the function.

I know the function is executing as I can go as far as printing the list of filtered product ids to the page. The only missing piece is getting the Divi WooCommerce Products module to show only the filtered results (it just returns all results)

What am I missing to update the actual results shown on my shop page?

function add_filter_to_product_search($query) { 
if ( is_admin() || ! $query->is_main_query() ){
    return;
 }

global $wpdb;

$distance = isset($_GET['distance']) ? intval($_GET['distance']) : 0;


if ($distance > 0) {
    $lat = isset($_GET['lat']) ? floatval($_GET['lat']) : 0; 
    $lng = isset($_GET['lng']) ? floatval($_GET['lng']) : 0; 

    $coords = get_coords($lat, $lng, $distance);
    
          
           if ($lat != 0 && $lng != 0) {
               
               
               $query = new WP_Query([
                'post_type' => 'product',
                   'posts_per_page' => 21,
                'meta_query' => [
                 'relation' => 'AND',
                      [
                    'key' => 'latitude',
                     'type' => 'DECIMAL',
                    'value' => [ $coords[1] , $coords[0]],
                    'compare' => 'BETWEEN',
                        ],
                 [
                    'key' => 'longitude',
                     'type' => 'DECIMAL',
                    'value' => [ $coords[2] , $coords[3]],
                    'compare' => 'BETWEEN',
      
                 ]
                             ]
                     ]);
               
                       
                   
         wp_reset_postdata();
               
                    
        return $query ;
        }
   }
}

//add_action('pre_get_posts', 'add_filter_to_product_search');

add_action('woocommerce_product_query', 'add_filter_to_product_search');

I've tried returning the query, returning the product list and different hooks to see if I can get it to update.

0

There are 0 best solutions below