Forums Forums Search & Filter Pro Price filter not updating after (custom) search

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #192976

    Hi,

    We have a filter for a WooCommerce shop. After we search in the search field, the price is not updated in the filter. and set to e.g. €4-€4 (which is the lowest price of a product in the shop.)

    This is the code we used to extend the search functionality so we can search for the slug (which is also the SKU):

    
    /**
     * Extend search.
     *
     * @param array $query WP_Query object.
     * @return void
     */
    function search_filter( $query ) {
    	if ( ! is_admin() && null !== $_GET['_sf_s'] ) {
    		$search = $_GET['_sf_s'];
    		$query->set( 'pr_title', $search ); // LIKE post_name via Filter.
    	}
    }
    
    add_action( 'pre_get_posts', 'search_filter', 10 );
    
    /**
     * Filter pr_title query var.
     *
     * @param string $where Where string.
     * @param array $wp_query WP_Query object.
     * @return string
     */
    function flux_posts_where( $where, $wp_query ) {
    	global $wpdb;
    	$pr_name = $wp_query->get( 'pr_title' );
    
    	if ( null !== $pr_name ) {
    		$where .= ' AND ' . $wpdb->posts . '.post_name LIKE \'%' . $wpdb->esc_like( $pr_name ) . '%\'';
    	}
    
    	return $where;
    }
    add_filter( 'posts_where', 'flux_posts_where', 10, 2 );
    

    Is there something we are doing wrong?

    Because our shop is a wholesale shop, I will send you the URL and login in a private reply.

    Thank you,
    Ruben

    Anonymous
    #192977
    This reply has been marked as private.
    Trevor
    #192983

    I am not sure if this relates directly to your question, but maybe it does. The Price field is a slider, and the slider, if set to auto detect, does not ‘react’ to changes made in other fields in the form. This is intentional, as to do so could have undesirable consequences. Read more about this here:

    https://support.searchandfilter.com/forums/topic/custom-max-price-for-price-slider/#post-77119

    If that is not the issue, please get back to me.

    Anonymous
    #193004

    Hi Trevor,

    After inputting a search term, the slider is set to min-min (€4-€4) instead of min-max (€4-€194) without us manually updating it.

    The price not updating is not really an issue for us, we just find it strange that it is ‘automatically’ updated to a wrong value.

    Also, even after hitting the reset button, the price is not set to its initial value (€4-€194).

    Trevor
    #193015

    Does this happen with and without your extra code?

    Anonymous
    #193026

    This is indeed only happening with our custom code…
    Do you have any idea what could go wrong?

    Trevor
    #193030

    You would need to explain in detail what they are doing. Or, what you want them to be doing.

    Anonymous
    #193089

    We would like to let the user search on SKU.
    Because our shop has 1000 variable products (with 20k+ variations), we prevent the search-query to be super heavy: we have set our product-slug as the SKU: https://arwb2b.be/123456 (where 123456 is equal to the SKU), we are extending the search query to search on the post_name (slug) in the posts-table of our database.
    This is working fine.

    However, the main problem is that the range-slider for the price is not staying in its initial ‘position’ and is set to €4-€4 after we hit the search query.
    We also need to click twice on the reset button before the slider is reset to €4-€194.

    Are we forgetting to pass some value to our extended search query?

    Trevor
    #193127

    I notice that all the other fields are empty. I wonder whether you need to sdo the search as a Custom type instead of WooCommerce, and then use our pre_get_posts method to attach our query to the page? Like this:

    function pre_get_posts_function($query)
    {
    //this would be a pre_get_posts you already have in place somewhere
    //then set <code>search_filter_id</code>
    $query->set("search_filter_id", 123);
    }
    add_action( 'pre_get_posts', array($this, 'pre_get_posts_function') );

    OR, to use your code to fetch an array of Post IDs and use post__in in the query by modifying the query arguments:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

Viewing 9 posts - 1 through 9 (of 9 total)