0

I recently installed ElasticPress on my woocommerce store, and it's working fine -- the product page is showing normally, and the search bar is working fine with the new elastic capabilities. However, when I try to use the product filters, nothing shows up, the query simply doesn't work. If I deactivate Elasticpress, however, the filters work normally. This is the query:

Codeshare.io link for better readability

<?php

defined( 'ABSPATH' ) || exit;

global $wp_query;

add_action( 'woocommerce_product_query', 'tpk_archive_query' );

function tpk_archive_query( $q ){

    if($_GET['js'] == 'y') {
        $cat = explode(',', esc_attr($_GET['categorias']));
        $a = explode(',', esc_attr($_GET['artistas']));
        $ta = explode(',', esc_attr($_GET['tamanhos']));
        $c = explode(',', esc_attr($_GET['colecoes']));
        $t = explode(',', esc_attr($_GET['tags']));
        $preco = explode(',', esc_attr($_GET['preco']));
        $operador = strtoupper(esc_attr($_GET['op']));
        $orderby = explode('-', esc_attr($_GET['orderby']));
    } else {
        $cat = esc_attr($_GET['cat']);
        $a = esc_attr($_GET['a']);
        $ta = esc_attr($_GET['ta']);
        $c = esc_attr($_GET['c']);
        $t = esc_attr($_GET['t']);
    }



    if((esc_attr($_GET['orderby']) && !empty(esc_attr($_GET['orderby'])))) {

        if($orderby[0] == 'preco') {

            $q->set('orderby', 'meta_value_num');
            $q->set('meta_key', '_price');
            $q->set('order', $orderby[1]);

        } else {

            $q->set('orderby', $orderby[0]);
            $q->set('order', $orderby[1]);

        }

    }



    $args = [
        'relation' => 'AND',
    ];

    if((esc_attr($_GET['cat']) && !empty(esc_attr($_GET['cat'])))) {
        
        $category = [
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $cat,
            'operator' => 'IN'
        ];

        array_push($args, $category);
    } elseif(esc_attr($_GET['categorias']) && !empty(esc_attr($_GET['categorias']))) {

        if(in_array('kits', $cat)) {

            $categorias = ['relation' => 'OR',];

            foreach ($cat as $cate) {

                if($cate != 'kits') {

                    $kitCat = 'kits-' . $cate;

                    $cate = [$cate, $kitCat];

                    $pima = [
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $cate,
                        'operator' => 'IN'
                    ];
                    
                    array_push($categorias, $pima);

                }

            }

        } else {

            $categorias = [
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $cat,
                'operator' => 'IN'
            ];

        }

        array_push($args, $categorias);

    }

    if((esc_attr($_GET['a']) && !empty(esc_attr($_GET['a']))) || (esc_attr($_GET['artistas']) && !empty(esc_attr($_GET['artistas'])))) {
        $artistas = [
            'taxonomy' => 'autor',
            'field' => 'slug',
            'terms' => $a,
            'operator' => 'IN'
        ];

        array_push($args, $artistas);
    }

    if((esc_attr($_GET['ta']) && !empty(esc_attr($_GET['ta']))) || (esc_attr($_GET['tamanhos']) && !empty(esc_attr($_GET['tamanhos'])))) {
        $tamanho = [
            'taxonomy' => 'tamanho',
            'field' => 'slug',
            'terms' => $ta,
            'operator' => 'IN'
        ];

        array_push($args, $tamanho);
    }

    if((esc_attr($_GET['c']) && !empty(esc_attr($_GET['c']))) || (esc_attr($_GET['colecoes']) && !empty(esc_attr($_GET['colecoes'])))) {
        $colecao = [
            'taxonomy' => 'colecao',
            'field' => 'slug',
            'terms' => $c,
            'operator' => 'IN'
        ];

        array_push($args, $colecao);
    }

    if((esc_attr($_GET['t']) && !empty(esc_attr($_GET['t']))) || (esc_attr($_GET['tags']) && !empty(esc_attr($_GET['tags'])))) {
        
        if(!$operador) {
            $operador = 'OR';
        }
        $tags = ['relation' => $operador,];

        foreach ($t as $tag) {
            $poma = [
                'taxonomy' => 'product_tag',
                'field' => 'slug',
                'terms' => $tag,
                'operator' => 'IN'
            ];
            
            array_push($tags, $poma);
        }

        array_push($args, $tags);
    }

    $q->set('tax_query', $args);

    if((esc_attr($_GET['preco']) && !empty(esc_attr($_GET['preco'])))) {

        $min_price = $preco[0];
        $max_price = $preco[1];

        $q->set('meta_query', [
            [
                'key' => '_price',
                'value' => [$min_price, $max_price],
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            ]
        ]);

    }

}

I tried using WP Marvelous Debug to see if there were any errors happening during the query, and it didn't point at anything different. The only "errors" it pointed out were these undefined indexes, which I don't belive have anything to do with the problem, because they happen with or without Elastic.

Do I have to write my queries in an elastic-specific syntax? I spent the last two days trying to fix this and I really have no idea what to do.

Thanks in advance for any help.

---EDIT---

This is the generated query without any filters active (just the default woocommerce query, which works without any problems):

{
"query": {
    "post_type": "product"
},
"query_vars": {
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "orderby": "popularity",
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}

And this is the generated query when I set the "autor" filter as "Arcane Minis", and the "orderby" to "popularity":

{
"query": {
    "orderby": "",
    "post_type": "product"
},
"query_vars": {
    "orderby": "popularity",
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "0": {
        "taxonomy": "autor",
        "field": "slug",
        "terms": [
        "arcane-minis"
        ],
        "operator": "IN"
    },
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}

And lastly, this is the filtered query without elastic press:

{
"query": {
    "orderby": "",
    "post_type": "product"
},
"query_vars": {
    "orderby": "popularity",
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "0": {
        "taxonomy": "autor",
        "field": "slug",
        "terms": [
        "arcane-minis"
        ],
        "operator": "IN"
    },
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}
  • 1
    Would it be feasible to dump the final **JSON query** (`json_encode($q)`) before it gets sent to ES? – Joe - GMapsBook.com Jan 28 '21 at 08:54
  • @JoeSorocin excuse my ignorance, but I'm not very well versed in PHP (I'm a UX designer), so if you could elaborate on your answer I would be very thankful. Do you mean I should just add `json_encode($q)` at the end of the function? – Alan Fernandes Jan 28 '21 at 16:32
  • 1
    No worries. I meant that we need to see the actual, final Elasticsearch query in order to assess this. So yes -- add that statement to the end of the function a edit your question with the response. – Joe - GMapsBook.com Jan 28 '21 at 16:46
  • @JoeSorocin Awesome, thanks. I edited the question with the query args for the default woocommerce query, and my filtered query. – Alan Fernandes Jan 28 '21 at 16:59
  • 1
    Thanks. I thought it was gonna be a query I can work with but I'm not familiar w/ this syntax/elasticpress. – Joe - GMapsBook.com Jan 28 '21 at 17:06

0 Answers0