Related to woocommerce filter queries

I stuck on 1 API,
not getting expected result.

  1. I want a filter like when I select category then based on selected category brand, metal, color and size list should be fetch.
  2. When I add category id 18, and add brand id 70 then the result should be arise based on category and brand.
    if there have no any category, brand, metal, color and size then the result will be all and it’s arising but on selection no proper result.

Please Help Me

My code is

if ($action == 'filters') {
    global $wpdb, $wp_query, $table_prefix, $CORE, $CORE_ADMIN, $woocommerce, $product, $jws_option;
    $token = $_REQUEST['token'];
    $cat_id = $_POST['cat_id'];
    $brand_term_id = $_POST['brand_term_id'];
    $color_term_id = $_POST['color_term_id'];
    $metal_terms_id = $_POST['metal_term_id'];
    $size_term_id = $_POST['size_term_id'];
    $user_id = ((get_userid_from_token($token)) > 0) ? get_userid_from_token($token) : 0;
    // $attributes_array = $attributes_cat = $merged_attributes = $terms_list = $category_ids = $tag_ids = [];

    /* --------------------------------------------------------------------------------
        Check if category is not empty fetch list by category id else all category list 
    --------------------------------------------------------------------------------- */
    $category_list = $category_ids = [];
    if (!empty($cat_id)) {
        $data = explode(',', $cat_id);
        foreach ($data as $id) {
            $category_list[] = $id;
        }
    } else {
        /* $category_list = get_terms(
            array(
                'taxonomy' => 'product_cat',
                'fields' => 'ids',
                'get' => 'all',
            )
        ); */
    }
    foreach ($category_list as $category_id) {
        $terms_list[] = array(
            'term_id' => $category_id,
            'term_name' => get_term_by('id', $category_id, 'product_cat')->name,
        );
        $category_ids[] = $category_id;
    }

    // $filters = get_result($category_ids = [], $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);
    // $filters = get_result($category_ids, $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);

    /* ----------------------------------------------------------------------------------------
        Check if brand_term_id is not empty fetch all attributes and category
    --------------------------------------------------------------------------------------------- */
    $brand_ids = [];
    $attribute_list = wc_get_attribute(3);
    $attribute_terms = get_terms(
        array(
            'taxonomy' => $attribute_list->slug,
            'hide_empty' => false,
        )
    );
    if (!empty($brand_term_id)) {
        $brands_terms = explode(',', $brand_term_id);
        foreach ($brands_terms as $brand_terms) {
            foreach ($attribute_terms as $term_list) {
                if ($term_list->term_id == $brand_terms) {
                    $brand_ids[] = $term_list->term_id;
                }
            }
        }
    } else {
        /* foreach ($attribute_terms as $term_list) {
            $brand_ids[] = $term_list->term_id;
        } */
    }

    // $filters = get_result($category_ids = [], $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);

    /* ----------------------------------------------------------------------------------------
        Check if color_term_id is not empty fetch all attributes and category
    --------------------------------------------------------------------------------------------- */
    $color_ids = [];
    $attribute_list = wc_get_attribute(1);
    $attribute_terms = get_terms(
        array(
            'taxonomy' => $attribute_list->slug,
            'hide_empty' => false,
        )
    );
    if (!empty($color_term_id)) {
        $color_terms = explode(',', $color_term_id);
        foreach ($color_terms as $color_term) {
            foreach ($attribute_terms as $term_list) {
                if ($term_list->term_id == $color_term) {
                    $color_ids[] = $term_list->term_id;
                }
            }
        }
    } else {
        /* foreach ($attribute_terms as $term_list) {
            $color_ids[] = $term_list->term_id;
        } */
    }
    // $filters = get_result($category_ids = [], $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);

    /* ----------------------------------------------------------------------------------------
       Check if metal_item_id is not empty fetch all attributes and category
   --------------------------------------------------------------------------------------------- */
    $metal_ids = [];
    $attribute_list = wc_get_attribute(4);
    $attribute_terms = get_terms(
        array(
            'taxonomy' => $attribute_list->slug,
            'hide_empty' => false,
        )
    );
    if (!empty($metal_terms_id)) {
        $metal_terms_ids = explode(',', $metal_terms_id);
        foreach ($metal_terms_ids as $metal_term_id) {
            foreach ($attribute_terms as $term_list) {
                if ($term_list->term_id == $metal_term_id) {
                    $metal_ids[] = $term_list->term_id;
                }
            }
        }
    } else {
        /* foreach ($attribute_terms as $term_list) {
            $metal_ids[] = $term_list->term_id;
        } */
    }
    // $filters = get_result($category_ids = [], $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);

    /* ----------------------------------------------------------------------------------------
        Check if size_term_id is not empty fetch all attributes and category
    --------------------------------------------------------------------------------------------- */
    $size_ids = [];
    /*  $attribute_list = wc_get_attribute(2);
     $attribute_terms = get_terms(array(
         'taxonomy' => $attribute_list->slug,
         'hide_empty' => false,
     )); */
    if (!empty($size_term_id)) {
        $sizes_terms_id = explode(',', $size_term_id);
        foreach ($sizes_terms_id as $sizes_term_id) {
            foreach ($attribute_terms as $term_list) {
                if ($term_list->term_id == $sizes_term_id) {
                    $size_ids[] = $term_list->term_id;
                }
            }
        }
    } else {
        /* foreach ($attribute_terms as $term_list) {
            $size_ids[] = $term_list->term_id;
        } */
    }
    $filters = get_result($category_ids, $brand_ids, $color_ids, $metal_ids, $size_ids);
    // $filters = get_result($category_ids = [], $brand_ids = [], $color_ids = [], $metal_ids = [], $size_ids = []);
    die(json_encode($filters));
}

