Limit the categories or products where the WooCommerce sale flash is displayed.
On the Advanced WooCommerce Facebook group a user asked about hiding the WooCommerce sale flash on certain products or categories. Changing the sale flash markup is easy and limiting it isn’t much extra work.
Get product ID and categories
The sale flash can be controlled via the ‘woocommerce_sale_flash‘ filter. The post and product objects are passed to the filter function along with the sale flash markup.
If you are changing the markup site wide then you can ignore the post and product objects.
To check whether the product is in the exclusion list I just pass $product->get_id() to the in_array() function. I could have used $post->ID but decided to use WooCommerce functions just in case the post and product IDs are not in sync.
Getting the product’s categories involves a tiny bit more work. The get_the_terms() function retrieves information about the product’s categories. From that I retrieve the category slug and check whether it is in the category exclusion list. If you prefer to search by category ID you can access $category->term_id and update the category exclusion list to use IDs. I think that slugs make the code easier to understand and maintain.
The code includes debug messages to help confirm that it is working as intended i.e. excluding the correct product or category. This screenshot is when the debug messages are enabled.
After you change the $exclude_by_product and $exclude_by_category arrays you can confirm that the code is working by using the two debug lines. These lines return text instead of an empty string (see screenshot above). When it’s working then you can comment those lines out and return an empty string.