Woo-Commerce Product Query with Multiple Arguments

I am trying to Query Woo-Commerce products based on the following

Featured
Popular
Promotional
Free

I was working to show them in isotop so I have to get all products and give specifiq tag based on my query to make filter. I was confused if I can make 4 Query as I have 4 different arguments / parameter.

I made 4 different query and got result in 4 different array. But there have problems.

  1. I had done 4 Queries and Save result on 4 array. So there same products containing. I have to filter all products and if same product in more than one category I have to input tag like features free . as Isotop works.
  2. If I unique marge array it will not show products in multiple category. But same products can be in two categories or can be in all categories so it should add tag in duplicate product and clear duplicate products list so that in result there will be unique product with multi tag.

Any idea how to do that ?

Below is my code which providing all products.

<?php

                                foreach ($filter_attr as $item) {
                                    if ($item == 'featured') {
                                        $meta_query = WC()->query->get_meta_query();
                                        $tax_query = WC()->query->get_tax_query();
                                        $tax_query[] = array(
                                            'taxonomy' => 'product_visibility',
                                            'field' => 'name',
                                            'terms' => 'featured',
                                            'operator' => 'IN',
                                        );

                                        $query_args = array(
                                            'post_type' => 'product',
                                            'post_status' => 'publish',
                                            'ignore_sticky_posts' => 1,
                                            'posts_per_page' => '12',
                                            'orderby' => 'date',
                                            'order' => 'desc',
                                            'meta_query' => $meta_query,
                                            'tax_query' => $tax_query,
                                        );


                                        $loop = new WP_Query($query_args);

                                        if ($loop->have_posts()) {
                                            while ($loop->have_posts()) : $loop->the_post();

                                                $featured_product = array(
                                                "type"=>$item,
                                                "product" => get_the_ID(),
                                                );

                                           endwhile;
                                        }  else {
                                            echo __('No products found');
                                        }
                                        wp_reset_postdata();

                                    }  elseif($item == 'popular'){

                                        $query_args = array(
                                            'post_type'           => 'product',
                                            'post_status'         => 'publish',
                                            'ignore_sticky_posts' => 1,
                                            'posts_per_page'      => '12',
                                            'meta_key'            => 'total_sales',
                                            'orderby'             => 'meta_value_num'
                                        );

                                        $loop = new WP_Query($query_args);

                                        if ($loop->have_posts()) {

                                            while ($loop->have_posts()) : $loop->the_post();

                                                $id=array();

                                                $popular_product = array(
                                                    "type"=>$item,
                                                    "product" => get_the_ID(),
                                                );

                                                print_r($popular_product);

                                            endwhile;
                                        }  else {
                                            echo __('No products found');
                                        }
                                        wp_reset_postdata();

                                    } elseif($item == 'promotional'){

                                        $query_args = array(
                                            'posts_per_page' => 12,
                                            'post_type' => 'product',
                                            'meta_key' => '_sale_price',
                                            'meta_value' => '0',
                                            'meta_compare' => '>='
                                        );

                                        $loop = new WP_Query($query_args);

                                        if ($loop->have_posts()) {

                                            while ($loop->have_posts()) : $loop->the_post();

                                                $id=array();

                                                $promotional_product = array(
                                                    "type"=>$item,
                                                    "product" => get_the_ID(),
                                                );

                                               print_r($promotional_product);

                                            endwhile;
                                        }  else {
                                            echo __('No products found');
                                        }
                                        wp_reset_postdata();
                                    } elseif($item == 'freebies'){

                                        $query_args = array(
                                            'posts_per_page' => 12,
                                            'post_type' => array('product', 'product_variation'),
                                            'meta_query' => array(
                                                array(
                                                    'key' => '_price',
                                                    'value' => 0,
                                                    'compare' => '<=',
                                                    'type' => 'NUMERIC'
                                                )
                                            )
                                        );


                                        $loop = new WP_Query($query_args);

                                        if ($loop->have_posts()) {

                                            while ($loop->have_posts()) : $loop->the_post();

                                                $id=array();

                                                $freebies_product = array(
                                                    "type"=>$item,
                                                    "product" => get_the_ID(),
                                                );

                                                print_r($freebies_product);

                                            endwhile;
                                        }  else {
                                            echo __('No products found');
                                        }
                                        wp_reset_postdata();
                                    }
                                }

                                ?>

Read more here: Woo-Commerce Product Query with Multiple Arguments

Leave a Reply

Your email address will not be published. Required fields are marked *