Add a custom text before the price display in WooC

2019-05-28 15:37发布

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.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-28 16:22

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

查看更多
迷人小祖宗
3楼-- · 2019-05-28 16:25

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

查看更多
Anthone
4楼-- · 2019-05-28 16:31

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…

查看更多
登录 后发表回答