Hey!
I have a bit of a complicated problem and I’m probably overthinking it in my stressed out developer brain by now so I wanted to ask here for some help:
I have a WooCommerce shop with around 20.000 different bike parts. Every product in the shop has either one or more terms of the taxonomy “pa\_compatibility”.
The taxonomy mentioned above has 4 fields in it:
* rp\_brand (e.g. “KTM”)
* rp\_model (e.g. “125 SX”)
* rp\_produced\_since (e.g. “2018”)
* rp\_produced\_until (e.g. “2022”)
I didn’t create the taxonomy, it was already part of a theme – the term name is created by a combination of the 4 fields in it, like “KTM 125 SX 2018-2022”, same for the term slug: “ktm-125-sx-2018-2022”.
So if I have for example an air filter as a product, it will probably have a few terms of the “pa\_compatibility” taxonomy like “KTM 125 SX 2018-2022” and “APRILIA RS 125 2014-2016”.
Now I wrote a filter that consists of 3 inputs: brand, year and model – so the user can input his bike data and only get products that fit his specific bike.
​
**The problem:** Whenever I query for the user inputs, I don’t get any results. I made a previous query that works about 50% and only shows a few fitting products but not all of them, which is strange since I can’t figure out why this query wouldn’t work.
This is the code for the query that only works halfways:
function vehicle_query_submit($filter_metavalue_brand, $filter_metavalue_year, $filter_metavalue_model) {
$term_query = [
‘taxonomy’ => “pa_compatibility”,
‘hide_empty’ => false,
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => “rp_brand”,
‘value’ => $filter_metavalue_brand,
‘compare’ => ‘=’
),
array(
‘key’ => “rp_produced_since”,
‘value’ => $filter_metavalue_year,
‘compare’ => ‘<=’,
‘type’ => ‘NUMERIC’
),
array(
‘key’ => “rp_produced_until”,
‘value’ => $filter_metavalue_year,
‘compare’ => ‘>=’,
‘type’ => ‘NUMERIC’
),
array(
‘key’ => ‘rp_model’,
‘value’ => $filter_metavalue_model,
‘compare’ => ‘=’
)
)
];
$terms = get_terms($term_query);
$results = array();
$productIds = [];
// var_dump($terms);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$newProdIds = get_product_ids($term->slug);
if ($newProdIds != NULL) {
$productIds = array_merge($productIds, $newProdIds);
}
}
echo get_permalink().’/shop/?product_ids=’.urlencode(json_encode($productIds));
}
}
And this is the get\_product\_ids() function that returns an array of product ids that match the slug
function get_product_ids($product_slug) {
$query = new WC_Product_Query(array(
‘limit’ => -1,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘tax_query’ => array( array(
‘taxonomy’ => ‘pa_compatibility’,
‘field’ => ‘slug’,
‘terms’ => $product_slug,
) ),
) );
$products = wc_products_array_orderby( $query->get_products(), ‘price’, ‘DESC’ );
$idArray = [];
if ( ! empty($products) ) {
foreach ($products as $product) {
$idArray[] = $product->get_id();
}
return $idArray;
}
}
Is there a better / more reliable way to do this that outputs all matching products?
Thanks in advance and greetings from Austria!
[ad_2]
Does it work better if you swap the compare for model and brand with a LIKE instead of a equals ?