My question is related to Add a custom text before the price display in WooCommerce answer.
- I would like the text 'TEXT' to be displayed only when the product has a discount, and if the product does not have a discount, another text should be displayed(Or by default, no text should be displayed before the price).
- I have Related Products on the product page, and I don't want these texts to be displayed in this section, and I only want them to be displayed on the product page (single product).
- How can I manage the display of this text in different sections? (For example, one text should be displayed on the Single Product page, another text should be displayed on the Shop page, another text should be displayed on the archive page, in the search list, and so on in other sections)
I used the following code:
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
if ( is_product() && $product->is_on_sale() ){
// Your additional text in a translatable string
$text = __('TEXT'); // returning the text before the price return
$text. ' ' . $price;
}
return $price;
}
But it doesn't give me what I would like.
$text. ' ' . $price;doesn't do anything, and should be replaced with$price = $text. ' ' . $price;.