在WooCommerce的价格显示之前添加自定义文本

时间:2017-10-30 13:25:34

标签: php wordpress woocommerce product price

在WooCommerce中,我使用此代码在价格显示中添加文字:

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

页面显示为"$99,99 TEXT"

我想让它显示如下:"TEXT $99,99"

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

你只需颠倒价格和文字:

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

这应该像你期望的那样工作......

答案 1 :(得分:0)

使用“woocommerce_currency_symbol”挂钩如下:

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;
}
希望它会有所帮助

答案 2 :(得分:0)

使用此代码,如果您没有为所有产品定价,那么价格前的文字将不会显示!

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 {

}
}

祝你好运;))

答案 3 :(得分:0)

您可以使用此:

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