Support » Plugin: YITH WooCommerce Ajax Product Filter » Negation of product attribute criteria

  • Hi there,

    I’m using the ajax product filter via two parallel widgets for a bakery shop:
    With one widget, customers can filter for ingredients – like chocolate, nuts, whatever…

    With the second widget, I want to filter for allergens: like wheat, soy products etc.

    By default, the filter checks whether the product attribute is set for the product. In case of the allergen filter, I would like to have it the other way round:
    Display only the products, that do NOT have this attribute set.

    My approach would be to modify the query accordingly in pre_get_posts.

    But my question is: Is there maybe a more straight forward way to do this?

    Thanks for your answers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hi,

    no I’m sorry but it’s not possible by our plugin. I think that the only way for this is change the query or create some custom code for this.

    Our plugin simply tell to WooCommerce wich filters we want to apply and WooCommerce plugin return the filtered product lists.

    Have a nice day
    YITH

    Hi,

    can you please point me into the right direction? I’m completely lost.

    This is the code I’m trying currently:

    define( 'HVM_ALLERGENS_TERM', 'pa_allergene' );
    add_filter( 'pre_get_posts', 'hvm_ajax_navigation_filter_def', 50 );
    
    function hvm_ajax_navigation_filter_def( $query ) {
    
    	if ( $query->is_tax() ) {
    
    		$tax_query = $query->tax_query->queries;
    
    		$new_tax_query = $tax_query;
    
    		if ( isset( $_GET['filter_allergene'] ) && count( $new_tax_query ) > 1 ) {
    
    				$key = array_search( HVM_ALLERGENS_TERM, array_column( $new_tax_query, 'taxonomy' ) );
    
    			if ( false !== $key ) {
    
    				if ( $new_tax_query[ $key ]['operator'] == 'IN' ) {
    					// Negate operator
    					$new_tax_query[ $key ]['operator'] = 'NOT IN';
    				}
    			}
    
    			$query->tax_query->queries[]    = $new_tax_query;
    			$query->query_vars['tax_query'] = $query->tax_query->queries;
    		}
    
    	}
    }
    Plugin Author YITHEMES

    (@yithemes)

    Hi,

    it’s not easy to do this with our plugin…I think the best way is to search for a plugin that allow you to create a step by step filter system….
    I don’t know how I can help you because the problem is that it’0s very hard to replicate your case…

    I’m sorry 🙁

    Have a nice day
    YITH

    Hi,

    you could get an impression on this site:
    Screenshot
    I would be grateful for any recommendations….

    The product attributes in the left column are the ones that should be negated.

    Plugin Author YITHEMES

    (@yithemes)

    Hi,

    I like this layout and I like your idea but unfortunately you can’t use our plugin for this. I think that the best solution is find a WordPress/WooCommerce devleoper and ask for a custom solution 🙂

    Have a nice day
    YITH

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Negation of product attribute criteria’ is closed to new replies.