Search code examples
phpwordpresswoocommerce

Check if a Category contains any products that are on sale in WooCommerce?


Well, it may sound easy but I searched a lot but could not find the appropriate answer. I want to check if a category contains any sale products.

For example, there are 10 categories. I want to get categories that contain products with sale and exclude those categories that don't contain any sale items.

Well, I am new to WP.


Solution

  • I searched a lot about the solution and finally I managed to write my own logic for answering this.

    Steps:

    1. Get all categories
    2. Get all ids of sale products
    3. loop through categories of step 1
    4. get products id of this category
    5. loop through ids of ids of step 4
    6. check if that id is present in list of ids of sale product
    7. if found, set flag to true and exit
    8. if flag is false (no product on sale), unset the category

    Here is my sample code:

    <?php
    
    $args = array(
                'orderby'    => 'name',
                'order'      => 'ASC',
                'hide_empty' => true,
                'parent'     => 0,
            );
    
    $categories = get_terms( 'product_cat', $args );
    
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $query_args = array(
                    'post_status'       => 'publish',
                    'post_type'         => 'product',
                    'posts_per_page'    => -1,
                    'paged'             => $paged,
                    'orderby'           => 'ASC',
                    'meta_query'        => WC()->query->get_meta_query(),
                    'post__in'          => array_merge( array( 0 ), wc_get_product_ids_on_sale() )
                );
    $sale_products = new WP_Query( $query_args );
    
    $products_ids_on_sale = wc_get_product_ids_on_sale();
    
    if($sale_products->have_posts()) {
        foreach ($categories as $key => $category) {
            //get product ids
            $category_product_ids = get_posts( array(
                                            'post_type'   => 'product',
                                            'posts_per_page' => -1,
                                            'post_status' => 'publish',
                                            'fields'      => 'ids',
                                            'tax_query'   => array(
                                                array(
                                                    'taxonomy' => 'product_cat',
                                                    'field'    => 'term_id',
                                                    'terms'    => $category->term_id,
                                                    'operator' => 'IN',
                                                )
                                            ),
                                        ) );
    
    
            $is_product_exist = false;
    
            if ( ! empty($category_product_ids) ) {
                foreach ($category_product_ids as $product_id) {
                    if (in_array($product_id, $products_ids_on_sale)) {
                        $is_product_exist = true;
                        break;
                    }
                }
    
                if ( $is_product_exist === false ) {
                    unset($categories[$key]);
                }
            }
        }
        wp_reset_query();
    }
    

    I have written a article on my blog and explain in more detail about the produces and code at https://www.kodementor.com/get-only-categories-that-contain-products-on-sale/