Hello Anas,
I have just tested your custom code on a clean site with Storefront theme, and it works. I believe it works on your page as well, you simply do not have any on sale products added to the “Sale” product category!
The on sale products are those that have their sale price set. If you follow the ???? ????? menu item of your site (sorry, I don’t know the language, but from the link I can see that it filters your shop page for all the on sale products), it returns only 1 product, and this products doesn’t belong to the “Sale” products category.
You can check the products count of your “Sale” category in the Products > Categories section of the WordPress admin.
A few other notes:
1. Are you sure that you want to alter the main WooCommerce query in this manner:
$q->set( 'post_type', 'product' );
$q->set( 'product_cat', null );
??
2. You have your AJAX mode set to the Dedicated AJAX. In this mode, your custom code doesn’t influence the filters’ AJAX queries.
3. If you need the product counts to reflect your custom query condition, you need to add it to the product counts query with the help of the ‘awf_product_counts_query’ filter. Let me know here if you ever want to go ahead with that, and I will look into providing you with an example.
4. And finally, the way the post__in is set in this line of your custom code:
$q->set( 'post__in', $product_ids_on_sale );
.. removes any values previously added to the post__in property by the filters or other plugins. Can’t speak for the 3d parties, but the string search and some other filters use the post__in, so you will be resetting them with this line of code.
You could refine the existing post__in by first getting its current value, and then using array_intersect to intersect it with your own set of product ids (please see the class-a-w-f-frontend.php file of our plugin’s code folder for examples).