Add a custom text before the price if the product has a sale price in WooCommerce
function add_custom_text_before_sale_prices( $price, $product ) {
// Text
$text_regular_price = __("Regular Price: ");
$text_final_price = __("Final Price: ");
if ( $product->is_on_sale() ) {
$has_sale_text = array(
'<del>' => '<del>' . $text_regular_price,
'<ins>' => '<br>'.$text_final_price.'<ins>'
);
$return_string = str_replace(
array_keys( $has_sale_text ),
array_values( $has_sale_text ),
$price
);
return $return_string;
}
return $price;
} `add_filter( 'woocommerce_get_price_html', 'add_custom_text_before_sale_prices', 100, 2 );`
But I am getting output –
USD100.00
Final Price: USD50.00
But I want
Regular Price: USD100.00
Final Price: USD50.00
2
Answers
you can use this code for it
Here are two snippets of code you can use, You can add in an active child theme
function.php
file.Method 1: Add text before regular price and sale price
Method 2: Add text before regular price only
See the attachment Output