在woocommerce中将千位转换为K,百万转换为M.

时间:2015-12-19 06:18:32

标签: wordpress woocommerce

我正在使用woocommerce,我想将所有产品价格缩短为K(千分之一)和M(百分之百)。那么150,000将是150K,2,500,000将是2,5M等。我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:3)

add_filter('woocommerce_price_html','rei_woocommerce_price_html', 10, 2);
add_filter('woocommerce_sale_price_html','rei_woocommerce_price_html', 10, 2);
function rei_woocommerce_price_html($price, $product) {

    $currency = get_woocommerce_currency_symbol( );
    $price = $currency . custom_number_format($product->get_price(),1);

    return $price;
}

function custom_number_format($n, $precision = 3) {
    if ($n < 1000000) {
        // Anything less than a million
        $n_format = number_format($n);
    } else if ($n < 1000000000) {
        // Anything less than a billion
        $n_format = number_format($n / 1000000, $precision) . 'M';
    } else {
        // At least a billion
        $n_format = number_format($n / 1000000000, $precision) . 'B';
    }

    return $n_format;
}

这里需要注意的事情..

  1. woocommerce_sale_price_html不包含原始价格..您必须对其进行编码。
  2. 忽略了WooCommerce上货币格式的逻辑。您可能需要根据需要调整代码。

答案 1 :(得分:0)

我认为这会对你有帮助

<?php
         $n = 265460;

        function bd_nice_number($n) {

            $n = (0+str_replace(",","",$n));


            if(!is_numeric($n)) return false;


            if($n>1000000000000) return round(($n/1000000000000),1).'-T';
            else if($n>1000000000) return round(($n/1000000000),1).'B';
            else if($n>1000000) return round(($n/1000000),1).'M';
            else if($n>1000) return round(($n/1000),1).'K';

            return number_format($n);
        }

         $v = bd_nice_number($n);
        echo  $v;
    ?>
相关问题