Emanuell Vieira
Emanuell Vieira

Reputation: 13

Add a custom text before the price display in WooCommerce

In WooCommerce, I'm using this code to put a text in the price display:

function cw_change_product_price_display( $price ) {
    $price .= ' TEXT';
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

The page displays like "$99,99 TEXT"

I want to make it displays like this: "TEXT $99,99"

Thank you for the help.

Upvotes: 1

Views: 18198

Answers (4)

Learning and sharing
Learning and sharing

Reputation: 1398

You can use this:

if( !function_exists("add_custom_text_prices") ) {
    function add_custom_text_prices( $price, $product ) {
        // Text
        $text_regular_price = __("Regular Price: ");
        $text_final_price = __("FinalPrice: ");

        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 $text_regular_price . $price;
    }
    add_filter( 'woocommerce_get_price_html', 'add_custom_text_prices', 100, 2 );
}

Upvotes: 1

Axīc
Axīc

Reputation: 21

use this code, if you haven't price for all of your products, then text before the price will not show up!

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 ) {

$text = __('text-before-price-here:');

if ($price  == true) {
return '<span class="pre-price">'. $text . '</span> ' . $price;
}
else {

}
}

good luck ;))

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254492

You have just to inverted the price and the text:

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 ) {
    // Your additional text in a translatable string
    $text = __('TEXT');

    // returning the text before the price
    return $text . ' ' . $price;
}

This should work as you expect…

Upvotes: 7

Rajkumar Gour
Rajkumar Gour

Reputation: 1159

use "woocommerce_currency_symbol" hook something like this :

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
  switch( $currency ) {
    case 'AUD': $currency_symbol = 'AUD$'; break;
  }
  return $currency_symbol;
}

hope it will help

Upvotes: 0

Related Questions