0
votes

I am trying to search for a product by attribute as taxonomy but the query doesn't return any result. In my example I hard coded id 58 which has attached 3 products to it. My attribute is called Make and I am using pa_make.

function custom_search($q)
{
  if ($q->is_search()) { 
    $tax_query = array(
      array(
        'taxonomy' => 'pa_make',
        'field' => 'id',
        'terms' => [58],
        'operator' => 'IN'
      )
    );
    $q->set("tax_query", $tax_query);
  }
}
add_action("woocommerce_product_query", "custom_search");

EDIT:

<form action="/" id="search-form" class="filters-wrapper">
  <input type="hidden" name="s" value="" />
  <input type="hidden" name="post_type" value="product" />
  <input type="checkbox" name="search_make" value="58" checked />
</form>

The form is submitted on checkbox click via JS. Don't know how does will help but still.

EDIT 2:

function custom_search($q)
{
    $tax_query = (array)$q->get('tax_query');
    if ($q->is_search()) { 
      $tax_query = array(
        'taxonomy' => 'pa_make',
        'field' => 'id',
        'terms' => [58],
        'operator' => 'IN'
      );
      $q->set("tax_query", $tax_query);
    }
}

echo "<pre>";
var_dump($tax_query);die;
add_action("woocommerce_product_query", "custom_search");

This way by adding my tax_query to the previous array the var dump shows:

array(3) {
  ["relation"]=>
  string(3) "AND"
  [0]=>
  array(4) {
    ["taxonomy"]=>
    string(18) "product_visibility"
    ["field"]=>
    string(16) "term_taxonomy_id"
    ["terms"]=>
    array(1) {
      [0]=>
      int(9)
    }
    ["operator"]=>
    string(6) "NOT IN"
  }
  [1]=>
  array(4) {
    ["taxonomy"]=>
    string(11) "pa_make"
    ["field"]=>
    string(2) "id"
    ["terms"]=>
    array(1) {
      [0]=>
      int(58)
    }
    ["operator"]=>
    string(2) "IN"
  }
}
1
Are you sure field shouldn't be term_id? - Sergiu Paraschiv
Tried with it as well. Still nothing. - Toma Tomov
Then I'm afraid you'll have to show us some more relevant code. At the moment I don't see anything wrong with the code I see. - Sergiu Paraschiv
This is the whole code of the function. pa_make has two values with IDs 58,60. Not sure what more I have to provide. I`ll put the form that calls the search. - Toma Tomov
Well, it should work, but there's no way for me to tell if $q->is_search() is true, pa_make is the correct taxonomy name, in is the correct field name, woocommerce_product_query is not intercepted by something else before or is manipulated after your action, your action is actually added at all (because maybe it's not added at the right time), etc. In other words the syntax looks correct, that is indeed the way you add a tax_query to a woocommerce_product_query, but other than that there's not much more one can see here. - Sergiu Paraschiv

1 Answers

0
votes

Hopefully you're not still stuck on this issue but I was having a similar issue and discovered that the woocommerce_product_query wasn't even running on the search. In my case I was trying to get hidden products to not appear in the search. I had to use pre_get_posts instead.

I don't know if that's the intended behaviour or a bug. It seems like it might be intended since the search isn't necessarily a WC product query even though products are being queried by it.

Maybe something like this might work for you like it did for me:

function custom_search( $q )
{
    if ( $q->is_search() /*&& ! is_admin() && $q->is_main_query()*/ )
    {
        $q->set( 'tax_query', [
            [
                'taxonomy' => 'pa_make',
                'field'    => 'id',
                'terms'    => [ 58 ],
                'operator' => 'IN'
            ]
        ] );
    }

    return $q;
}

add_action( "pre_get_posts", "custom_search" );

In mine I have that bit I commented out in the if statement but maybe you don't need that for yours.

There is also a filter for woocommerce_product_query_tax_query that I also couldn't get to work on it likely for the same reason that it's not necessarily a WC product query.