2

In Woocommerce I'm trying to add text before price if is higher than 59€

I tried the following code (and others one) but they didn't work:

    add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
if ($price>='59'){
$new_price = $price .__('(GRATIS!)');
}
return $new_price;
}

How can I do to add (GRATIS!) text before product price if it's higher than 59?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
delfinoweb
  • 103
  • 1
  • 3
  • 9

1 Answers1

2

Updated: You should try the following revisited function code:

add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // Only on frontend and excluding min/max prices on variable products
    if( is_admin() || $product->is_type('variable') ) 
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if( $price > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
    
    return $price_html;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.


For single product pages only you will replace this line:

if( $price > 15 ){

By this:

if( $price > 15 && is_product() ){

For single product pages and archives pages you will replace this line:

if( $price > 15 ){

By this:

if( $price > 15 && ( is_product() || is_shop() || is_product_category() || is_product_tag() ){
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks a lot! It's possible just only prduct page? Thanks – delfinoweb Feb 02 '18 at 01:11
  • @delfinoweb Accepting an answer is not upvoting it. You should need to click on the greyed checkmark icon (so not the arrows icons) beside the answer, to toggle it from greyed to green. So Acceptance is indicated by a colored checkmark next to the answer that has been accepted by the original author of the question. – LoicTheAztec Jul 12 '18 at 23:49