function get_result($category_ids, $brand_ids, $color_ids, $metal_ids, $size_ids)
{
    $merged_attributes = $tag_ids = $terms_list = $tax_query = [];


    if (!empty($category_ids) || !empty($brand_ids) || !empty($color_ids) || !empty($metal_ids) || !empty($size_ids)) {
        $tax_query[] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $category_ids,
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => wc_get_attribute(3)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $brand_ids,
            ),
            array(
                'taxonomy' => wc_get_attribute(1)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $color_ids,
            ),
            array(
                'taxonomy' => wc_get_attribute(4)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $metal_ids,
            ),
            array(
                'taxonomy' => wc_get_attribute(2)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $size_ids,
            ),
        );
    } else {
        $category_list = get_terms(
            array(
                'taxonomy' => 'product_cat',
                'fields' => 'ids',
                'get' => 'all',
            )
        );
        foreach ($category_list as $category) {
            $category_ids1[] = $category;
        }

        $attribute_terms1 = get_terms(
            array(
                'taxonomy' => wc_get_attribute(3)->slug,
                'hide_empty' => false,
            )
        );

        foreach ($attribute_terms1 as $term_list) {
            $brand_ids1[] = $term_list->term_id;
        }

        $attribute_terms2 = get_terms(
            array(
                'taxonomy' => wc_get_attribute(1)->slug,
                'hide_empty' => false,
            )
        );

        foreach ($attribute_terms2 as $term_list) {
            $color_ids1[] = $term_list->term_id;
        }


        $attribute_terms3 = get_terms(
            array(
                'taxonomy' => wc_get_attribute(4)->slug,
                'hide_empty' => false,
            )
        );

        foreach ($attribute_terms3 as $term_list) {
            $metal_ids1[] = $term_list->term_id;
        }

        $attribute_terms4 = get_terms(
            array(
                'taxonomy' => wc_get_attribute(2)->slug,
                'hide_empty' => false,
            )
        );
        foreach ($attribute_terms4 as $term_list) {
            $size_ids1[] = $term_list->term_id;
        }

        $tax_query[] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $category_ids1,
            ),
            array(
                'taxonomy' => wc_get_attribute(3)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $brand_ids1,
            ),
            array(
                'taxonomy' => wc_get_attribute(1)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $color_ids1,
            ),
            array(
                'taxonomy' => wc_get_attribute(4)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $metal_ids1,
            ),
            array(
                'taxonomy' => wc_get_attribute(2)->slug,
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms' => $size_ids1,
            ),
        );
    }
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'tax_query' => $tax_query,
    );

    /*  echo '<pre>';
     print_r($args);
     echo '</pre>';
     die(); */
    $products = new WP_Query($args);
    if ($products->have_posts()) {
        while ($products->have_posts()) {
            $products->the_post();
            $product = wc_get_product(get_the_ID());
            echo '<pre>';
            print_r($product->get_name());
            echo '</pre>';
            $all_attributes = $product->get_attributes();
            foreach ($all_attributes as $attribute) {
                $id = $attribute->get_data()['id'];
                $value = $attribute->get_data()['options'];
                if (!isset($merged_attributes[$id])) {
                    $merged_attributes[$id] = $value;
                } else {
                    $merged_attributes[$id] = array_merge($merged_attributes[$id], $value);
                    $merged_attributes[$id] = array_unique($merged_attributes[$id]);
                }
            }
            $categories = wp_get_post_terms($product->get_id(), 'product_cat')[0];
            if (!in_array($categories->term_id, $category_ids)) {
                $category_ids[] = wp_get_post_terms($product->get_id(), 'product_cat')[0]->term_id;
            }

            if ((!empty($product->get_tag_ids()))) {
                $tag_ids[] = $product->get_tag_ids();
            }
        }
        wp_reset_postdata();
    }
    $merged_result = [];
    foreach ($merged_attributes as $id => $value) {
        $merged_result[] = ['id' => $id, 'value' => $value];
    }
    if (!empty($merged_result)) {
        foreach ($merged_result as $attributes) {
            $id = $attributes['id'];
            $value = $attributes['value'];
            $attribute_list = wc_get_attribute($id);
            $attribute_terms = get_terms(
                array(
                    'taxonomy' => $attribute_list->slug,
                    'hide_empty' => false,
                    'include' => $value,
                )
            );
            $term_items_array = [];
            foreach ($attribute_terms as $term_list) {
                if (in_array($term_list->term_id, $value)) {
                    $term_items_array[] = array(
                        'item_id' => $term_list->term_id,
                        'item_name' => $term_list->name,
                    );
                }
            }
            $terms_list[] = array(
                'term_id' => $attribute_list->id,
                'term_name' => $attribute_list->name,
                'term_items' => $term_items_array,
            );
        }
    } else {
        $attribute_list = wc_get_attribute_taxonomies();
        foreach ($attribute_list as $attributes) {
            $attribute_list = wc_get_attribute($attributes->attribute_id);
            $attribute_terms = get_terms(
                array(
                    'taxonomy' => $attribute_list->slug,
                    'hide_empty' => false,
                )
            );
            $term_items_array = [];
            foreach ($attribute_terms as $term_list) {
                $term_items_array[] = array(
                    'item_id' => $term_list->term_id,
                    'item_name' => $term_list->name,
                );
            }
            $terms_list[] = array(
                'term_id' => $attribute_list->id,
                'term_name' => $attribute_list->name,
                'term_items' => $term_items_array,
            );
        }
    }

    // $tag_ids_array = array_merge(...$tag_ids);
    if (!empty($tag_ids)) {
        $tag_list = get_terms(
            array(
                'taxonomy' => 'product_tag',
                'hide_empty' => false,
                'include' => $tag_ids,
            )
        );
    }
    foreach ($tag_list as $list) {
        $terms_list[] = array(
            'tag_id' => $list->term_id,
            'tag_name' => $list->name,
        );
    }

    // $cat_ids_array = array_merge(...$category_ids);
    foreach ($category_ids as $list) {
        $terms_list[] = array(
            'category_id' => $list,
            'category_name' => get_term_by('id', $list, 'product_cat')->name,
        );
    }
    return $terms_list;
}