Add a custom text before the price display in WooC

2019-05-28 16:11发布

问题:

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.

回答1:

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…



回答2:

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



回答3:

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