• I have a product.
• In the product card there are 2 attributes (with different id's), with two different prices.
• In wp_postmeta the product has 2(!!!) meta_key == _price.
Accordingly, the search works on the 1st price. How to make the search work on the 2nd price?
Attribute id of the 2nd price is "rentfortheevent" ("pa_rentfortheevent" in the attributes table in database).
Right now the search by price is done like this:
function set_price($q)
{
$min_price = $_GET["min_price"];
$max_price = $_GET["max_price"];
$meta_query = $q -> get("meta_query");
if ($min_price)
{
$meta_query[] =
[
"key" => "_price",
"value" => $min_price,
"type" => "NUMERIC",
"compare" => ">=",
];
}
if ($max_price)
{
$meta_query[] =
[
"key" => "_price",
"value" => $max_price,
"type" => "NUMERIC",
"compare" => "<=",
];
}
$q -> set("meta_query", $meta_query);
}
add_action("woocommerce_product_query", "set_price");
So, I solved it.
It's not the prettiest solution, but there's no other.
1. I removed the search by meta fields.
2. For the taxonomy of the required attribute ("pa_rentfortheevent") I get all possible options ("terms").
3. I filter them by the requested price.
4. I add a filter by taxonomy with the remaining "slugs".
Since the filter is by taxonomy, another event is using - "woocommerce_product_query_tax_query".
The code is as follows:
function custom_price_query($tax_query, $query)
{
$min_price = isset($_GET["min_price"]) ? intval($_GET["min_price"]) : 0;
$max_price = isset($_GET["max_price"]) ? intval($_GET["max_price"]) : 10000000;
$taxonomy = "pa_rentfortheevent";
$terms = get_terms
(
[
"taxonomy" => "pa_rentfortheevent",
"hide_empty" => false
]
);
$slugs = [];
for ($i = 0, $c_terms = count($terms); $i < $c_terms; $i++)
{
$tmp = $terms[$i] -> name;
if (strpos($tmp, "dols") !== false)
{
$tmp = str_replace("dols", "", $tmp);
$tmp = str_replace(".", "", $tmp);
$tmp = str_replace(" ", "", $tmp);
$tmp = intval($tmp);
if (($tmp >= $min_price)
&& ($tmp <= $max_price)
)
{
$slugs[] = $terms[$i] -> slug;
}
}
}
unset($i);
$tax_query[] =
[
"taxonomy" => $taxonomy,
"field" => "slug",
"terms" => $slugs,
"operator" => "IN"
];
return $tax_query;
}
add_filter("woocommerce_product_query_tax_query", "custom_price_query", 10, 2);