While I was working on a project, I came across a situation where I need to filter all the categories with products on sale. Well, this is straight forward, right? BUT I also need to remove those categories whose there were no any products on sale. Confused? Let’s check an example.
For Example: There are 3 categories: A, B, & C. Each category A and B has 5 products on sale. But, C doesn’t have any products on sale. So, I want to get only an A and B. In this article, we will retrieve ONLY categories that contain products on sale.
Get all Categories
First of all, we will get all categories ordered by name
in ascending order. We will set parent
to 0
which means we will retrieve only the parent category.
Get all IDs of Products on sale
In second step, we will get all the ids of products that are on sale. Remember that, we don’t need products that are not on sale. To filter the products that are on sale, we will use the WooCommerce default function wc_get_product_ids_on_sale
.
Note: Its good to get the IDs only because when the list of sale products is long, storing such values on variable can consume a lot of system resources.
$products_ids_on_sale = wc_get_product_ids_on_sale();
Remove Emtpy Categories
We have all the categories list and IDs of products that are on sale. Now, we will compare if a certain category contains ID of sale product. We will loop through the categories and get the IDs of of each product. Then, we will check if the product is present in the list of IDs of sale products.
First of all, we loop through the all categories. Inside loop, we get the IDs of products and store in a $category_product_ids
variable. We also define a flag $is_product_exist
which will helps us to check if there are any sale items inside a category.
In last loop, we loop through the IDs of products and check that ID with the array elements of IDs of products on sale. If Category product is contained inside IDs that are on sale, we set the flag to true and break loop. Finally, we check flag status and unset the category from category list when flag is false.
Final Code
So, our complete code looks like below:
Hi there,
Thanks for this information. I don’t have a lot of knowladge about wordpress and php codes. My question is where should i copy this code above to make the code work? I’ve paste it in child theme functions.php but nothing changed. Could you help me?
Thanks in advande.