Convert thousand to K, million to M in woocommerce

I am using woocommerce and I’d like to shorten all product prices to K (for thousand) and M (for million). So 150,000 would be 150K, 2,500,000 would be 2,5M etc. How do I do that?

Thank you!

Related posts

2 comments

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

    couple things to note here..

    1. woocommerce_sale_price_html does not include the original price.. you have to code it.
    2. the logic on currency format on WooCommerce is ignored. you may have to adjust the code according to your needs.
  2. I think this will help you

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

Comments are closed.