Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to stylise the decimal values in prices in WooCommerce

How to stylise the decimal values in prices in WooCommerce | tychesoftwares.com

There are many ways in which you can customize your WooCommerce store. We saw one such request on Facebook where the member asked how he can stylise the decimal values on the WooCommerce price.

WooCommerce > Settings has some currency options where you can set the decimal separator, thousands separator, as well as the number of decimal points to be displayed in the price.

stylise the decimal values in WooCommerce through settings

If you don’t want any decimal value in your price, just set the ‘Number of Decimals’ value to 0.

In this post, I will explain 5 different ways in which you can display the decimals in the WooCommerce prices on your store.

1. Decimals as Superscript without decimal separator:

Let’s see how we can remove the decimal symbol and make the decimal display as superscript.

We will use a WooCommerce filter called ‘formatted_woocommerce_price‘ which is defined in the function wc_price() in the file ‘wc-formatting-functions.php‘.

We first need to find what the decimal is, in the product price. Let’s look at the code snippet:

In the above example, we calculate the decimal from the price. We use the <sup> HTML tag to display the decimal as superscript and then return the formatted price. The price will be displayed in this format everywhere – product page, shop page, cart & checkout page as well.

The product page will look like this –

.stylise the decimal values in WooCommerce in superscript

2. Decimals as Superscript with decimal separator:

If you want the decimal separator in the superscript, you can easily do that too. If you look at our filter, it has an argument called $decimal_separator. This is the same separator which we have set in the WooCommerce settings. We just have to include that in our final result. Here is an example –

Whatever text we add inside the <sup> tags will be displayed as superscript. Here is the screenshot of the product page –

stylise the decimal values in WooCommerce in superscript

3. Decimals as Subscript without decimal separator:

You can use the subscript tag ‘<sub>’ in place of the superscript tag ‘<sup>‘  in the above example, and the decimals will be displayed at the bottom.

Here is how the product page will look like –

How to stylise the decimal values in prices in WooCommerce - Tyche Softwares

4. Decimals as Subscript with decimal separator:

You can display the decimals in the subscript with decimal separator in the same way as we displayed the superscript. You just need to add the variable $decimal_separator in the return value. Here is the code snippet –

The price will be displayed like this –

stylise the decimal values in WooCommerce in subscript

5. Decimals in small font:

If you want to display the decimals in a small font, you can do that using the HTML tag called ‘<small>‘. Here is the code snippet –

The price on the product page will look like –

stylise the decimal values in WooCommerce in small font

The prices will be displayed in the same way on the shop page and cart page as well. Here is how the prices in the shop & cart page will be displayed if we stylise the decimals as superscript –

stylise the decimal values in WooCommerce on cart page
stylise the decimal values in WooCommerce on shop page

The code for calculating the decimals will be same. You just have to change the final return value to match your needs. If you have any other needs to stylise the decimals in WooCommerce prices, do let us know.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
9 Comments
Newest
Oldest
Inline Feedbacks
View all comments
2 years ago

Well done,
as Bri wrote, some of decimals are smaller about 0,01. To solve it try this snippet:
add_filter( ‘formatted_woocommerce_price’, ‘ts_woo_decimal_price’, 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
   $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
   $three_dp = $price – intval( $price );
   $two_dp = ( number_format($three_dp,2) * 100);
   if($two_dp == 100) {
       $two_dp = 0;
       $unit++;
   }
   $decimal = sprintf( ‘%02d’, ( $two_dp ) );
   return $unit . ‘<span style=”font-size: 0.75em;”>’ . $decimal_separator. $decimal . ‘</span>’;
}

2 years ago

Phew, this caused me some headaches… If you are also having rounding issues (float!) then this (admittedly hacky) overhauled function might do the trick:

function tweak_decimal_price($formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator) {
  $unit = number_format(intval($price), 0, $decimal_separator, $thousand_separator);
  $decimal = str_replace('0.','', wc_format_decimal(fmod($price, 1)));
  if(strlen($decimal) == 1) {
    $decimal = (int) $decimal . '0';
  }
  return $unit . $decimal_separator . '<sup>' . $decimal . '</sup>';
}

Please test.
We can assume that wc_format_decimal is available since we’re targeting a WC filter…

Last edited 2 years ago by E-VANCE
4 years ago

Hello,

When i implemented your code, some of the prices were wrong by one cent. I altered your code and now everything is fine.

//Create sup price
add_filter( ‘formatted_woocommerce_price’, ‘sitiweb_woo_decimal_price’, 10, 5 );
function sitiweb_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
//Display normal format in backend
if(!is_admin()&&is_woocommerce()){

//Format the price
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
$decimal = explode(“.”,$price);

return $unit . $decimal_separator. ” .$decimal[1] . ”;
}
else{
return $formatted_price;
}
}

4 years ago

For some reason this is not working for me. I am not getting any results?

adnan
4 years ago

thank you

Bri
5 years ago

Hi, I love this idea and for a while had it on my site. However, it seems that for some odd reason it subtracts 1p from my shipping price when active. So, £2.95 becomes £2.94

If you are able to fix this issue then I will add the function back to my site in a flash, otherwise it is unusable in its current form.

Dylan
3 years ago
Reply to  Bri

Here is the solution :

Replace the following line :
$decimal = sprintf( ‘%02d’, ( $price – intval( $price ) ) * 100 );

by :
$decimal = sprintf( ‘%02d’, strval(( $price – intval( $price ) ) * 100 ));

anon
7 months ago
Reply to  Dylan

It does not work in some places.

anon
7 months ago
Reply to  Dylan

It does not work in subtotal (checkout and cart)

9
0
Would love your thoughts, please comment.x
()
x