admin管理员组文章数量:1346070
I am trying to build a custom ajax product filters. My main objective is when user is on X category page, I will show him some size option(as checkbox) for filtering. So when user check single/multiple sizes the page show the products which belongs to that category and have that size option in stock. By default it will show all the products of that category.
In my case without filtering products are:
I am using this code for testing the filter:
<?php
$args = [
"post_type" => ["product", "product_variation"],
"posts_per_page" => 12,
"meta_query" => [
"key" => "_stock_status",
"value" => "instock",
"compare" => "=",
],
"tax_query" => [
"relation" => "AND",
[
"taxonomy" => "product_cat",
"field" => "slug",
"terms" => ["c1_1"],
],
[
"taxonomy" => "pa_size",
"field" => "slug",
"terms" => ["m"],
],
],
];
$query = new WP_Query($args);
echo "query result start";
echo '<div class= "products-grid">';
if ($query->have_posts()):
while ($query->have_posts()):
$query->the_post();
wc_get_template_part("content", "product");
endwhile;
wp_reset_postdata();
else:
echo "<p>" . esc_html__("No products found", "your-text-domain") . "</p>";
endif;
echo "</div>";
?>
The result showing from this query is:
Both are same. So my question is where is the issue and how can i achieve my ultimate goal. If i get the query result i can handle the rest.
I am trying to build a custom ajax product filters. My main objective is when user is on X category page, I will show him some size option(as checkbox) for filtering. So when user check single/multiple sizes the page show the products which belongs to that category and have that size option in stock. By default it will show all the products of that category.
In my case without filtering products are:
I am using this code for testing the filter:
<?php
$args = [
"post_type" => ["product", "product_variation"],
"posts_per_page" => 12,
"meta_query" => [
"key" => "_stock_status",
"value" => "instock",
"compare" => "=",
],
"tax_query" => [
"relation" => "AND",
[
"taxonomy" => "product_cat",
"field" => "slug",
"terms" => ["c1_1"],
],
[
"taxonomy" => "pa_size",
"field" => "slug",
"terms" => ["m"],
],
],
];
$query = new WP_Query($args);
echo "query result start";
echo '<div class= "products-grid">';
if ($query->have_posts()):
while ($query->have_posts()):
$query->the_post();
wc_get_template_part("content", "product");
endwhile;
wp_reset_postdata();
else:
echo "<p>" . esc_html__("No products found", "your-text-domain") . "</p>";
endif;
echo "</div>";
?>
The result showing from this query is:
Both are same. So my question is where is the issue and how can i achieve my ultimate goal. If i get the query result i can handle the rest.
Share Improve this question edited 2 days ago LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked 2 days ago Sreenath KumarSreenath Kumar 239 bronze badges1 Answer
Reset to default 0you can't call product variation like this "post_type" => ["product", "product_variation"].
<?php
$current_category = get_queried_object();
$category_slug = 'c1_1'; // Default or starting category slug for testing
if (isset($current_category->slug)) {
$category_slug = $current_category->slug;
}
$selected_sizes = ['m']; // Example: User selected 'M' size. Could be ['m', 'l'] etc.
$args = [
'post_type' => 'product', // Query ONLY for the parent product type
'post_status' => 'publish',
'posts_per_page' => 12,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => [$category_slug],
],
[
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $selected_sizes,
'operator' => 'IN'
],
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
]
]
];
$query = new WP_Query($args);
echo "query result start";
echo '<div class="products products-grid">';
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
wc_get_template_part('content', 'product');
endwhile;
wp_reset_postdata();
else :
echo "<p>" . esc_html__("No products matching your criteria were found.", "your-text-domain") . "</p>";
endif;
echo "</div>";
?>
本文标签: phpWooCommerce custom ajax product filterStack Overflow
版权声明:本文标题:php - WooCommerce custom ajax product filter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743819349a2544507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论