2

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!

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • 2
    Possible duplicate of [Shorten long numbers to K/M/B?](http://stackoverflow.com/questions/4371059/shorten-long-numbers-to-k-m-b) – Tristan Dec 19 '15 at 06:24
  • Code isn't my cup of tea. I've seen your question earlier before I post mine. Still have no idea how to add the code let alone on woocommerce plugin. – Irfan Affandi Dec 19 '15 at 08:58

2 Answers2

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

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.
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • It works! Only on shop page tho, not on cart page -- which is important 'cause cart table is a mess on mobile, t00 much 0s. Do you think you can add the filter to target only the subtotal on cart page? Since I get rid of the price column to shrink the table. Something like `add_filter('woocommerce_order_amount_subtotal','rei_woocommerce_order_amount_subtotal', 10, 2); function rei_woocommerce_order_amount_subtotal($subtotal, $product) { $currency = get_woocommerce_currency_symbol( ); $subtotal = $currency . custom_number_format($product->get_subtotal(),1); return $subtotal; }` – Irfan Affandi Dec 21 '15 at 04:37
  • look inside templates of woocommerce, find cart.php, you will get a hint there. try to code and come back if you have problems. that's how stackoverflow works. – Reigel Gallarde Dec 21 '15 at 04:41
0

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;
    ?>
vignesh warar
  • 27
  • 1
  • 8