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

问题描述 投票:2回答:3

在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"

感谢您的帮助。

php wordpress woocommerce product price
3个回答
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;
}

当你想到这应该工作...


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

希望这将有助于


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 {

}
}

祝好运 ;))


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 );
}
© www.soinside.com 2019 - 2024. All rights reserved.