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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

you 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