- Free code for WooCommerce discount labels
- Automate product badges in WordPress functions.php
- Screenshot9.com custom code tutorial
- How to increase sales on WooCommerce site free tips
/*==========================================
= Show Save TK or Out-of-Stock Badge =
==========================================*/
add_filter(‘woocommerce_sale_flash’, ‘custom_badge_with_save_tk’, 20, 3);
function custom_badge_with_save_tk($html, $post, $product) {if ( ! $product->is_in_stock() ) {
return ‘<span class=”onsale” style=”background:#dc3545;color:#fff;padding:0px 3px;border-radius:6px;font-size:13px;font-weight:bold;position:absolute;”>Out of Stock</span>’;
}if ( $product->is_type(‘variable’) ) {
$regular_price = $product->get_variation_regular_price(‘max’);
$sale_price = $product->get_variation_sale_price(‘min’);
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}if ( $regular_price > 0 && $sale_price > 0 && $sale_price < $regular_price ) {
$save_amount = number_format( $regular_price – $sale_price, 0 );
return ‘<span class=”onsale” style=”background:#28a745;color:#fff;padding:0px 3px;border-radius:6px;font-size:13px;font-weight:bold;position:absolute;”>Save TK ‘ . esc_html( $save_amount ) . ‘</span>’;
}
return $html;
}