Customizing Woocommerce Sale Badge based on Product’s Category

Lately, I was working on an e-commerce shop built with Woocommerce for ease of use, as they’ve got also a great-looking app for managing your store on the way. There is a great variety of Plugins you can use on your Woocommerce setup.

The most needed feature of an e-commerce store, especially for targeting ads through the web, is a coupon/discount manager. Ended up using ELEX WooCommerce Dynamic Pricing and Discounts as I needed a global discount, based on the Product’s category. The final result was pretty decent, but it would be great to fully customize based on the product’s category.

Customizing based on Category ID


As there isn’t any way to do it from the Dashboard, we should add a small snippet in functions.php

// Sale badge
add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($terms  as $term  ) {
        $product_cat_id = $term->term_id;
        if ($product_cat_id == 68) {
            return '<span class="onsale offer">OFFER</span>';
    return '<span class="onsale">DISCOUNT</span>';
}Code language: PHP (php)

On the above snippet 68 is the Category ID and also I added an extra CSS class called “offer”. In this way you can add as much conditions as you like, but also make the color of event fonts differ based on the CSS rules you’ll apply.

Leave a Reply