In WooCommerce, the sales badge is a small label displayed on products to indicate discounts or promotions. While the default text is “Sale,” you may want to personalize it to suit your branding better or convey a different message. In this guide, we’ll walk you through the process of changing the sales badge text to meet your specific needs.
/**
* Snippet Name: Change sales badge text
* Snippet Author: wpsnippets.dev
*/
//Change the sales badge text in WooCommerce
function wps_custom_change_sales_badge_text( $html, $post, $product ) {
if ( $product->is_on_sale() ) {
$html = '<span class="onsale">' . __( 'Discount', 'text-domain' ) . '</span>';
}
return $html;
}
add_filter( 'woocommerce_sale_flash', 'wps_custom_change_sales_badge_text', 10, 3 );
Code Explanation
- The function woocommerce_sale_flash filter hook to modify the HTML output of the sales badge.
- It checks if the product is on sale ($product->is_on_sale()) and replaces the default text “Sale” with “Discount” using the __( ‘Discount’, ‘woocommerce’ ) function for localization support.
- You can Customize sale badge by applying css in style.css file of your child theme.