1

My question is related to Add a custom text before the price display in WooCommerce answer.

  1. 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).
  2. 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).
  3. 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.

2
  • 3
    Note that $text. ' ' . $price; doesn't do anything, and should be replaced with $price = $text. ' ' . $price;. Commented Sep 3, 2023 at 10:16
  • Can you please guide me in this matter, I am really confused! stackoverflow.com/questions/76888202/… Commented Sep 3, 2023 at 15:28

1 Answer 1

4

The following, will add a different string prefix to the displayed price depending if the product is on sale or not and depending and depending on the page type (single product, shop, archives, cart and checkout):

add_filter( 'woocommerce_get_price_html', 'add_string_prefix_to_price_html', 10, 2 );
function add_string_prefix_to_price_html( $price_html, $product ) { 
    global $woocommerce_loop;

    // Not on related products
    if ( isset($woocommerce_loop['name']) && $woocommerce_loop['name'] === 'related' ) {
        return $price_html; 
    }

    $prefix = ''; // Initializing

    // Set the text by section type
    if ( is_shop() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE SHOP', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL SHOP', 'woocommerce') . ' '; // optional
        }
    } elseif( is_tax() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE ARCHIVES', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL ARCHIVES', 'woocommerce') . ' '; // optional
        }
    } elseif( is_product() ) {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE PRODUCT', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL PRODUCT', 'woocommerce') . ' '; // optional
        }
    } else {
        if ( $product->is_on_sale() ){ 
            $prefix = __('ON SALE OTHERS', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL OTHERS', 'woocommerce') . ' '; // optional
        }
    }
    return $prefix . $price_html; 
}

// For cart (optional)
add_filter( 'woocommerce_cart_item_price', 'add_string_prefix_to_cart_item_price_html', 10, 2 ); 
function add_string_prefix_to_cart_item_price_html( $price_html, $cart_item ) {
    $prefix = ''; // Initializing
    
    if ( $cart_item['data']->is_on_sale() ){ 
        $prefix = __('ON SALE CART', 'woocommerce') . ' ';
    } else {
        $prefix = __('NORMAL CART', 'woocommerce') . ' '; // optional
    }
    return $prefix . $price_html;
}

// For checkout (optional)
add_filter( 'woocommerce_cart_item_subtotal', 'add_string_prefix_to_checkout_item_subtotal_html', 10, 2 );
function add_string_prefix_to_checkout_item_subtotal_html( $subtotal_html, $cart_item ) {
    $prefix = ''; // Initializing

    if( is_checkout() ) {
        if ( $cart_item['data']->is_on_sale() ){ 
            $prefix = __('ON SALE CHECKOUT', 'woocommerce') . ' ';
        } else {
            $prefix = __('NORMAL CHECKOUT', 'woocommerce') . ' '; // optional
        }
    }
    return $prefix . $subtotal_html;
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent, this is the most complete answer that could be received. Only when the Related Product is on the Single Product page, the text (On sale product) is displayed! Can this be handled?
No sorry I just tested my code on single product pages, my code handle related products in the first line inside the first function, so nothing is displayed on related products. Now if something is displayed, it's because your theme, a plugin or some code that you have added is interacting with my code. What you can try is to increase the hook priority number in the first function from 10to 10000 for example, it may solve the problem.
No, unfortunately it didn't work and it still displays the text, thank you. I learned a new thing today and I didn't know that this number is 10 for the hook priority number. This number can be from 0 to what value? What is the default value? add_filter( 'woocommerce_get_price_html', 'add_string_prefix_to_price_html', 10, 2 ); What is the number 2 for?
The number 2 is for the number of argument variables in the function… so here we have 2: $price_htmland $product… For the priority, the number can go from one to any number up. Now for the related products, something in your site is making trouble (your theme, a plugin or some other code that you have added).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.